Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Isn't it better to group the information by student?</p> <p>Example:</p> <p><pre><code>&lt;form...> &lt;? foreach ($students as $id => $info) : ?> &lt;input type="text" name="fname;&lt;=$id;?>" value="&lt;?=$info['fname'];?>" /> &lt;input type="text" name="lname;&lt;=$id;?>" value="&lt;?=$info['lname'];?>" /> etc &lt;? endforeach ?> &lt;/form> </pre></code></p> <p>(I'm using PHP short tags here) <br /><br /> Then, when you process the form: <br /><br /></p> <pre><code>$update = array(); foreach ($_POST as $key => $value) { // key will look like: fname;1, fname;2, etc // so, split it on the ; sign to separate the field name from the student's id $data = explode(';',$key); // result: // $data[0] = fname, lname, etc // $data[1] = 1, 2, etc // you can add some checks to make sure that this field is valid // and that the id is in fact a valid id (number, > 0, etc) // sanitize data (however you want, just an example) $value = mysql_real_escape_string(trim($value)); // now add it to the update array, grouped by student id $update[$data[1]][$data[0]] = $value; // result: // $update[1]['fname'] = 'First name student 1'; // $update[1]['lname'] = 'Last name student 1'; // $update[2]['fname'] = 'First name student 2'; // $update[2]['lname'] = 'Last name student 2'; // etc } </code></pre> <p>After that, go through the update array:</p> <pre><code>foreach ($update as $id => $info) { $sql = "UPDATE students SET fname = '".$info['fname']."', lname = '".$info['lname']."' WHERE id = $id"; mysql_query($sql); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload