【发布时间】:2016-03-25 06:01:39
【问题描述】:
我正在尝试使用以下代码删除我的数据库中的一个条目。 javascript 函数使用正确的“adventureID”将我带到index.php?delpost=,但是当我检查我的数据库时,该行仍然存在。我最近开始使用 PDO,所以我想知道 execute() 语句是否可能是问题所在。 $dbh 在页面顶部连接到我的数据库,它正在打印我试图从中删除行的表中的每一行。我的目标是在调用 javascript 函数时成功删除一行。问题是 - 它没有。
<script language="JavaScript" type="text/javascript">
function delpost(adventureID, title)
{
if (confirm("Are you sure you want to delete '" + title + "'" + " '" + adventureID + "'"))
{
window.location.href = 'index.php?delpost=' + adventureID;
}
}
</script>
<?php
if(isset($_GET['delpost'])){
$stmt = $dbh->prepare("DELETE FROM adventure WHERE adventureID = :adventureID");
$stmt->execute(array(':adventureID' => $_GET['delpost']));
header('Location: index.php?action=deleted');
exit;
}
?>
<?php
if(isset($_GET['action'])){
echo '<h3>Post '.$_GET['action'].'.</h3>';
}
try {
foreach($dbh->query("SELECT adventureID, title, postDate FROM adventure ORDER BY adventureID DESC") as $row) {
echo '<tr>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.date('jS M Y', strtotime($row['postDate'])).'</td>';
?>
<td>
<a href="javascript:delpost('<?php echo $row['adventureID'] ?>','<?php echo $row['title'] ?>')">Delete</a>
</td>
<?php
echo '</tr>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
【问题讨论】:
-
mysql在运行命令时会说什么?
-
如果我在 phpMyAdmin 中运行该命令,它会删除该行
-
返回给php的是什么?
-
将
try {移到if(isset($_GET['delpost']))之前 -
好吧,把重定向注释掉...
标签: javascript php mysql pdo