Lindsie 0 Newbie Poster

I am a newbie so please be kind. I have put together a form using multiple queries which pulls information from MySQL tables that are joined by ID number. It displays the info in a form so updates can be made. I cannot get it to update more than one of the records at a time.

For example: Joe Blow lives at 123 sesame street and has 3 children: Sally F 10, Bobby M 8 & Jake M 4 - the child info is stored in a separate table and pulled with a separate query. I make changes to each of the records pertaining to the children but it only saves the changes made to the last record. What am I doing wrong? How would I go about making it so a new child record could be added if necessary?

Part of the code that pulls multiple records...

<? $sql = "SELECT * FROM $usertable2 where $usertable2.id = $id";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No children listed.";
    
}

while ($row = mysql_fetch_assoc($result)) {?>
 <tr>      
          <td width="24%"><input type="text" size="40" name="ChildFName[i]"  value="<? echo $row["ChildFName"] ?>"/></td>
          <td width="23%"><input type="text" size="40" name="ChildLName" value="<? echo $row["ChildLName"]  ?>"/></td>
          <td width="13%">
            <input type="text" size="20" name="ChildDOB" value="<? echo $row["ChildDOB"] ?>"/></td>
          <td width="4%">
            <input type="text" size="3" name="Age" value="<? echo $row["Age"] ?>"/></td>
          <td width="14%"><input type="text" size="3" name="ChildGender" value="<? echo $row["ChildGender"] ?>"/></td>
          <td width="19%">
            <input type="text" size="3" name="ShowedID" value="<? echo $row["ShowedID"]?>"/>
            <input type="hidden" size="3" name="childID" value="<? echo $row["childID"]?>"/>
            <input type="hidden" size="3" name="id" value="<? echo $row["id"]?>"/></td>
           </tr>
         
 <tr>

what i have for the processing code for this part...

<PHP?
$ChildFName = $_POST['ChildFName'];
$ChildLName = $_POST['ChildLName'];
$ChildDOB = $_POST['ChildDOB'];
$ChildGender = $_POST['ChildGender'];
$Age = $_POST['Age'];
$ShowedId = $_POST['ShowedId'];
$childID = $_POST['childID'];
$id = $_POST['id'];

$query="UPDATE $usertable2 SET ChildFName='".$ChildFName."',ChildLName='".$ChildLName."' WHERE childID='".$childID."'";
   $result = mysql_query($query) or die('Sorry, we are unable to process your request.' . mysql_error());
   
}
   if ($result)
      echo "<br><br><h1><center>Database has been updated!</h1><br><br><h2>\n";
}
?>

Any help is much appreciated!

Dani AI

Generated

— the symptom (only the last child saving) comes from using the same plain input names for every row. When multiple inputs share a name without array syntax, PHP only keeps the final value. Fix this by naming inputs as arrays or keying them by the child id, then loop the arrays server‑side and UPDATE each record. Also stop using the old mysql_* functions; use PDO or mysqli with prepared statements for safety and performance.

Example HTML strategy (key inputs by childID so keys remain matched even if rows reorder):

<input name="ChildFName[<?= $row['childID'] ?>]" value="<?= htmlspecialchars($row['ChildFName'], ENT_QUOTES) ?>">
<input name="ChildLName[<?= $row['childID'] ?>]" value="<?= htmlspecialchars($row['ChildLName'], ENT_QUOTES) ?>">
<!-- for new rows use keys like "new1", "new2" and leave childID unset -->

Example server workflow with PDO (shows per-row update or insert for new keys):

$pdo->beginTransaction();
$update = $pdo->prepare("UPDATE children SET ChildFName=?, ChildLName=?, ChildDOB=?, ChildGender=?, Age=? WHERE childID=?");
$insert = $pdo->prepare("INSERT INTO children (ChildFName, ChildLName, ChildDOB, ChildGender, Age, parentID) VALUES (?, ?, ?, ?, ?, ?)");

foreach ($_POST['ChildFName'] as $childID => $fname) {
    $lname  = $_POST['ChildLName'][$childID] ?? '';
    $dob    = $_POST['ChildDOB'][$childID] ?? null;
    $gender = $_POST['ChildGender'][$childID] ?? '';
    $age    = $_POST['Age'][$childID] ?? null;

    if (ctype_digit((string)$childID)) {
        $update->execute([$fname, $lname, $dob, $gender, $age, (int)$childID]);
    } else {
        $insert->execute([$fname, $lname, $dob, $gender, $age, $parentID]);
    }
}
$pdo->commit();

Notes and troubleshooting: validate and sanitize every input, escape values into form fields with htmlspecialchars, wrap DB changes in a transaction to avoid partial updates, and detect new rows by empty/nonnumeric childID keys. For adding rows dynamically, append form rows client‑side (JS) with unique "newX" keys and handle them as INSERTs on submit. For PDO docs see PDO prepared statements.

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.