Ty44ler Posted January 29, 2010 Share Posted January 29, 2010 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 Link to comment https://forums.phpfreaks.com/topic/190270-spot-the-error-mysql-update-statement-in-php/ Share on other sites More sharing options...
akitchin Posted January 29, 2010 Share Posted January 29, 2010 you're trying to run a query that contains HTML formatting (those <br /> tags). as well, mysql_query only takes one query, so you can't pass it a string with several queries separated by semicolons. Link to comment https://forums.phpfreaks.com/topic/190270-spot-the-error-mysql-update-statement-in-php/#findComment-1003839 Share on other sites More sharing options...
Ty44ler Posted January 29, 2010 Author Share Posted January 29, 2010 so should I be using mysql_fetch_assoc(); instead of mysql_query()? Link to comment https://forums.phpfreaks.com/topic/190270-spot-the-error-mysql-update-statement-in-php/#findComment-1003845 Share on other sites More sharing options...
Mchl Posted January 29, 2010 Share Posted January 29, 2010 so should I be using mysql_fetch_assoc(); instead of mysql_query()? No. You should run one query only in one mysql_query(). Link to comment https://forums.phpfreaks.com/topic/190270-spot-the-error-mysql-update-statement-in-php/#findComment-1003847 Share on other sites More sharing options...
akitchin Posted January 29, 2010 Share Posted January 29, 2010 ... 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. Link to comment https://forums.phpfreaks.com/topic/190270-spot-the-error-mysql-update-statement-in-php/#findComment-1003848 Share on other sites More sharing options...
Ty44ler Posted January 29, 2010 Author Share Posted January 29, 2010 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. Link to comment https://forums.phpfreaks.com/topic/190270-spot-the-error-mysql-update-statement-in-php/#findComment-1003861 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.