简体   繁体   English

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

[英]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. 我已经为两个单独的php文件(即booklist.php和edit.php)提供了代码,感谢您的帮助,因为我对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. 将您的primary keys命名为id。 Second - try to echo your query . 第二,尝试回显您的query Are you sure, if your $_REQUEST['bookID'] == 14? 您确定$_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. 并测试通过哪个ID,我确定存在通过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> . 有更好的方法向下传递每一行的ID,一种简单的方法是将删除逻辑分离为delete.php ,然后可以将Delete按钮作为链接(而不是表单)作为链接,并将每一行链接delete.php?bookid=<id>

Try this approach :- 试试这个方法:

use hyperlink not input 使用hyperlinkinput

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

or php code :- 或PHP代码:-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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