【问题标题】:PDO delete from MySQL从 MySQL 中删除 PDO
【发布时间】: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


【解决方案1】:

准备好查询和绑定参数后,需要调用$stmt->execute()

更新:

您正在检查$_GET['t_id'] 的内容,但总是将$id 设置为$_REQUEST['t_id'],并且只有在$_POST['_DELETE_'] 包含delete 时才会执行所有操作。

另外,请在执行前尝试使用$stmt->debugDumpParams() 检查生成的查询和参数,并可能将您的bindParam 替换为$stmt->bindParam(1, $id, PDO::PARAM_INT)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 2011-06-26
    • 1970-01-01
    • 2017-03-19
    相关资源
    最近更新 更多