简体   繁体   中英

PHP delete button always starts deleting the last row

webbrowser上的booklist.php enter image description here

数据库表内容

数据库表结构

Okay, so this may be just simple for you guys since you have experience but I can't get to figure out why is it that everytime I hit "Delete" button on the first row of my table, it deletes the last row instead of the row on which I hit "Delete." Only happens on the first row. Second row would delete just perfectly on the database.

Is there anything wrong with my code or is there a problem with my table structure? I've provided my code for two separate php files namely booklist.php and edit.php I would appreciate your help as I am really new with PHP. Not yet familiar with all the rules and what not. Thanks.

//booklist.php below
echo "<form method='POST' action='edit.php'>";
    while($row = mysql_fetch_array($result))
    {
    $bookID_Var = $row['No'];
    $titleVar = $row['Title'];
    $authorVar = $row['Author'];  //authorVar is a variable container for the value of Author row in librarydatabase
    $ISBN = $row['ISBN'];

    echo "<tr>";
        echo "<td>" .$bookID_Var. "</td>";
        echo "<td>" . $titleVar. "</td>";   // there's no need to quote variables. In this line, we used variables $authorVar and $titleVar
        echo "<td>" . $authorVar. "</td>";
        echo "<td>" . $ISBN . "</td>";
        echo "<td> <input type='submit' value='Edit'> </td>"; // Edit button
        echo "<td> <input type='submit'  name='delete' value='Delete'> </td>"; // Delete button

        echo "</tr>"; 
    echo "<input type='hidden' name='bookID' value='  " .$bookID_Var. "  '>";

    } // end of while loop

    echo "</form>";



//edit.php below

<?php
if (isset($_POST['delete'])) {
    echo "entered delete if block <br />";
    echo "Value: " . $_REQUEST['bookID'];

    $bookID = $_REQUEST['bookID'];
    $sql = " DELETE FROM booklist WHERE No='$bookID'  ";
    $result = mysql_query($sql);

    if ($result) {
        echo "Successfully deleted" . "Book ID: " . $bookID;
    }
} // end of main if
?>

First. Follow conventions. Name your primary keys as id. Second - try to echo your query . Are you sure, if your $_REQUEST['bookID'] == 14? :-)

Replace

 $sql = " DELETE FROM booklist WHERE No='$bookID'  ";

to

echo  $sql = " DELETE FROM booklist WHERE No=".$bookID;exit;

and test which id is passed i am sure there is issue of passing an id.

You should use link action rather than submit action to delete based on rows:

Eg:

Change:

<input type='submit'  name='delete' value='Delete'>

to

<a href="edit.php?bookID=<?php echo $row['No'] ?>">Delete</a>

If you use one form for the entire data set, your hidden field will have the same name in every row. It seems the last one overrides all the previous ones.

There are better ways of passing down the id of each row, one simple method would be to separate out your deletion logic into delete.php , then you could put the Delete button as a link, and not a form, and have each row link to delete.php?bookid=<id> .

Try this approach :-

use hyperlink not input

echo "<a href='edit.php?act=delete&id=<?php echo $bookID; ?> '>Delete</a>"; 

or php code :-

<?php
if(isset($_GET['act']) && $_GET['act']=='delete'){
$bookID=$_GET['act'];
 $sql = " DELETE FROM booklist WHERE No='$bookID'  ";
}

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