הוסטס - פורום אחסון האתרים הגדול בישראל

הוסטס - פורום אחסון האתרים הגדול בישראל (https://hosts.co.il/forums/index.php)
-   תכנות - מדריכים, code snippets (https://hosts.co.il/forums/forumdisplay.php?f=68)
-   -   [מדריך] PHP,SQL - הכנסה קריאה ומחיקה (מערכת תגובות) (https://hosts.co.il/forums/showthread.php?t=22761)

OldMega 29-05-06 02:52

[מדריך] 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"]." &nbsp;&nbsp;  השולח : ".$array["poster"]." &nbsp;&nbsp;  תאריך : ".$array["date"]."  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ".$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

ohadamar 29-05-06 03:42

תודה רבה =]
אם אפשר לקבל את הקוד של הSQL יהיה טוב :P

adiga000 29-05-06 05:26

תודה אחי יעזור לחלשים

eXtaZa 29-05-06 05:49

כל הכבוד על היזמה! מדריך טוב!

-VladK- 29-05-06 07:03

פשוט וטוב! כל הכבוד על היוזמה...אבל אתה לא חושב שצריך לעשות אפשרות של עריכה למקרה שהתגובה קצת לא בסדר אבל עדיין רוצים שהיא תהיה?

OldMega 29-05-06 09:45

אממ צריך אני יוסיף את זה...

meshuga 29-05-06 09:47

ציטוט:

נכתב במקור על ידי BarMan
אממ צריך אני יוסיף את זה...

אממ...יש אחד כזה (http://www.hosts.co.il/forums/showthread.php?p=238013) אבל אני יוסיף בכל מקרה...שיהיו 2, אולי יבינו יותר ככה.
בכל מקרה.....
משתמשים, לפני שאתם כותבים מדריך תסתכלו אם הוא קיים!


כל הזמנים הם GMT +2. הזמן כעת הוא 02:48.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ