[英]Delete from MYSQL Table
I have several forms that delete various rows from the table the user is viewing. 我有几种形式可以从用户正在查看的表中删除各行。 I am using MYSQL and PHP.
我正在使用MYSQL和PHP。 I have the same code below (As far as i can tell) on another page and it works fine.
我在另一页上有下面相同的代码(据我所知),它工作正常。 For some reason i cannot get my code to execute.
由于某种原因,我无法执行我的代码。 It gives the update failed error.
它给出了更新失败的错误。
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" />';
?>
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 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 致命错误: delete_equipment.php:16中堆栈错误:#0 delete_equipment.php(16):PDO-> prepare( '')#1 {main}在第16行抛出indelete_equipment.php
VarDump for equipmentID string(8) "E1202612" 设备ID字符串(8)“ E1202612”的VarDump
Change 更改
$result = $statement -> execute(array('equipmentID' => $equipmentID));
to 至
$result = $statement -> execute(array(':equipmentID' => $equipmentID));
^
UPDATE: Turn on exception throwing and disable prepare emulation. 更新:打开引发异常并禁用准备仿真。
$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);
UPDATE2: Change UPDATE2:更改
$statement = $db->prepare($delete_equipment_single);
to 至
$statement = $db->prepare("DELETE FROM EquipmentItem WHERE equipmentID = :equipmentID");
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
This means the query you tried to prepare was empty. 这意味着您尝试准备的查询为空。 You can verify by running:
您可以通过运行以下命令进行验证:
var_dump($delete_equipment_single)
immediately before $db->prepare()
$db->prepare()
之前的var_dump($delete_equipment_single)
$db->prepare()
From your code it looks like $delete_equipment_single
is defined in a different file? 从您的代码看起来
$delete_equipment_single
是在其他文件中定义的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.