所以我有这段代码:
echo "<h1>Books</h1>";
echo "<table cellpadding=5 border=1>";
echo "<tr><th>Title</th><th>Author</th><th>Year</th><th>Deleting</th></tr>";
foreach ($array as $value) {
echo "<tr><td>".$value['title']."</td>";
echo "<td>".$value['author']."</td>";
echo "<td>".$value['year']."</td>";
echo "<td><input type='button' id='".$value['id']."' value='delete' name='delete'></td></tr>";
}
echo "</table>";
我正在使用它将其从我的数据库中删除:
if(isset($_GET['delete'])){
$deleted = $_GET['id'];
$new ="DELETE FROM `book`.`book` WHERE `book`.`id` = $deleted;";
$dbh->query($new);
}
我使用PDO作为mysql数据库的连接字符串。在数据库中,我有ID,标题,作者,年份列。
所以当我点击新形成的表格时,按钮&#34;删除&#34;什么都没发生。 我做错了什么?
谢谢!
答案 0 :(得分:4)
由于您使用的是type="button"
,因此不会提交表单。
使用type="submit"
提交带有按钮标记的表单,然后将$value['id']
设置为value=""
:
echo "<td><button type='submit' value='".$value['id']."' name='delete'>Delete</button</td></tr>";
如果您尚未设置,请确保<form>
标记包含<table>
并且具有与您的PHP对应的method
类型:
<form method="GET">
既然您正在使用PDO,为什么不使用预先准备好的语句:
if(isset($_GET['delete'])){
$deleted = $_GET['delete'];
$new ="DELETE FROM `book`.`book` WHERE `book`.`id` = :id";
$delete = $dbh->prepare($new);
$delete->bindParam(':id', $deleted, PDO::PARAM_INT);
$delete->execute();
}
假设您已经连接。