집 >데이터 베이스 >MySQL 튜토리얼 >PHP를 사용하여 MySQL 테이블에서 행을 제거하기 위해 삭제 버튼을 구현하는 방법은 무엇입니까?
PHP를 사용하여 MySQL 테이블에서 행을 제거하는 삭제 버튼 추가
HTML에 표시된 MySQL 테이블 행에 삭제 옵션을 추가하려면 테이블에서 다음 단계를 수행할 수 있습니다.
<code class="php">$contacts = mysql_query(" SELECT * FROM contacts ORDER BY ID ASC") or die(mysql_error());</code>
<code class="php"><table id="contact-list"> <thead> <tr> <th>Name</th> <th>Email</th> <th>Telephone</th> <th>Address</th> <th>Delete</th> </tr> </thead> <tbody> //Iterate results and display data <?php while( $contact = mysql_fetch_array($contacts) ) : ?> <tr> <td><?php echo $contact['name']; ?></td> <td><?php echo $contact['email']; ?></td> <td><?php echo $contact['telephone']; ?></td> <td><?php echo $contact['address']; ?></td> <td> <form action='delete.php' method="post"> <input type="hidden" name="name" value="<?php echo $contact['name']; ?>"> <input type="submit" name="submit" value="Delete"> </form> </td> </tr> <?php endwhile; ?> </tbody> </table></code>
<code class="php">$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1"; // Execute the query to delete the record mysql_query ($query); // Handle success/failure scenarios if (mysql_affected_rows() == 1) { // Success: Contact deleted echo "<strong>Contact Has Been Deleted</strong><br /><br />"; } else { // Failure: Deletion failed echo "<strong>Deletion Failed</strong><br /><br />"; } </code>
<code class="php"><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></code>
위 내용은 PHP를 사용하여 MySQL 테이블에서 행을 제거하기 위해 삭제 버튼을 구현하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!