Jump to content

foreach query


genzedu777

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.