1

我有几个表单可以从用户正在查看的表中删除各种行。我正在使用 MYSQL 和 PHP。我在另一个页面上有相同的代码(据我所知),它工作正常。由于某种原因,我无法让我的代码执行。它给出了更新失败的错误。

try 
{
    $db = new PDO('mysql:host=$hname;dbname=' . $DBname, $DBusername, $DBpassword, array(PDO::ATTR_PERSISTENT => true));
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}

catch ( Exception $ex ) 
{
    die ( 'Cannot Connect to the Database.' );
}

$delete_equipment_single = "DELETE FROM EquipmentItem 
                              WHERE equipmentID = :equipmentID";    


equipment_delete.php
<?php require_once('php/security.php'); ?>
<?php

// Get the ID Tag
if( !isset($_GET['equipmentID'] ))
{
    echo "You Didn't Supply a equipmentID - (equipmentID) [ERROR x1]";
    exit();
}

require_once('php/database.php');

$equipmentID = htmlspecialchars($_GET['equipmentID']);

// Delete the Equipment
$statement = $db->prepare($delete_equipment_single);
$result = $statement -> execute(array(
    ':equipmentID' => $equipmentID
));

// Make Sure Everything Worked
    if( $result == false )
    {
        die('Update Failed, Please Check Your Database. [ERROR x2]');
    }

echo '<meta http-equiv="Refresh" Content="0; url=display_equipment.php?a=1" />'; 

?>

致命错误:未捕获的异常“PDOException”和消息“SQLSTATE [42000]:语法错误或访问冲突:delete_equipment.php:16 中的 1065 查询为空”堆栈跟踪:#0 delete_equipment.php(16):PDO->prepare( '') #1 {main} 在第 16 行抛出 indelete_equipment.php

设备ID字符串(8)“E1202612”的 VarDump

4

2 回答 2

1

改变

$result = $statement -> execute(array('equipmentID' => $equipmentID));

$result = $statement -> execute(array(':equipmentID' => $equipmentID));
                                       ^

更新:打开异常抛出并禁用准备模拟。

$db = new PDO('mysql:host=localhost;dbname=mydb;charset=UTF-8', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

更新2:改变

$statement = $db->prepare($delete_equipment_single);

$statement = $db->prepare("DELETE FROM EquipmentItem WHERE equipmentID = :equipmentID");
于 2013-03-12T04:04:21.003 回答
0
1065 Query was empty' in delete_equipment.php:16 
Stack trace: #0 delete_equipment.php(16): PDO->prepare('') 
  #1 {main} thrown indelete_equipment.php on line 16

这意味着您尝试准备的查询是空的。您可以通过运行来验证:

var_dump($delete_equipment_single)就在之前$db->prepare()

从您的代码来看,它看起来像是$delete_equipment_single在不同的文件中定义的?

于 2013-03-12T06:22:59.700 回答