genzedu777 Posted May 23, 2010 Share Posted May 23, 2010 if (isset($_POST['submit'])) { // Write the questionnaire response rows to the response table foreach ($_POST as $response_id => $response) { $query = "UPDATE mismatch_response SET response = '$response' WHERE response_id = '$response_id'"; mysqli_query($dbc, $query); } echo '<p>Your responses have been saved.</p>'; } Can someone explain this current code? foreach ($_POST as $response_id => $response) { My understanding for 'foreach' function, it will basically run through each line? How about ($_POST as $response_id => $response), why is the $_POST exist? $response_id => $response --- Is the same meaning as $response_id = $response? Thanks, Wilson Link to comment https://forums.phpfreaks.com/topic/202629-foreach-query/ Share on other sites More sharing options...
jcbones Posted May 23, 2010 Share Posted May 23, 2010 foreach() is used to process an array. $_POST is always an array. The ($_POST as $response_id => $response) simple tells the foreach loop that you want to grab the key as $response_id, and the value as $response. So if the name of your input was "letter" and you type in "q". The $response_id is "letter". The $response is "q". Link to comment https://forums.phpfreaks.com/topic/202629-foreach-query/#findComment-1062145 Share on other sites More sharing options...
genzedu777 Posted May 23, 2010 Author Share Posted May 23, 2010 Hi, thanks for the clear explanation Link to comment https://forums.phpfreaks.com/topic/202629-foreach-query/#findComment-1062152 Share on other sites More sharing options...
phant0m Posted May 23, 2010 Share Posted May 23, 2010 If you want to update many different rows with different values, you should have a look at MySQL's CASE function. That way you only need to use one query instead of issuing one query per row. Link to comment https://forums.phpfreaks.com/topic/202629-foreach-query/#findComment-1062207 Share on other sites More sharing options...
Daniel0 Posted May 23, 2010 Share Posted May 23, 2010 Also have a look at http://php.net/foreach Link to comment https://forums.phpfreaks.com/topic/202629-foreach-query/#findComment-1062212 Share on other sites More sharing options...
genzedu777 Posted May 23, 2010 Author Share Posted May 23, 2010 Hi, Could I ask one question. if (isset($_POST['submit'])) { // Write the questionnaire response rows to the response table foreach ($_POST as $response_id => $response) { $query = "UPDATE mismatch_response SET response = '$response' WHERE response_id = '$response_id'"; mysqli_query($dbc, $query); } echo '<p>Your responses have been saved.</p>'; } Why do we need $_POST? Cant we just write the code as without the $_POST? For instance, foreach ($response_id => $response)? Link to comment https://forums.phpfreaks.com/topic/202629-foreach-query/#findComment-1062230 Share on other sites More sharing options...
kenrbnsn Posted May 23, 2010 Share Posted May 23, 2010 Did you read the responses and the manual page. Foreach loops through an array. $_POST is the array that holds all the responses from a form. Ken Link to comment https://forums.phpfreaks.com/topic/202629-foreach-query/#findComment-1062246 Share on other sites More sharing options...
genzedu777 Posted May 24, 2010 Author Share Posted May 24, 2010 Thanks, I have gotten it Link to comment https://forums.phpfreaks.com/topic/202629-foreach-query/#findComment-1062622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.