[מדריך] PHP,SQL - הכנסה קריאה ומחיקה (מערכת תגובות)
למי שכבר התחיל ללמוד PHP
הנה מדריך שיעזור לו להתחיל ללמוד על SQL עם PHP .
אוקיי צרו מסד נתונים חדש, ודרך הPHPMYADMIN תכניסיו בו את הטבלאות הבאות.
מספר שדות : 5
שם הטבלה : topics
שדה : id סוג : int תוספות : auto_increment להגדיר את זה על מפתח ראשי .
שדה: title סוג : text
שדה: poster סוג : text
שדה : text סוג : text
שדה : date סוג : text
עמוד ה index.php
HTML קוד:
<?php
include "add.php";
?>
<?php
mysql_connect("localhost","nir2_test","123456");
mysql_select_db("nir2_test");
$query = mysql_query("SELECT * FROM topics ORDER BY id DESC");
?>
<?php
while($array = mysql_fetch_array($query)) {
$array["poster"] = str_replace("<","<",$array["poster"]);
$array["poster"] = str_replace(">",">",$array["poster"]);
$array["title"] = str_replace("<","<",$array["title"]);
$array["title"] = str_replace(">",">",$array["title"]);
$array["text"] = str_replace("<","<",$array["text"]);
$array["text"] = str_replace(">",">",$array["text"]);
$array["date"];
$array["id"];
echo "<br><table align='center' border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='330' height='81' id='AutoNumber1'>
<tr>
<td width='330' height='12' bgcolor='#66CCFF'><p align='right'>כותרת : ".$array["title"]." השולח : ".$array["poster"]." תאריך : ".$array["date"]." # ".$array["id"]." </p></td>
</tr>
<tr>
<td width='330' height='65'><p align='right'> ".$array["text"]." </p>----------------------------------------<br>
אפשרויות ניהול : <br> <a href='remove.php?id=".$array["id"]."'>מחק </a> | <a href='remove2.php'> מחק הכל </a><br></td></tr> </table>
</center>";
}
?>
<?php
mysql_close();
?>
בקוד הנ"ל התחברנו למסד נתונים, והצגנו בדף את מה שיש שם.
הוספת ההודעה add.php :
HTML קוד:
<?php
if($_POST["posted"] == 1) {
$title = $_POST["title"];
if(!$title) {
die("חובה לכתוב כותרת");
}
mysql_connect("localhost","dv","123456");
mysql_select_db("db");
$text = htmlspecialchars($_POST["text"], ENT_QUOTES);
$poster = $_POST["name"];
$date = date("d-m-Y H:i:s");
mysql_query("INSERT INTO `topics` VALUES ('','$title','$poster','$text','$date')") or die("שגיאה");
?>
<script language="JavaScript">
window.location=('index.php');
</script>
<?php
}
?>
<html dir="rtl">
<form action="index.php" method="post">
<input type="hidden" name="posted" value="1">
כותרת: <input type="text" name="title"><Br />
השולח: <input type="text" name="name"><Br />
תוכן: <br />
<textarea cols=50 rows=10 name="text"></textarea><br />
<input type="submit" name="submit" value="פרםס נושא">
</form>
פה בעצם זה העמוד שמכניס תוכן למסד, והתוכן מוצג בדף index.php
בעצם אתם מוסיפים הודעה ורואים אותה ב index.php.
[/HTML]
הקובץ remove.php
HTML קוד:
<?php
mysql_connect("localhost","nir2_test","123456");
mysql_select_db("nir2_test");
mysql_query("DELETE FROM topics WHERE id=$id");
?>
<script language="JavaScript">window.location=('index.php');</script>
<?php
?>
הקובץ הזה מוחק את ההודעה, לפי ה ID שלה.
רצוי לעשות את זה בפאנל ניהול ולהשתמש בהגנת דף באמצעות סיסמא שיש בפורום.
המערכת הזואתי יכולה לעזור למתחילים, ותוכלו לפתח את הקוד ולהתקדם איתו .
מקווה שהבנתם את המדריך, המדריך מיועד למתחילים בphp ו sql
מי שרוצה את הקבצי מקור ניתן להוריד מפה -
http://www..net/posted.rar
|