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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.