osyrys14 Posted July 14, 2011 Share Posted July 14, 2011 Simple statement, but it's not functioning... Just a simple update query, only I get NO error, and NO update... Here's the code... mysql_query("UPDATE contacts SET fname = '$_POST[formName]' WHERE ID = '$rec'"); $rec used to be $_POST[formid] which is the ID of the entry in the database... The variables passed have been verified as accurate, but still no error, and no update... My eyes hurt from staring at this for this long, any sets of eyes see something I don't? (p.s., I'm not a newbie in this by any means, mostly why this is confusing me so much... ) Thanks in advance for any help! Osyrys Quote Link to comment https://forums.phpfreaks.com/topic/241940-mysql-simple-statement-malfunction/ Share on other sites More sharing options...
AyKay47 Posted July 14, 2011 Share Posted July 14, 2011 I assume that you have prooperly debugged your query before posting..? try this $form_name = $_POST['formName']; if(!empty($_POST['formName'])){ $form_name = mysql_real_escape_string($form_name); $query = mysql_query("UPDATE contacts SET fname = '$form_name' WHERE ID = $rec"); if(!$query){ exit(mysql_error()); }else{ print "query was a success"; } } If this does not work for you, I will need to see more of your code Quote Link to comment https://forums.phpfreaks.com/topic/241940-mysql-simple-statement-malfunction/#findComment-1242526 Share on other sites More sharing options...
jcbones Posted July 14, 2011 Share Posted July 14, 2011 p.s., I'm not a newbie in this by any means, mostly why this is confusing me so much... Things can be overlooked by even the best of programmers. I would suggest standard de-bugging protocol. error_reporting(E_ALL); ini_set('display_errors','On'); mysql_query("UPDATE contacts SET fname='" . mysql_real_escape_string($_POST['formName']) . "' WHERE ID = '" . intval($rec) . "'") or trigger_error( mysql_error() ); Quote Link to comment https://forums.phpfreaks.com/topic/241940-mysql-simple-statement-malfunction/#findComment-1242527 Share on other sites More sharing options...
osyrys14 Posted July 14, 2011 Author Share Posted July 14, 2011 The only thing that shows up different is this message.... Notice: Use of undefined constant formid - assumed 'formid' in /home/acinquemani/public_html/forms/updatecontact.php on line 24 And line 24 is just echoing the variable to make sure that is flowing to the page.... No other messages or errors. Osyrys Quote Link to comment https://forums.phpfreaks.com/topic/241940-mysql-simple-statement-malfunction/#findComment-1242532 Share on other sites More sharing options...
AyKay47 Posted July 14, 2011 Share Posted July 14, 2011 you did not wrap the index in quotes, so the parser assumes this as a constant, which it is not, try my code Quote Link to comment https://forums.phpfreaks.com/topic/241940-mysql-simple-statement-malfunction/#findComment-1242533 Share on other sites More sharing options...
Psycho Posted July 14, 2011 Share Posted July 14, 2011 Whenever a query does not do as you expect - echo it to the page to validate it contains what you think it does. I would advise NEVER actually building your query inside the mysql_query() function. Instead build it as a string variable so you can see what it contains. You say the variables have been verified, but since you don't show the code of how you verified them I can't take your word for it. Also, although an array variable such as "$_POST[formName]" will work; it is wrong. That is not my opinion, that is stated in the PHP manual several times. Always enclose string keys in quotes. Building upon jcbones's code and adding more error handling and debugging code. This will show if there are any errors and what the results of the query are. If this still doesn't show any affected rows, then there is no record matching the ID given. Make sure you are running against the right database. it wouldn't be the first time someone updated a page in one environment and was testing against a different one. error_reporting(E_ALL); ini_set('display_errors','On'); //Escape/prepare values for query $fnameValue = mysql_real_escape_string(trim($_POST['formName'])); $id =intval($rec); //Create query $query = "UPDATE contacts SET fname='{$fnameValue}' WHERE ID={$id}"; //Debugging line: echo query echo "Query : $query<br>\n";; //Run query mysql_query($query) or die(mysql_error()); //Debugging line: echo line affected echo "Records updated: " . mysql_affected_rows(); you did not wrap the index in quotes, so the parser assumes this as a constant, which it is not, try my code Assuming there is an array index of "formid" that would not be the problem. It is wrong - but it will work, which is why it is a "Notice" and not an error. Quote Link to comment https://forums.phpfreaks.com/topic/241940-mysql-simple-statement-malfunction/#findComment-1242539 Share on other sites More sharing options...
jcbones Posted July 14, 2011 Share Posted July 14, 2011 If I wasn't so lazy today, I would have broken the query out of the function. But, ya caught me... Quote Link to comment https://forums.phpfreaks.com/topic/241940-mysql-simple-statement-malfunction/#findComment-1242542 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.