Heim  >  Fragen und Antworten  >  Hauptteil

So fügen Sie eine Löschschaltfläche im PHP-Formular hinzu, um eine Zeile aus der MySQL-Tabelle zu löschen

<p>Ich habe die Ergebnisse der 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. Aber ich schaffe es scheinbar nicht, es zum Laufen zu bringen.</p> <p>Dies ist der Code für meine Ergebnisseite: </p> <pre class="brush:php;toolbar:false;"><?php $contacts = mysql_query(" SELECT * FROM communications ORDER BY ID ASC") oder die( mysql_error() ); // Wenn es ein Ergebnis gibt if( mysql_num_rows( $contacts ) > 0 ) ?> <table id="contact-list"> <thead> <tr> <th>Name</th> <th>E-Mail</th> <th>Telefon</th> <th>Adresse</th> <th>Löschen</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></pre> <p>Dies ist mein delete.php-Skript: </p> <pre class="brush:php;toolbar:false;"><?php //Abfrage definieren $query = „LÖSCHEN AUS Kontakten WHERE name={$_POST['name']} LIMIT 1“; //Anfrage senden, um Eintrag zu löschen mysql_query ($query); if (mysql_affected_rows() == 1) { //Wenn das Löschen erfolgreich ist ?> <strong>Kontakt gelöscht</strong><br /><br /> <?php } anders { //Wenn das Löschen fehlschlägt ?> <strong>Löschen fehlgeschlagen</strong><br /><br /> <?php } ?></pre> <p>Ich verstehe nicht, warum das nicht funktioniert. </p>
P粉174151913P粉174151913394 Tage vor403

Antworte allen(2)Ich werde antworten

  • P粉976737101

    P粉9767371012023-08-23 09:35:07

    使用javascript

    <input name="Submit2" type="button" class="button" onclick="javascript:location.href='delete.php?id=<?php echo $your_id;?>';" value="&laquo; 返回" />

    在delete.php中

    $id=$_GET['id'];

    并将$id放入你的sql语句中。

    Antwort
    0
  • P粉289775043

    P粉2897750432023-08-23 09:15:04

    您必须在删除链接中传递一个变量。您必须在隐藏字段中传递<?php echo $contact['name']; ?>name值)或将此值传递给URL

    替换为

    <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>

    使用

    <td class="contact-delete">
        <form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
            <input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
            <input type="submit" name="submit" value="Delete">
        </form>
    </td>

    Antwort
    0
  • StornierenAntwort