Ich habe die Ergebnisse einer MySQL-Tabelle in eine HTML-Tabelle ausgegeben. In der letzten Spalte möchte ich eine Löschoption hinzufügen, die ein anderes Formular aufruft und den Benutzer aus der MySQL-Tabelle löscht. Ich bekomme es aber irgendwie nicht zum Laufen.
Dies ist mein Code für die Ergebnisseite:
<?php
$contacts = mysql_query("
SELECT * FROM contacts ORDER BY ID ASC") or die( mysql_error() );
// If results
if( mysql_num_rows( $contacts ) > 0 )
?>
<table id="contact-list">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Telephone</th>
<th>Address</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while( $contact = mysql_fetch_array( $contacts ) ) : ?>
<tr>
<td class="contact-name"><?php echo $contact['name']; ?></td>
<td class="contact-email"><?php echo $contact['email']; ?></td>
<td class="contact-telephone"><?php echo $contact['telephone']; ?></td>
<td class="contact-address"><?php echo $contact['address']; ?></td>
<td class="contact-delete"><form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
und das ist mein delete.php-Skript
<?php
//Define the query
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
//sends the query to delete the entry
mysql_query ($query);
if (mysql_affected_rows() == 1) {
//if it updated
?>
<strong>Contact Has Been Deleted</strong><br /><br />
<?php
} else {
//if it failed
?>
<strong>Deletion Failed</strong><br /><br />
<?php
}
?>
Ich kann mir nicht erklären, warum das nicht funktioniert.
Bitte nicht verwenden
mysql_*
Funktionen in neuem Code. Sie werden nicht mehr gepflegt und sind offiziell veraltet. Siehe die rote Kiste? Lernen vorbereitete Erklärungen stattdessen und verwenden PDO oder MySQLi – Dieser Artikel hilft Ihnen bei der Entscheidung, welche. Wenn Sie sich für PDO entscheiden, hier ist eine gute Anleitung.– h2oooooo
17. Juni 2013 um 10:07 Uhr