Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用“删除”按钮从数据库中删除记录_Php_Mysql_Database_Button_Delete Row - Fatal编程技术网

Php 使用“删除”按钮从数据库中删除记录

Php 使用“删除”按钮从数据库中删除记录,php,mysql,database,button,delete-row,Php,Mysql,Database,Button,Delete Row,我有一个包含书籍的数据库。在一个页面上,我有一个循环,它打印数据库中的每条记录,记录每本书的标题、作者、出版商、日期和评级。我想使用每个记录底部的删除按钮来删除它 当我点击删除按钮时,页面会被更新,但记录不会被删除 我试图找到解决这个问题的办法,但还没有找到。我尝试使用数据库表中的book_id类别作为索引,以标识每个记录并将其删除,但它不起作用 以下是我的按钮表单的代码,它与html代码一起出现: while ($row = mysqli_fetch_array($result)) {

我有一个包含书籍的数据库。在一个页面上,我有一个循环,它打印数据库中的每条记录,记录每本书的标题、作者、出版商、日期和评级。我想使用每个记录底部的删除按钮来删除它


当我点击删除按钮时,页面会被更新,但记录不会被删除

我试图找到解决这个问题的办法,但还没有找到。我尝试使用数据库表中的book_id类别作为索引,以标识每个记录并将其删除,但它不起作用

以下是我的按钮表单的代码,它与html代码一起出现:

 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>';
}
?> 

这是更新后的代码。

从注释开始,您实际上没有在任何地方获得$id。在表单中添加一个字段:

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

我认为您传递给PHP代码的id有问题。尝试使用var_dump($_POST)查看$_POST变量的内容。正如我所看到的,$u POST的索引是错误的

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

由于您现在正在使用mysqli,请使用准备好的语句。不要直接使用您的用户输入进行查询!例如:

$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>
$books=array();
//联系
$con=newmysqli('localhost','your_username','password_of_username','your_database');
如果(isset($_POST['myDelete'])){
$book\u id=$\u POST['myDelete'];//获取变量id
$stmt=$con->prepare('DELETE FROM readbook,其中book_id=?');
$stmt->bind_参数('i',$book_id);
$stmt->execute();
}
$result=mysqli_查询($con,'SELECT*FROM readbook');
而($row=$result->fetch_assoc()){
$books[]=$row;
}
?>
标题
作者
出版商

问题是您没有尝试从表单发送行ID

在表单中,尝试从表单发送行id

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

什么不起作用,缩小到问题所在当我单击delete按钮时,页面被刷新,但记录没有被删除。表单输入的值不是它的id,所以你得到的“delete”不是你在
$\u POST['myDelete']
中想要的id。你是否从表单的DB中提取行?如果没有,那就去做。除了
$id=htmlspecialchars($row['book\u id',entu QUOTES,'UTF-8'),我看不到任何类似的效果
则无。在php中,您正在查找无效的
$\u POST['$id']
。它应该是
$\u POST['myDelete']
。。事实上,等等。这还不够。我看不到您的表单中有任何地方设置了名为“id”的输入。。你可以试试:
然后使用
$\u POST['id']
这确实有道理。现在,让我们看看OP是否会说“万岁”;-)然后我可以用它来识别每条记录?循环中每条记录都有一个删除按钮?是的,每条记录都有一个删除按钮。如果每个按钮都不同,那么值是多少?我试过了,但没有成功。同样的事情也发生在我上面的帖子中。你只需要发送id,替换这行echo“”@带$id=$row['id'];回声';希望这会有帮助。
<input type="submit" name="book_id" value="$id" style = "width:100px" required>';
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>';
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  
}
$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>
 echo '<input type="hidden" name="id" value="$id">'
 $id = $_POST['id'];
 $con = mysql_connect("host address","mysql_user","mysql_pwd");
 $query = "DELETE FROM readbooks WHERE id = $id";
 mysql_query($query,$con);