Quick PHP SQL Question

  • theBruney
  • Student
  • Student
  • User avatar
  • Posts: 67
  • Loc: China

Post 3+ Months Ago

Hello there,

I've recently stumbled upon something strange with some PHP\SQL where a DELETE statement only deletes a record on the id column or by directly plugging in the conditional value instead of using a variable. Which should not be so. Here is the code that I was working with:

Code: [ Select ]
$ClientDeleteID = $_GET['Cid']; 
    mysql_select_db($database, $connection);               
    $deleteSQL = "DELETE FROM `client` WHERE `client_id` = '$ClientDeleteID' ";  
    $Result1 = mysql_query($deleteSQL, $connection) or die(mysql_error());
  1. $ClientDeleteID = $_GET['Cid']; 
  3.     mysql_select_db($database, $connection);               
  4.     $deleteSQL = "DELETE FROM `client` WHERE `client_id` = '$ClientDeleteID' ";  
  5.     $Result1 = mysql_query($deleteSQL, $connection) or die(mysql_error());

client_id is actually the users' name, containing both letters and numbers. I have echoed $ClientDeleteID and compared its result to that stored in the database; No problems there the values are the same, but the statement still does not delete the row where `client_id` = '$ClientDeleteID'. If I change the code to only delete by the key id column, it deletes the row. I have also tried plugging in the value of $_GET['Cid'] directly into the query instead of the variable $ClientDeleteID and the row then deletes. Just wondering why it will not delete with the the variable $ClientDeleteID.

By the way, I've tried changing the data type of the column client_id from vachar to text and the like, to see if there was a conflict, renamed $ClientDeleteID to something short like $cid (not that it should matter), still the row will not be deleted. Where might I be going wrong?

  • Stoot
  • Newbie
  • Newbie
  • Stoot
  • Posts: 5

Post 3+ Months Ago

Try concatenating the variable into the string.

$deleteSQL = "DELETE FROM `client` WHERE `client_id` = '" . $ClientDeleteID . "' ";

Or printing the query variable to give some debugging hints
  • mishka
  • Novice
  • Novice
  • mishka
  • Posts: 20
  • Loc: Toronto

Post 3+ Months Ago

I would urge U to acquire as fast as possible basics methods of debugging.

1. (I use it a lot) ... echo your query
echo $deleteSQL = "DELETE FROM `client` WHERE `client_id` = '$ClientDeleteID' ";

than cut & paste it in order to invoke this statement directly from mysql engine (console or GUI, whatever U use) .... see the results.
If the statements fails ... than its mysql error (may be syntax) ... if not than ... something not in-tact with the logic.

2. Also
$Result1 = mysql_query($deleteSQL, $connection) or die(mysql_error());

from that line, if something wrong, depends on your settings ... the error should be dump to screen or routed to error file ... it worth to check its content

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 35 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.