RON_ron Posted September 24, 2012 Share Posted September 24, 2012 Could someone tell what's wrong with the below code? it's not updating the database? $result = sprintf("UPDATE student_db1 SET completed='Yes', name='".$sname."' WHERE id ='%s'", mysql_real_escape_string($sid)); $results = mysql_query($result); Quote Link to comment https://forums.phpfreaks.com/topic/268735-update_not_working/ Share on other sites More sharing options...
kicken Posted September 24, 2012 Share Posted September 24, 2012 Echo your query out with var_dump($result) before you run it to make sure it is valid. Try running it manually in something like PHPMyAdmin or mysql console and check for errors. Quote Link to comment https://forums.phpfreaks.com/topic/268735-update_not_working/#findComment-1380540 Share on other sites More sharing options...
RON_ron Posted September 24, 2012 Author Share Posted September 24, 2012 it's strange.... echo works.... is there anything wrong in that piece of code? Quote Link to comment https://forums.phpfreaks.com/topic/268735-update_not_working/#findComment-1380600 Share on other sites More sharing options...
Jessica Posted September 24, 2012 Share Posted September 24, 2012 When you ran it in mysql or phpmyadmin it worked? See the link in my signature. Quote Link to comment https://forums.phpfreaks.com/topic/268735-update_not_working/#findComment-1380605 Share on other sites More sharing options...
White_Lily Posted September 25, 2012 Share Posted September 25, 2012 (edited) Hmm... you've got "%s" in your code where i believe it updates the 'id'? Is it supposed to be like that? Edited September 25, 2012 by White_Lily Quote Link to comment https://forums.phpfreaks.com/topic/268735-update_not_working/#findComment-1380795 Share on other sites More sharing options...
Jessica Posted September 25, 2012 Share Posted September 25, 2012 White_lily look up sprintf. Quote Link to comment https://forums.phpfreaks.com/topic/268735-update_not_working/#findComment-1380803 Share on other sites More sharing options...
Barand Posted September 25, 2012 Share Posted September 25, 2012 (edited) White_Lily: see manual http://uk3.php.net/m...ion.sprintf.php Sorry, these warnings about previous posts don't work too well :-\ Edited September 25, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/268735-update_not_working/#findComment-1380805 Share on other sites More sharing options...
White_Lily Posted September 25, 2012 Share Posted September 25, 2012 Hmm, ive never had to use that o.o Quote Link to comment https://forums.phpfreaks.com/topic/268735-update_not_working/#findComment-1380828 Share on other sites More sharing options...
Jessica Posted September 25, 2012 Share Posted September 25, 2012 There's not much reason to in this example, OP could be using PDO for a better way to do the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/268735-update_not_working/#findComment-1380830 Share on other sites More sharing options...
PFMaBiSmAd Posted September 25, 2012 Share Posted September 25, 2012 I'm going to guess that your $sname variable contains some sql special characters and since you don't appear to be escaping that variable before putting it into the query statement, you are getting an sql syntax error. Why are you building a query statement using two different methods of putting values into it, one for $sname and a different one for $sid? Consistency does count in programming. There's around a half-dozen three-quarters of a dozen of different things that could cause your query to not update the database. If everything is perfect up to the point of the code you did post, that query should work. Here's a list of the things that could prevent your update query from working - You don't have a database connection at the point where you are running the query. You haven't selected any database or have selected the wrong one. The php code where the query is at is not being executed. The query is failing with an error due to a wrong table or column name. The query is failing due to some sql special characters in the $sname variable. The WHERE clause is false because $sid is empty or doesn't match any id in your table. The wrong column is being used in the WHERE clause. Is the column you are trying to match id? or is it something else? The values you are updating the columns to are not compatible with the column type. The value you are updating the columns to are the same as what they already are. any more that I forgot to think of... It's up to you to troubleshoot what your code and your data are doing on your server to pin down why something does not work. You need to devise tests to check each of these possible reasons to either eliminate it as the cause of the problem or to find that it is the cause of the problem. Quote Link to comment https://forums.phpfreaks.com/topic/268735-update_not_working/#findComment-1380834 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.