
23-09-2007, 19:49
|
|
|
|
חבר מתאריך: 30.07.05
הודעות: 949
|
|
נניח שהמפתח של הטבלה שלך הוא שדה בשם id.
שדה ה-ID מאפיין לך את הרשומה, והוא מיוחד וייחודי לכל שורה בטבלה.
* התשובה שאני אתן תתאים למחיקה של מספר שדות במקביל, אם אתה מעוניין למחוק רק שדה אחד בכל פעם, תשתמש ב-RADIO.
כשאתה בונה את הטופס שלך, נשתמש ב-checkbox כדי לבחור אילו שורות ברצוננו למחוק.
כדי להקל על ניתוח הטופס מאוחר יותר, ה-checkbox-ים יקבלו כשם את ה-ID של הרשומה:
קוד PHP:
<form action="" method="POST"> <?php $sql_query = "SELECT id, ___ FROM ___"; // If you need to, you can add the WHERE. $result = mysql_query($sql_query); $num=mysql_numrows($result);
for ($i = 0; $i < $num; $i++) { // You add should all the design building... echo '<input type="checkbox" name="' . mysql_result($result,$i,"id") . '">\n'; // Echo the rest of the information from the sql query } ?> <input type="submit" value="Delete" name="sub"> </form>
כעת, כאשר ה-FORM שלנו בנוי, נבנה את החלק שמנתח את הנתונים המתקבלים ממנו ופועל בהתאם:
קוד PHP:
if (isset($_POST['sub'])) { // Proccess whatever you need before... $sql_query = "SELECT id FROM ___"; // If you need to, you can add the WHERE. $result = mysql_query($sql_query); $num=mysql_numrows($result);
for ($i = 0; $i < $num; $i++) { $id = mysql_result($result,$i,"id"); if($_POST[$id] == 'on') { $sql_query = "DELETE FROM ____ WHERE id='$id'"; mysql_query($sql_query); // Add checks and outputs at will... } }
// Proccess whatever you need after... }
כמובן שניתן לבצע את הכל באותו הדף, למשל ע"י בנייה של הדף שלך בצורה כזו:
קוד PHP:
<?php $sql_query = "SELECT id, ___ FROM ___"; // If you need to, you can add the WHERE. $result = mysql_query($sql_query); $num=mysql_numrows($result);
if(!isset($_POST['sub'])) { echo "<form action='" . $_SERVER['PHP_SELF'] . "' method='POST'>";
for ($i = 0; $i < $num; $i++) { // You should add all the design building... echo '<input type="checkbox" name="' . mysql_result($result,$i,"id") . '">\n'; // Echo the rest of the information from the sql query } echo "<input type='submit' name='sub' value='Delete'>\n"; echo "</form>"; } else{ //Proccess whatever you need before... for ($i = 0; $i < $num; $i++) { $id = mysql_result($result,$i,"id"); if($_POST[$id] == 'on') { $sql_query = "DELETE FROM ____ WHERE id='$id'"; mysql_query($sql_query); // Add checks and outputs at will... } }
// Proccess whatever you need after... } ?>
שיהיה בהצלחה 
_____________________________________
חתימתכם הוסרה כיוון שלא עמדה בחוקי האתר. לפרטים נוספים לחצו כאן. תוכלו לקבל עזרה להתאמת החתימה לחוקים בפורום חתימות וצלמיות.
|