3

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Stephane
  • 71
  • 2
  • 11
  • whats not working, narrow down to where the problem is – user1978142 Jul 04 '14 at 02:41
  • When I click on the delete button, the page is refreshed, but the record is not deleted. – Stephane Jul 04 '14 at 02:41
  • the **value** of a form input is sent not its id, so you get "Delete" not the id you want in `$_POST['myDelete']` –  Jul 04 '14 at 02:44
  • Are you fetching your row from DB in your form? If not, then do. I don't see anything to that effect, except `$id = htmlspecialchars($row['book_id'], ENT_QUOTES, 'UTF-8');` no `while`, no nothing. – Funk Forty Niner Jul 04 '14 at 02:44
  • In your php, you're looking for `$_POST['$id']` which isn't valid. It should be `$_POST['myDelete']` .. actually wait. that isn't enough. I don't see anywhere in your form where you set an input with the name 'id' .. might try that: `` and then use `$_POST['id']` – John C Jul 04 '14 at 02:44
  • I connect to the database fine. I simply did not insert the relevant code. – Stephane Jul 04 '14 at 02:45
  • Yes, I am fetching the row from the database. – Stephane Jul 04 '14 at 02:45
  • Well it's either as John said, or `$_POST['id']` that `$` next to `id` shouldn't be there. Yet, you don't have an element named `id` so it's likely for `name="myDelete"` – Funk Forty Niner Jul 04 '14 at 02:46
  • select all values and loop all books, print them each including the name and more importantly the id, handle the form, using the id, use paramertized query then execute – user1978142 Jul 04 '14 at 02:47
  • Developer's tool: Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` see if it yields anything. Plus, `var_dump($variable);` of your choice. – Funk Forty Niner Jul 04 '14 at 02:51

4 Answers4

1

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);
Arun G
  • 1,678
  • 15
  • 17
  • I tried that, but it did not work. The same thing happens as stated in my above post. – Stephane Jul 07 '14 at 00:54
  • All you need is to send id, replace this line echo ''; @with $id = $row['id']; echo ''; Hope it will help. – Arun G Jul 09 '14 at 06:07
0

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']); 
John C
  • 666
  • 4
  • 8
0

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  
}
Imat
  • 500
  • 2
  • 4
  • 15
0

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>