hi im a beginner on php and i just want to know if my codes is correct because when i try to update it it only saves the last value i hope some one can understand me.

    $id = ($_REQUEST['StudentNumber']);
    $result= mysql_query("SELECT NameofSiblings, Age, HEA, CivilStatus, Occupation FROM $tblname where StudentNumber='$id'");
    $child = $_REQUEST['NumberofChildren'];

    echo "<form method='post' >";

    for($n=1; $n<=$child; $n++)
    {       
    while($row=mysql_fetch_array($result))
    {
        echo "<table>
        <tr>
        <td class='siblings'><input type='text' name='sibname[]' style='width: 150px;' value=".$row['NameofSiblings']."></td>
        <td class='siblings'><input type='text' name='sibage[]' style='width: 35px;' value=".$row['Age']."></td>
        <td class='siblings'><input type='text' name='sibhea[]' style='width: 260px;' value=".$row['HEA']."></td>
        <td class='siblings'><input type='text' name='sibcs[]' style='width: 100px;' value=".$row['CivilStatus']."></td>
        <td class='siblings'><input type='text' name='siboccu[]' style='width: 100px;' value=".$row['Occupation']."></td>
        </tr>        
        </table>";
    }
}
<td><input type="hidden" name="n" value="<?php echo $child; ?>">

and this is my separate php for update

$n=intval($_POST['n']);
for($i=0;$i<$n;$i++)
{
  $sibname =$_POST['sibname'][$i];
  $sibage =$_POST['sibage'][$i];
  $sibhea =$_POST['sibhea'][$i];
  $sibcs =$_POST['sibcs'][$i];
  $siboccu =$_POST['siboccu'][$i];*/

    $query = "UPDATE $tblname SET NameofSiblings = '$sibname', Age = '$sibage', HEA = '$sibhea', CivilStatus = '$sibcs', Occupation = '$siboccu' WHERE StudentNumber='$sn' ";

    mysql_query($query);
}

Dani AI

Generated

Short diagnosis: the form-generation + update logic are misaligned. Because StudentNumber is not unique (as you confirmed), running UPDATE ... WHERE StudentNumber='$sn' inside a loop will repeatedly set every sibling row for that student — so the final loop values overwrite earlier ones. Also the form code mixes a for-loop around a while(mysqlfetch*); the result set is exhausted on the first iteration, so the form and the posted arrays won't line up with DB rows.

Use the table primary key (StudID) to bind inputs to specific rows, and drop the outer for-loop. Name the inputs keyed by that ID so you don't rely on numeric array order.

Example: generate the form rows with each input keyed by StudID (escape values when printing):

while ($row = mysql_fetch_assoc($result)) {
  $id = (int)$row['StudID'];
  echo '<input type="text" name="sibname['.$id.']" value="'.htmlspecialchars($row['NameofSiblings'], ENT_QUOTES).'">';
  echo '<input type="text" name="sibage['.$id.']" value="'.htmlspecialchars($row['Age'], ENT_QUOTES).'">';
  // HEA, CivilStatus, Occupation similarly...
}

On submit, update each row by its StudID instead of StudentNumber:

foreach ($_POST['sibname'] as $id => $name) {
  $id = (int)$id;
  $name = mysql_real_escape_string($name);
  $age  = mysql_real_escape_string($_POST['sibage'][$id]);
  // escape the rest...
  mysql_query("UPDATE $tblname SET NameofSiblings='$name', Age='$age', HEA='$hea', CivilStatus='$cs', Occupation='$occu' WHERE StudID=$id");
}

Notes and quick tips: was right to question uniqueness; and were on the right track asking about table structure — using StudID (AUTOINCREMENT PK) fixes the WHERE clause. ’s point about arrays is okay — arrays are appropriate, but keyed arrays are more robust than index counting. Finally, move away from deprecated mysql* to mysqli or PDO with prepared statements to avoid SQL injection and for future compatibility; use var_dump($_POST) while testing to verify the posted structure.

Recommended Answers

All 7 Replies

Hmm I can't test it but i think it is all good, is StudentNumber unique?
If it is unique then it will update just 1 row because you have singe row with that value...

nope it is not unique and also not a primary key

what is structure of $tblname and what is primary key in that table

$tblname is where i put NameofSibling,Age... all in varchar and theres a primary key StudID but its on AUTO_INCREMENT

my suggestion is:
1. use method "GET" instead of POST to populate data from database into form.
2. give the text name according to table column. No need to name it like an array ( name='sibname[]', name='sibage[]')

ok,i've tried using get method but it wont work and i think i need the array to get the value of the loop..can someone give me an example of how to do it correctly?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.