jeff5656 Posted November 19, 2008 Share Posted November 19, 2008 I am trying to update a record. Although I can echo the correct values, the database does not get updated. I sense that the problem is with the $query command. Also, where in this code do I put a WHERE statment to limit the record to a certain one? <?php include "../connectdb.php"; $query = "UPDATE staffsched SET "; $set = array(); foreach($_POST as $field => $value){ $field = mysql_real_escape_string($field); $value = mysql_real_escape_string($value); $set[] = "'{$field}' = '{$value}'"; echo "field: " . $field . "<br>"; echo "value: " . $value . "<br>"; echo "set: " . $set . "<br>"; } $query .= implode(", ",$set); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133395-database-not-getting-updated/ Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 A. You are not calling mysql_query to run the update query. B. You would put a where statement after the last update. So basically here: $query .= implode(", ",$set) . " WHERE blah=blah"; Quote Link to comment https://forums.phpfreaks.com/topic/133395-database-not-getting-updated/#findComment-693776 Share on other sites More sharing options...
jeff5656 Posted November 19, 2008 Author Share Posted November 19, 2008 where do I put the mysql_query statement in here? And also, shouldn't it be $query .= "implode(', ',$set)" . " WHERE blah='blah' "; Also, my code has two $query= lines. One is the UPDATE one, and the other is as above. How can this be? Doesn't that overwrite the first statement? Sorry I am a beginner but am trying to learn Quote Link to comment https://forums.phpfreaks.com/topic/133395-database-not-getting-updated/#findComment-693784 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 No my version is correct, yours would take implode literally and write it out. $query .= implode(", ",$set) . " WHERE blah='blah'"; mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/133395-database-not-getting-updated/#findComment-693790 Share on other sites More sharing options...
jeff5656 Posted November 19, 2008 Author Share Posted November 19, 2008 Ok I put in the mysql query line, but when I submit the form, the database fields are all still blank! To reiterate, here's what I added $query .= implode(", ",$set) . "WHERE current_status = 'c'"; mysql_query($query) or die(mysql_error()); Now the echo commands echo all the variables that I entered in the form, but they just do not get into the database. Quote Link to comment https://forums.phpfreaks.com/topic/133395-database-not-getting-updated/#findComment-693797 Share on other sites More sharing options...
revraz Posted November 19, 2008 Share Posted November 19, 2008 echo $query and post it. Quote Link to comment https://forums.phpfreaks.com/topic/133395-database-not-getting-updated/#findComment-693799 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 Echo out the $query and see what that is displaying. Is the script dieing with the mysql error? Are you initiaiting the mysql connection first with mysql_connection then selecting the database with mysql_select_database before you run mysql_query? I would echo out this line: $query .= implode(", ",$set) . " WHERE current_status = 'c'"; echo "Query is: " . $query . "<br />; mysql_query($query) or die(mysql_error()); Other than that I have no clue. Without it throwing any error messages or error messages being provided. Quote Link to comment https://forums.phpfreaks.com/topic/133395-database-not-getting-updated/#findComment-693801 Share on other sites More sharing options...
jeff5656 Posted November 19, 2008 Author Share Posted November 19, 2008 Ok I get an error. My echo commands were echoing 270 lines so I didn't think to scroll to end. Here is the 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 ''which_date1' = '1', 'icufellow1' = 'jonny', 'icustaff1' = 'eferferfer', 'f2staf' at line 1 Here is the echoed $query (warning it's long): UPDATE staffsched SET 'which_date1' = '1', 'icufellow1' = 'jonny', 'icustaff1' = 'eferferfer', 'f2staff1' = 'hybyhb', 'intervent1' = 'yhbhybyh', 'wb1' = 'hybhyb', 'tx1' = 'hybhyb', 'phtn1' = 'hybhyb', 'warren1' = 'hybyhb', 'which_date2' = '33', 'icufellow2' = 'hbhby', 'icustaff2' = 'bybyhbhy', 'f2staff2' = 'yhbhyb', 'intervent2' = '', 'wb2' = '', 'tx2' = '', 'phtn2' = '', 'warren2' = '', 'which_date3' = '3', 'icufellow3' = 'hihkjhk', 'icustaff3' = '', 'f2staff3' = 'byhb', 'intervent3' = '', 'wb3' = '', 'tx3' = '', 'phtn3' = '', 'warren3' = '', 'which_date4' = '4', 'icufellow4' = 'yhbyhb', 'icustaff4' = '', 'f2staff4' = '', 'intervent4' = '', 'wb4' = '', 'tx4' = '', 'phtn4' = '', 'warren4' = '', 'which_date5' = '5', 'icufellow5' = 'hbyh', 'icustaff5' = '', 'f2staff5' = '', 'intervent5' = '', 'wb5' = '', 'tx5' = '', 'phtn5' = '', 'warren5' = '', 'which_date6' = '6', 'icufellow6' = '', 'icustaff6' = '', 'f2staff6' = '', 'intervent6' = '', 'wb6' = '', 'tx6' = '', 'phtn6' = '', 'warren6' = '', 'which_date7' = '7', 'icufellow7' = '', 'icustaff7' = '', 'f2staff7' = '', 'intervent7' = '', 'wb7' = '', 'tx7' = '', 'phtn7' = '', 'warren7' = '', 'which_date8' = '8', 'icufellow8' = '', 'icustaff8' = '', 'f2staff8' = '', 'intervent8' = '', 'wb8' = '', 'tx8' = '', 'phtn8' = '', 'warren8' = '', 'which_date9' = '9', 'icufellow9' = '', 'icustaff9' = '', 'f2staff9' = '', 'intervent9' = '', 'wb9' = '', 'tx9' = '', 'phtn9' = '', 'warren9' = '', 'which_date10' = '10', 'icufellow10' = '', 'icustaff10' = '', 'f2staff10' = '', 'intervent10' = '', 'wb10' = '', 'tx10' = '', 'phtn10' = '', 'warren10' = '', 'which_date11' = '11', 'icufellow11' = '', 'icustaff11' = '', 'f2staff11' = '', 'intervent11' = '', 'wb11' = '', 'tx11' = '', 'phtn11' = '', 'warren11' = '', 'which_date12' = '12', 'icufellow12' = '', 'icustaff12' = '', 'f2staff12' = '', 'intervent12' = '', 'wb12' = '', 'tx12' = '', 'phtn12' = '', 'warren12' = '', 'which_date13' = '13', 'icufellow13' = '', 'icustaff13' = '', 'f2staff13' = '', 'intervent13' = '', 'wb13' = '', 'tx13' = '', 'phtn13' = '', 'warren13' = '', 'which_date14' = '14', 'icufellow14' = '', 'icustaff14' = '', 'f2staff14' = '', 'intervent14' = '', 'wb14' = '', 'tx14' = '', 'phtn14' = '', 'warren14' = '', 'which_date16' = '16', 'icufellow16' = '', 'icustaff16' = '', 'f2staff16' = '', 'intervent16' = '', 'wb16' = '', 'tx16' = '', 'phtn16' = '', 'warren16' = '', 'which_date17' = '17', 'icufellow17' = '', 'icustaff17' = '', 'f2staff17' = '', 'intervent17' = '', 'wb17' = '', 'tx17' = '', 'phtn17' = '', 'warren17' = '', 'which_date18' = '18', 'icufellow18' = '', 'icustaff18' = '', 'f2staff18' = '', 'intervent18' = '', 'wb18' = '', 'tx18' = '', 'phtn18' = '', 'warren18' = '', 'which_date19' = '19', 'icufellow19' = '', 'icustaff19' = '', 'f2staff19' = '', 'intervent19' = '', 'wb19' = '', 'tx19' = '', 'phtn19' = '', 'warren19' = '', 'which_date20' = '20', 'icufellow20' = '', 'icustaff20' = '', 'f2staff20' = '', 'intervent20' = '', 'wb20' = '', 'tx20' = '', 'phtn20' = '', 'warren20' = '', 'which_date21' = '21', 'icufellow21' = '', 'icustaff21' = '', 'f2staff21' = '', 'intervent21' = '', 'wb21' = '', 'tx21' = '', 'phtn21' = '', 'warren21' = '', 'which_date22' = '22', 'icufellow22' = '', 'icustaff22' = '', 'f2staff22' = '', 'intervent22' = '', 'wb22' = '', 'tx22' = '', 'phtn22' = '', 'warren22' = '', 'which_date23' = '23', 'icufellow23' = '', 'icustaff23' = '', 'f2staff23' = '', 'intervent23' = '', 'wb23' = '', 'tx23' = '', 'phtn23' = '', 'warren23' = '', 'which_date24' = '24', 'icufellow24' = '', 'icustaff24' = '', 'f2staff24' = '', 'intervent24' = '', 'wb24' = '', 'tx24' = '', 'phtn24' = '', 'warren24' = '', 'which_date25' = '25', 'icufellow25' = '', 'icustaff25' = '', 'f2staff25' = '', 'intervent25' = '', 'wb25' = '', 'tx25' = '', 'phtn25' = '', 'warren25' = '', 'which_date26' = '26', 'icufellow26' = '', 'icustaff26' = '', 'f2staff26' = '', 'intervent26' = '', 'wb26' = '', 'tx26' = '', 'phtn26' = '', 'warren26' = '', 'which_date27' = '27', 'icufellow27' = '', 'icustaff27' = '', 'f2staff27' = '', 'intervent27' = '', 'wb27' = '', 'tx27' = '', 'phtn27' = '', 'warren27' = '', 'which_date28' = '28', 'icufellow28' = '', 'icustaff28' = '', 'f2staff28' = '', 'intervent28' = '', 'wb28' = '', 'tx28' = '', 'phtn28' = '', 'warren28' = '', 'which_date29' = '29', 'icufellow29' = '', 'icustaff29' = '', 'f2staff29' = '', 'intervent29' = '', 'wb29' = '', 'tx29' = '', 'phtn29' = '', 'warren29' = '', 'which_date30' = '30', 'icufellow30' = '', 'icustaff30' = '', 'f2staff30' = '', 'intervent30' = '', 'wb30' = '', 'tx30' = '', 'phtn30' = '', 'warren30' = '', 'which_date31' = '31', 'icufellow31' = '', 'icustaff31' = '', 'f2staff31' = '', 'intervent31' = '', 'wb31' = '', 'tx31' = '', 'phtn31' = '', 'warren31' = ''WHERE current_status = 'c': UPDATE staffsched SET 'which_date1' = '1', 'icufellow1' = 'jonny', 'icustaff1' = 'eferferfer', 'f2staff1' = 'hybyhb', 'intervent1' = 'yhbhybyh', 'wb1' = 'hybhyb', 'tx1' = 'hybhyb', 'phtn1' = 'hybhyb', 'warren1' = 'hybyhb', 'which_date2' = '33', 'icufellow2' = 'hbhby', 'icustaff2' = 'bybyhbhy', 'f2staff2' = 'yhbhyb', 'intervent2' = '', 'wb2' = '', 'tx2' = '', 'phtn2' = '', 'warren2' = '', 'which_date3' = '3', 'icufellow3' = 'hihkjhk', 'icustaff3' = '', 'f2staff3' = 'byhb', 'intervent3' = '', 'wb3' = '', 'tx3' = '', 'phtn3' = '', 'warren3' = '', 'which_date4' = '4', 'icufellow4' = 'yhbyhb', 'icustaff4' = '', 'f2staff4' = '', 'intervent4' = '', 'wb4' = '', 'tx4' = '', 'phtn4' = '', 'warren4' = '', 'which_date5' = '5', 'icufellow5' = 'hbyh', 'icustaff5' = '', 'f2staff5' = '', 'intervent5' = '', 'wb5' = '', 'tx5' = '', 'phtn5' = '', 'warren5' = '', 'which_date6' = '6', 'icufellow6' = '', 'icustaff6' = '', 'f2staff6' = '', 'intervent6' = '', 'wb6' = '', 'tx6' = '', 'phtn6' = '', 'warren6' = '', 'which_date7' = '7', 'icufellow7' = '', 'icustaff7' = '', 'f2staff7' = '', 'intervent7' = '', 'wb7' = '', 'tx7' = '', 'phtn7' = '', 'warren7' = '', 'which_date8' = '8', 'icufellow8' = '', 'icustaff8' = '', 'f2staff8' = '', 'intervent8' = '', 'wb8' = '', 'tx8' = '', 'phtn8' = '', 'warren8' = '', 'which_date9' = '9', 'icufellow9' = '', 'icustaff9' = '', 'f2staff9' = '', 'intervent9' = '', 'wb9' = '', 'tx9' = '', 'phtn9' = '', 'warren9' = '', 'which_date10' = '10', 'icufellow10' = '', 'icustaff10' = '', 'f2staff10' = '', 'intervent10' = '', 'wb10' = '', 'tx10' = '', 'phtn10' = '', 'warren10' = '', 'which_date11' = '11', 'icufellow11' = '', 'icustaff11' = '', 'f2staff11' = '', 'intervent11' = '', 'wb11' = '', 'tx11' = '', 'phtn11' = '', 'warren11' = '', 'which_date12' = '12', 'icufellow12' = '', 'icustaff12' = '', 'f2staff12' = '', 'intervent12' = '', 'wb12' = '', 'tx12' = '', 'phtn12' = '', 'warren12' = '', 'which_date13' = '13', 'icufellow13' = '', 'icustaff13' = '', 'f2staff13' = '', 'intervent13' = '', 'wb13' = '', 'tx13' = '', 'phtn13' = '', 'warren13' = '', 'which_date14' = '14', 'icufellow14' = '', 'icustaff14' = '', 'f2staff14' = '', 'intervent14' = '', 'wb14' = '', 'tx14' = '', 'phtn14' = '', 'warren14' = '', 'which_date16' = '16', 'icufellow16' = '', 'icustaff16' = '', 'f2staff16' = '', 'intervent16' = '', 'wb16' = '', 'tx16' = '', 'phtn16' = '', 'warren16' = '', 'which_date17' = '17', 'icufellow17' = '', 'icustaff17' = '', 'f2staff17' = '', 'intervent17' = '', 'wb17' = '', 'tx17' = '', 'phtn17' = '', 'warren17' = '', 'which_date18' = '18', 'icufellow18' = '', 'icustaff18' = '', 'f2staff18' = '', 'intervent18' = '', 'wb18' = '', 'tx18' = '', 'phtn18' = '', 'warren18' = '', 'which_date19' = '19', 'icufellow19' = '', 'icustaff19' = '', 'f2staff19' = '', 'intervent19' = '', 'wb19' = '', 'tx19' = '', 'phtn19' = '', 'warren19' = '', 'which_date20' = '20', 'icufellow20' = '', 'icustaff20' = '', 'f2staff20' = '', 'intervent20' = '', 'wb20' = '', 'tx20' = '', 'phtn20' = '', 'warren20' = '', 'which_date21' = '21', 'icufellow21' = '', 'icustaff21' = '', 'f2staff21' = '', 'intervent21' = '', 'wb21' = '', 'tx21' = '', 'phtn21' = '', 'warren21' = '', 'which_date22' = '22', 'icufellow22' = '', 'icustaff22' = '', 'f2staff22' = '', 'intervent22' = '', 'wb22' = '', 'tx22' = '', 'phtn22' = '', 'warren22' = '', 'which_date23' = '23', 'icufellow23' = '', 'icustaff23' = '', 'f2staff23' = '', 'intervent23' = '', 'wb23' = '', 'tx23' = '', 'phtn23' = '', 'warren23' = '', 'which_date24' = '24', 'icufellow24' = '', 'icustaff24' = '', 'f2staff24' = '', 'intervent24' = '', 'wb24' = '', 'tx24' = '', 'phtn24' = '', 'warren24' = '', 'which_date25' = '25', 'icufellow25' = '', 'icustaff25' = '', 'f2staff25' = '', 'intervent25' = '', 'wb25' = '', 'tx25' = '', 'phtn25' = '', 'warren25' = '', 'which_date26' = '26', 'icufellow26' = '', 'icustaff26' = '', 'f2staff26' = '', 'intervent26' = '', 'wb26' = '', 'tx26' = '', 'phtn26' = '', 'warren26' = '', 'which_date27' = '27', 'icufellow27' = '', 'icustaff27' = '', 'f2staff27' = '', 'intervent27' = '', 'wb27' = '', 'tx27' = '', 'phtn27' = '', 'warren27' = '', 'which_date28' = '28', 'icufellow28' = '', 'icustaff28' = '', 'f2staff28' = '', 'intervent28' = '', 'wb28' = '', 'tx28' = '', 'phtn28' = '', 'warren28' = '', 'which_date29' = '29', 'icufellow29' = '', 'icustaff29' = '', 'f2staff29' = '', 'intervent29' = '', 'wb29' = '', 'tx29' = '', 'phtn29' = '', 'warren29' = '', 'which_date30' = '30', 'icufellow30' = '', 'icustaff30' = '', 'f2staff30' = '', 'intervent30' = '', 'wb30' = '', 'tx30' = '', 'phtn30' = '', 'warren30' = '', 'which_date31' = '31', 'icufellow31' = '', 'icustaff31' = '', 'f2staff31' = '', 'intervent31' = '', 'wb31' = '', 'tx31' = '', 'phtn31' = '', 'warren31' = ''WHERE current_status = 'c'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 ''which_date1' = '1', 'icufellow1' = 'jonny', 'icustaff1' = 'eferferfer', 'f2staf' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/133395-database-not-getting-updated/#findComment-693804 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 <?php include "../connectdb.php"; $query = "UPDATE staffsched SET "; $set = array(); foreach($_POST as $field => $value){ $field = mysql_real_escape_string($field); $value = mysql_real_escape_string($value); $set[] = "`{$field}` = '{$value}'"; echo "field: " . $field . "<br>"; echo "value: " . $value . "<br>"; echo "set: " . $set . "<br>"; } ?> Change the ' (single quotes) in the field name to backticks (`) for correct usage. That should correct it. Quote Link to comment https://forums.phpfreaks.com/topic/133395-database-not-getting-updated/#findComment-693806 Share on other sites More sharing options...
jeff5656 Posted November 19, 2008 Author Share Posted November 19, 2008 Change the ' (single quotes) in the field name to backticks (`) for correct usage. That should correct it. It works!!!! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/133395-database-not-getting-updated/#findComment-693807 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.