简体   繁体   中英

Use Delete Button to Delete Record from Database

I have a database containing books. On a page, I have loop that prints each record in the database which each book's title, author, publisher, date, and rating. I want to use a delete button at the bottom of each record in order to delete it.

When I click on the delete button, the page is updated, but the record is not deleted.

I have tried to find solutions to this problem, but have yet to. I tried to use the book_id category in my database table as my index, in order to identify each record and delete it but it does not work.

Here is the code for my button form, as it appears with html code:

 while ($row = mysqli_fetch_array($result)) 
 {

 echo '<div id="forminput">';

$timestamp = strtotime($row['date']);

echo '<div id = "bookpicture">';
echo '<img src="images/Book Cover 8crop.jpg" alt="Book Cover">';
echo '</div>';

echo '<div id = "bookinfo">';
echo '<div id = "titleauthor">';
echo '<h3>' . htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8') . '</h3>';
echo '<p>' . htmlspecialchars($row['author'], ENT_QUOTES, 'UTF-8') . '</p>';

echo '</div>';
echo '</div>';

$id = htmlspecialchars($row['book_id'], ENT_QUOTES, 'UTF-8');

echo '</div>' ;
echo '<div id = "publisher">' ;
echo '<p>' . 'Published on' . " " . htmlspecialchars(date('F j, Y,', $timestamp),    
ENT_QUOTES, 'UTF-8') . 
" " . 'by' . " " . htmlspecialchars($row['publisher'], ENT_QUOTES, 'UTF-8') . '</p>';
echo '</div>';
echo '<div id = "formDelete">';
echo '<form name="deleteForm" method="post" id="deleteForm" action="index.php">'; 
echo '<input type="submit" value="Update" name="myAdd" id="myAdd" style = "width:  
100px" required>';
echo '<input type="submit" value="Delete" name="myDelete" id="$id" style = "width: 
100px" required>';
echo '</form>';
echo '</div>';
echo '</div>';
echo '<hr>' ;

echo '</div>';
}
?> 

Here is the code from my index.php file.

else if (isset($_POST['myDelete']))
{
$ids = mysqli_real_escape_string($link, $_POST['$id']);    

$sql="DELETE FROM readbooks WHERE book_id = '$ids'";

if (!mysqli_query($link, $sql))   

{   

$error = 'Error with submission: ' . mysqli_error($link);   

include 'php/error.html.php'; 

exit();   

  } 
}

Here is the updated code.

The Problem is you are not trying to send the row ID from the form.

In Form try sending row id from the form

 echo '<input type="hidden" name="id" value="$id">'

try receiving that parameter

 $id = $_POST['id'];
 $con = mysql_connect("host address","mysql_user","mysql_pwd");
 $query = "DELETE FROM readbooks WHERE id = $id";
 mysql_query($query,$con);

Moving from comment, you're not actually getting the $id anywhere. Add a field to your form:

<input type='hidden' name='id' value='$id'>

and then refer to it in your php:

$ids = mysqli_real_escape_string($link, $_POST['id']); 

I think you're having problem with the id that you are passing to your PHP code. Try to use var_dump($_POST) to see what is the content of your $_POST variable. As I can see, the index of $_POST is wrong.

<input type="submit" value="Delete" name="myDelete" id="$id" style = "width:100px" required>';

Try to change this to

<input type="submit" name="book_id" value="$id" style = "width:100px" required>';

In your php use this $_POST["book_id"] to get the value of book_id you posted.

Note : The attribute "name" of the input tags will be the index of your $_POST variable

I understand that you want to have a delete and update button at the same time. The problem is your logic behind the situation. You need to have a hidden field and put the id inside it. Try to use this.

echo '<form name="deleteForm" method="post" id="deleteForm" action="index.php">'; 
echo '<input type="hidden" value="$id" name="book_id" id="myAdd" required>';
echo '<input type="submit" value="Update" name="action" id="myAdd" required>';
echo '<input type="submit" value="Delete" name="action" id="$id" required>';
echo '</form>';

In your php code use try to have a condition like this :

$book_id = $_POST["book_id"];

if($_POST["action"] == "delete")
{ 
   //do your deletion code here. use the variable $book_id  
} 
else{
    //do your update code here.use the variable $book_id  
}

Since your using mysqli now, use prepared statements. Do not directly use your user input to the query! Example:

$books = array();
// connection
$con = new mysqli('localhost', 'your_username', 'password_of_username', 'your_database');

if(isset($_POST['myDelete'])) {
    $book_id = $_POST['myDelete']; // get the variable id
    $stmt = $con->prepare('DELETE FROM readbooks WHERE book_id = ?');
    $stmt->bind_param('i', $book_id);
    $stmt->execute();
}

$result = mysqli_query($con, 'SELECT * FROM readbooks');
while($row = $result->fetch_assoc()) {
    $books[] = $row;
}

?>

<form method="POST">
    <table border="1" cellpadding="10">
    <tr>
        <th>Title</th>
        <th>Author</th>
        <th>Publisher</th>
        <th></th>
    </tr>
    <?php foreach($books as $book): ?>
        <tr>
            <td><?php echo $book['title']; ?></td>
            <td><?php echo $book['author']; ?></td>
            <td><?php echo $book['publisher']; ?></td>
            <td><button type="submit" name="myDelete" value="<?php echo $book['book_id']; ?>">Delete</button></td>
        </tr>
    <?php endforeach; ?>
    </table>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM