views:

189

answers:

5

Is this an operator? I can't find it here.

What does the part "$_POST as $response_id => $response" in the following code means?

  // If the questionnaire form has been submitted, write the form responses to the database
  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>';
  }

Edit: Another qn here. Why is there an additional line "mysqli_query($dbc, $query); " which seems to do nothing? This piece of code is gotten from a book's source code.

+13  A: 

It is used with foreach loops to get the key and value from an array.

foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement

The first form loops over the array given by array_expression. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element).

The second form does the same thing, except that the current element's key will be assigned to the variable $key on each loop.

It can also be used for defining keys in arrays:

An array can be created by the array() language construct. It takes as parameters any number of comma-separated key => value pairs.

cletus
Thanks! Besides this, do u know also why is there an additional line "mysqli_query($dbc, $query); " which seems to do nothing? This piece of code is gotten from a book's source code.
yeeen
@yeeen: the second argument of `mysql_query()` is a resource (connection). If omitted the most recently opened connection is used. If you only connect to one database it's superfluous.
cletus
I was thinking that why no variable assigned when using mysql_query(), so whether it is neccessary. The reason is just cos UPDATE doesn't return anything, but the database is actually changed. Only queries like SELECT is necessary to return sth.
yeeen
+3  A: 

It's used to specify array keys and values:

array(
 'key1'=> 'value1',
 'key2'=> 'value2'
);

Edit: and for use in foreach loops of course (as your codes notes and as cletus answered)

More info: http://www.php.net/manual/en/language.types.array.php

pygorex1
A: 

It represents value and is used on these occasions:

  • An array
  • Foreach loop

Examples:

Array

$myarray = array('name' => 'sarfraz');

Foreach Loop

foreach ($myarray as $key => $value)
{
  $echo $value; // print sarfraz
}
Sarfraz
Keynny? No such person replying/commenting to this qn... Who r u replying to?
yeeen
Keeynny was a user who had posted a comment then removed it that's y you cant see it, even i removed my prevoius comment :)
Sarfraz
A: 

mysqli_query($dbc, $query); is not useless at all, at this time $query is just a string, mysqli_query($dbc, $query); commits this command to the database.

BTW: one of the most horrorbile code examples i ever saw when it comes to security... injection hoooo....

Hannes
i understood tt commit part alr (see comments under cletus's reply). Why is it the most horrible code eg when it comes to security?? I want to know :) I just learnt abt sql injection recently, quite interesting... the hacker actually adds "-- " to comment the rest of the sql...
yeeen
The form is actually qns with radio buttons, how to do sql injection like that? And how to protect it?
yeeen
A: 

I've always read it as "is associated with"... from "associative array".

Frank Shearar