doraemon0769 2015-11-02 07:38
浏览 49
已采纳

PHP删除按钮始终开始删除最后一行

booklist.php on webbrowserenter image description here

Database Table Contents

Database Table Structure

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
?>
  • 写回答

5条回答 默认 最新

  • duanqian8867 2015-11-02 07:50
    关注

    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'  ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?