【发布时间】:2014-11-22 17:07:21
【问题描述】:
我的 PDO 代码有问题。 我尝试下面的代码。
$id = null;
if ( !empty($_GET['t_id'])) {
$id = $_REQUEST['t_id'];
}
$action = isset($_POST['_DELETE_']) ? $_POST['_DELETE_'] : "";
if ($action == 'do_not_delete') {
header("Location: index.php?action=DEL_ERROR");
}
if($action=='delete') {
$host = "localhost";
$db_name = "_notice";
$username = "root";
$password = "111";
$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
$id = $_REQUEST['t_id'];
$query = "DELETE FROM topics WHERE topic_id = ?";
$stmt = $con->prepare($query);
$stmt->bindParam(1, $id);
$exc = $stmt->execute();
if($exc){
$con = null;
header("Location: index.php?action=DEL_OK");
}else{
$con = null;
header("Location: index.php?action=DEL_ERROR");
}}
任何事情都会发生(不要从数据库中删除元素)。
我在页面上没有错误;即使我使用了 try catch 块,或者像 index.php?action=DELETE 这样的页面参数
【问题讨论】:
-
$_REQUEST['t_id']与您的?action=DELETE不匹配。向我们展示完整的代码以及您尝试的确切内容。同时,在您打开<?php标签error_reporting(E_ALL); ini_set('display_errors', 1);后立即在文件顶部添加错误报告,看看它是否产生任何结果。 -
查看 Falc 关于您未执行的回答(至少,不在发布的代码中);如果您确实“不”使用它。
标签: php mysql database pdo sql-delete