从MYSQL表中删除

时间:2013-03-12 03:22:53

标签: php mysql

我有几种表单可以删除用户正在查看的表中的各行。我正在使用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]:语法错误或访问冲突:1065查询为空'在delete_equipment.php中:16堆栈跟踪:#0 delete_equipment。 php(16):PDO-&gt;在第16行准备('')#1 {main}抛出indelete_equipment.php

VarDump for equipmentID string(8)“E1202612”

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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在另一个文件中定义了吗?