PDO Delete query doesn't work

264
August 24, 2017, at 10:34 PM

i have this code that don't want work, and i can't understand why...

$DeleteT=$con->prepare("DELETE Examples FROM USERS WHERE Email=:Email AND Pass=:Pass");
$DeleteT->bindParam(':Pass', $Pass);
$DeleteT->bindParam(':Email', $Email);
$DeleteT->execute(array(
      ':Email' => $Email,
      ':Pass' => $Pass,
));

I have already tried to search for example of delete query, but i still can't understand why, my value don't want to be delete from database, i have also check if the name of the table and the name of the database match, and they do.

Answer 1

You are currently binding the variables twice - once through bindParam(), then once again as the argument to execute(). You need to bind the variable once, either by bindParam() or by execute(), but not both at the same time.

Also, the syntax for deleting is DETLETE FROM..., not DELETE column FROM....

So it'll either look like this, binding them through execute()

$DeleteT = $con->prepare("DELETE FROM USERS WHERE Email=:Email AND Pass=:Pass");
$DeleteT->execute(array(
  ':Email' => $Email,
  ':Pass' => $Pass
));

or, if you want to use bindParam()...

$DeleteT = $con->prepare("DELETE FROM USERS WHERE Email=:Email AND Pass=:Pass");
$DeleteT->bindParam(':Pass', $Pass);
$DeleteT->bindParam(':Email', $Email);
$DeleteT->execute();

Either is fine for this purpose, choose which one you like most. PDOStatement::bindParam() has additional options should you need it (there's additional information on that in the documentation linked below), but for the purpose of binding two strings, either way is applicable.

Update:
As per the comments, you wish to just empty out the Examples column. This means that you want to UPDATE the row, not DELETE it. The example below would set the Example column to an empty string (assuming that it's a text or varchar column, and accept strings). If you wish to set it to NULL or another value, modify it as such.

$DeleteT = $con->prepare("UPDATE USERS SET Examples=:example WHERE Email=:Email AND Pass=:Pass");
$DeleteT->execute(array(
  ':example' => '', 
  ':Email' => $Email,
  ':Pass' => $Pass
));

SECURITY NOTE:
You should NEVER store passwords in plain-text, or by using poor methods of hashing (such as md5(), sha1(), etc). It's simply not secure! PHP has built-in functions which you should use to handle storing of passwords, see the password_hash() function which is a lot more secure!

  • PHP.net on PDOStatement::bindParam()
  • PHP.net on PDOStatement::execute()
  • MySQL documentation on DELETE
Answer 2

You are binding on the wrong variable you need to bind on $Delete instead of $DeleteT

This is what you need

$DeleteT=$con->prepare("DELETE Examples FROM USERS WHERE Email=:Email AND Pass=:Pass")->execute(array(':Email' => $Email,':Pass' => $Pass));

PS : Note the extra , on ':Pass' => $Pass,)); remove it. Also don't store password in plain text, use password_hash() and password_verify()

Answer 3

Your syntax for the delete isn't standard, should be delete from tableName ...

    $DeleteT=$con->prepare("DELETE FROM USERS WHERE Email=:Email AND Pass=:Pass");
    $DeleteT->execute(array(
      ':Email' => $Email,
      ':Pass' => $Pass
    ));
Rent Charter Buses Company
READ ALSO
Replace the glob () function with an Array from select

Replace the glob () function with an Array from select

I would like to have the opportunity to decide the position of the photos in the folders

252
MySql subquery inside View

MySql subquery inside View

I created a view using MySql subquery, it worked on my localhost, it failed in production because Mysql version is 56, it doesn't support subqueries on views, so i need help rewriting the whole query, I tried with UNION ALL but it tells me i need the same amount...

276
Three columns index

Three columns index

I have the following table:

203