Jump to content

Spot the error: MySQL Update statement in PHP


Ty44ler

Recommended Posts

Been looking at this for a few hours now and can't seem to find what is wrong with this UPDATE statement, hopefully a fresh pair of eyes will...

 

Connects fine. echo's the statement fine. ran the sql statement in MySQL admin and works fine. Just won't run through my PHP code...

 

$con = mysql_connect("localhost","****","*****");
							       mysql_select_db("***", $con);

							if (!$con)  {
								die('Could not connect: ' . mysql_error());
								 }

							$cnt = count($keys);
							for ($i=0;$i<$cnt;$i++) {

								$sql .= "UPDATE save SET " . $keys[$i] . " = '" . $astring2[$i] . "' WHERE username = '".$username1 ."';<br />";
								}
								echo $sql;
								echo "<br />";										

							if (!mysql_query($sql,$con)){
								die('Error: ' . mysql_error());
									}
								mysql_close($con);

 

Output:

UPDATE save SET DateName = '01_29_014659' WHERE username = 'TylerA'; //not going to list the whole echo'd loop, but this is just one SQL statement output so you get the idea.

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '

UPDATE save SET DateName = '01_29_014659' WHERE username = 'TylerA';

' at line 1

... no. you should be writing each query out, on its own, without any HTML formatting or semicolons in the string, and running it as an individual query. right now you're trying to feed it a string that looks like this:

 

QUERY HERE;<br />QUERY HERE;<br />QUERY HERE;<br />

 

you need to be giving it JUST the:

 

QUERY HERE

 

portion.

 

EDIT: beaten to the punch, but this has a bit more info.

Thanks. I moved the query so that it would iterate through and not just the statement string. Works fine now.

 

for ($i=0;$i<$cnt;$i++) {

							$con = mysql_connect("localhost","***","***");
							       mysql_select_db("compliance", $con);

							if (!$con)  {
								die('Could not connect: ' . mysql_error());
								 }

							if (!mysql_query("UPDATE save SET " . $keys[$i] . " = '" . $astring2[$i] . "' WHERE username = '".$username1 ."';"))
							{
								die('Error: ' . mysql_error());
									}
								mysql_close($con);
								}

 

It seems as though there would be a more efficient way to do this? Either way though, it's solved... Thanks again.

Archived

This topic is now archived and is closed to further replies.

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