droidus Posted July 17, 2011 Share Posted July 17, 2011 how would i modify this code so that it works, using sprintf?: if (mysql_num_rows($result) > 0) { mysql_query(sprintf("UPDATE members SET (Age) VALUES ('%s')",mysql_real_escape_string($age))) or die(mysql_error()); mysql_close($con); } Quote Link to comment https://forums.phpfreaks.com/topic/242232-update-database-using-sprintf/ Share on other sites More sharing options...
Alex Posted July 17, 2011 Share Posted July 17, 2011 Your problem isn't with using sprintf, it's the query itself. That's not how you do a MySQL update query. Quote Link to comment https://forums.phpfreaks.com/topic/242232-update-database-using-sprintf/#findComment-1243932 Share on other sites More sharing options...
dcro2 Posted July 19, 2011 Share Posted July 19, 2011 I think you mean: mysql_query(sprintf("UPDATE members SET Age = '%s'", mysql_real_escape_string($age))) You might want to add a WHERE clause to that though, or you'll update all the rows in that table. For example: UPDATE members SET age = '12' WHERE username = 'droidus' Quote Link to comment https://forums.phpfreaks.com/topic/242232-update-database-using-sprintf/#findComment-1244417 Share on other sites More sharing options...
droidus Posted July 19, 2011 Author Share Posted July 19, 2011 thanks! lol, i was looking at the sprintf, and the update clause, but wasn't exactly sure how to put 1+1 together Quote Link to comment https://forums.phpfreaks.com/topic/242232-update-database-using-sprintf/#findComment-1244556 Share on other sites More sharing options...
droidus Posted July 21, 2011 Author Share Posted July 21, 2011 I think you mean: mysql_query(sprintf("UPDATE members SET Age = '%s'", mysql_real_escape_string($age))) You might want to add a WHERE clause to that though, or you'll update all the rows in that table. For example: UPDATE members SET age = '12' WHERE username = 'droidus' so let me make sure that i have this right: mysql_query(sprintf("UPDATE members SET Age = '%s' WHERE uname = $_SESSION[user]", mysql_real_escape_string($age))) Quote Link to comment https://forums.phpfreaks.com/topic/242232-update-database-using-sprintf/#findComment-1245464 Share on other sites More sharing options...
dcro2 Posted July 21, 2011 Share Posted July 21, 2011 You'll need single quotes around that string you're comparing `uname` against: mysql_query(sprintf("UPDATE members SET Age = '%s' WHERE uname = '$_SESSION[user]'", mysql_real_escape_string($age))) Quote Link to comment https://forums.phpfreaks.com/topic/242232-update-database-using-sprintf/#findComment-1245597 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.