Jump to content

mySQL query string help


RCurtis
Go to solution Solved by .josh,

Recommended Posts

I realize this straddles the line into MySQL...I am hoping someone can show me another way...

 

I have the following code that builds a sql query string:

 

$sql='UPDATE mytable
 SET user_class = $class_list
 WHERE user_id = $userid ';

 

When I "echo" it out for troubleshooting it looks right.  However...I have a situation where the $class_list string translates to an empty string "".  This appears to be throwing MySQL into a syntax error:  It seems to think the column I am referencing in the WHERE is the $userid instead of user_id (which IS correct)

 

UPDATE e107_test SET user_class = $class_list WHERE user_id = $userid

Could not update class: Unknown column '$userid' in 'where clause'

 

When I do this:

 

$sql='UPDATE mytable
 SET user_class = "$class_list"
 WHERE user_id = "$userid" ';

 

This is the echo result - UPDATE mytable SET user_class = "$class_list" WHERE user_id = "$userid"

 

It no longer complains about an Unknown column but it also does not update any records.

 

I have the following to help troubleshoot:

 

$sqlresult = mysql_query( $sql1, $conn );
if(! $sqlresult )
{
  die('Could not update class: ' . mysql_error());
  exit;
}
else
{
echo 'Changed 1 record';
echo '<br>';
}  //This is the end of the $sql1 query

 

Here is what I get:

 

UPDATE mytable SET user_class = "$class_list" WHERE user_id = "$userid" Changed 1 record

 

I get the "Changed 1 record" output for every record in table but refreshing the table in phpMyAdmin shows that no data has changed.

I know I'm doing something stupid.... ??

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.