简体   繁体   中英

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. 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

VarDump for equipmentID string(8) "E1202612"

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

$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()

From your code it looks like $delete_equipment_single is defined in a different file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM