Php 从MySQL删除PDO
我的PDO代码有问题。 我尝试下面的代码Php 从MySQL删除PDO,php,mysql,database,pdo,sql-delete,Php,Mysql,Database,Pdo,Sql Delete,我的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"); } i
$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
,您也需要在准备查询和绑定参数后调用
更新:
您正在检查
$\u GET['t\u id']
的内容,但总是将$id设置为$\u REQUEST['t\u id']
,只有$\u POST['u DELETE\u']
包含DELETE
时,才会执行所有操作
另外,在执行之前,尝试使用检查结果查询和参数,并可能使用
$stmt->bindParam(1,$id,PDO::PARAM_INT)
$\u请求['t_id']
与不匹配?操作=删除。向我们展示完整的代码和您尝试的内容。同时,在打开后立即将错误报告添加到文件顶部,参见Falc关于您未执行的回答(至少,不是在发布的代码中);如果你真的“不”使用它。