Muddy_Funster Posted April 25, 2012 Share Posted April 25, 2012 Blep, could you change the code Adam gave you to var_dump(mysql_info()); and let us know what you get then? Quote Link to comment https://forums.phpfreaks.com/topic/261481-problem-with-storing-info-associated-id/page/3/#findComment-1340435 Share on other sites More sharing options...
PFMaBiSmAd Posted April 25, 2012 Share Posted April 25, 2012 I think it would be best (and quickest) if you posted everything from where your UPDATE query is being formed through to the end of the else {echo "nothing affected";} logic. I suspect you have more than one database connection and you are executing a query using one connection and testing the mysql_affected_rows() on a different connection. And why not simply refer to the query as $query? There's no need to specifically use variable names like $insert_query, especially if you are not going to rename them to what the query actually is to avoid confusion when you don't post all the relevant code at once. Quote Link to comment https://forums.phpfreaks.com/topic/261481-problem-with-storing-info-associated-id/page/3/#findComment-1340436 Share on other sites More sharing options...
blepblep Posted April 25, 2012 Author Share Posted April 25, 2012 Done Muddy, this is returned: bool(false) nothing affected @PFMaBiSmAd - <?php ///------------------------ /// Connecting to database ///------------------------ include 'connect_db.php'; ///----------------------------- /// Inserting data into database ///----------------------------- function sanitize_data($data) // create the function sanitize_data { $data = mysql_real_escape_string(trim($data)); return $data; } $post = array(); foreach($_POST as $key =>$value){ $post[$key] = sanitize_data($value); // call sanitize_data using each value from the $post array } $id = $post['review_id']; //var_dump($id); if(isset($post['save'])) { $insert_query = "UPDATE tc_tool.forms SET quality_ranking_review = '{$post['quality_ranking_review']}', input_doc_rank = '{$post['input_doc_rank']}', correct_doc = '{$post['correct_doc']}', internally_reviewed = '{$post['internally_reviewed']}', reqs_covered = '{$post['reqs_covered']}', correct_storage_library = '{$post['correct_storage_library']}', reports_described = '{$post['reports_described']}', approved_change_requests_included = '{$post['approved_change_requests_included']}', issues_addressed = '{$post['issues_addressed']}', doc_available = '{$post['doc_available']}', statement_problem_chapter = '{$post['statement_problem_chapter']}', major_comments = '{$post['major_comments']}', result = '{$post['result']}', num_of_major_comments = '{$post['num_of_major_comments']}', minor_comments = '{$post['minor_comments']}', next_review_forum = '{$post['minor_comments']}', date = '{$post['date']}', time = '{$post['time']}', venue = '{$post['venue']}', time2 = '{$post['time2']}', location = '{$post['location']}', severity = '{$post['severity']}', resp = '{$post['resp']}', status = '{$post['status']}', comment = '{$post['comment']}', remark = '{$post['remark']}', miscellaneous = '{$post['miscellaneous']}' WHERE (review_id = '{$post['review_id']}')"; //var_dump($post['review_id']); var_dump(mysql_info()); if (mysql_affected_rows() === 1) { echo 'Row was updated.'; } else { echo "nothing affected"; } //----------------------------- // Prints error if there is one //----------------------------- if (!mysql_query($insert_query, $connection)) { echo "Query failed: $query<br />" . mysql_error(); } function died($error) { // Error code here echo "Error, not saving successfully. "; echo "The errors are below.<br /><br />"; echo $error."<br /><br />"; echo "Please retry.<br /><br />"; var_dump($_POST); die(); } mysql_close($connection); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/261481-problem-with-storing-info-associated-id/page/3/#findComment-1340437 Share on other sites More sharing options...
Adam Posted April 25, 2012 Share Posted April 25, 2012 You're trying to get the number of affected rows before you execute the query... Quote Link to comment https://forums.phpfreaks.com/topic/261481-problem-with-storing-info-associated-id/page/3/#findComment-1340444 Share on other sites More sharing options...
Muddy_Funster Posted April 25, 2012 Share Posted April 25, 2012 a sample from the code you posted : WHERE (review_id = '{$post['review_id']}')"; //var_dump($post['review_id']); var_dump(mysql_info()); if (mysql_affected_rows() === 1) { echo 'Row was updated.'; } else { echo "nothing affected"; } //----------------------------- // Prints error if there is one //----------------------------- if (!mysql_query($insert_query, $connection)) { echo "Query failed: $query<br />" . mysql_error(); } As, Adam said : whats happening is that you are checking the var_dump and the mysql_affected_rows BEFORE your actualy running the query with if(!mysql_query($insert_query, $connection)). So at the point where the check is performaed it's telling the truth, as there was no query performed, it's then going on and updating the database as it should. You need to swap the possition of some things about. Quote Link to comment https://forums.phpfreaks.com/topic/261481-problem-with-storing-info-associated-id/page/3/#findComment-1340445 Share on other sites More sharing options...
blepblep Posted April 25, 2012 Author Share Posted April 25, 2012 @Adam, how does this look now? ........ WHERE (review_id = '{$post['review_id']}')"; //var_dump($post['review_id']); //----------------------------- // Prints error if there is one //----------------------------- if (!mysql_query($insert_query, $connection)) { echo "Query failed: $query<br />" . mysql_error(); } if (mysql_affected_rows() === 1) { echo "Row was updated." . "<br />"; } else { echo "nothing affected" . "<br />"; } var_dump(mysql_info()); function died($error) { // Error code here echo "Error, not saving successfully. "; echo "The errors are below.<br /><br />"; echo $error."<br /><br />"; echo "Please retry.<br /><br />"; //var_dump($_POST); die(); } When I run it how it is above this is my output: nothing affected string(40) "Rows matched: 1 Changed: 0 Warnings: 6" The 'Changed' in the above changes if I enter new data, working so I presume? Quote Link to comment https://forums.phpfreaks.com/topic/261481-problem-with-storing-info-associated-id/page/3/#findComment-1340457 Share on other sites More sharing options...
blepblep Posted April 25, 2012 Author Share Posted April 25, 2012 Sorry the above should read: Row was updated. string(40) "Rows matched: 1 Changed: 1 Warnings: 5" I think it's working.. Quote Link to comment https://forums.phpfreaks.com/topic/261481-problem-with-storing-info-associated-id/page/3/#findComment-1340464 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.