php help. confused with update
<p>I have a two tables, one for student records and one for siblings of the student.</p>

<p>When I  want to update the tables, I echo them to textareas. </p>

<p>I don't have any problems doing this for the student table, but for the siblings table, if the student has two or more siblings, my update fails because the name of the textareas becomes the last row (the last sibling).</p>

<p>How do I deal with this one-to-many relationship?</p>

<p>Here's my code:</p>

<pre><code>&lt;?php
$database="sy_database"; 
mysql_connect ("localhost", "root", ""); 
@mysql_select_db($database) or die( "Unable to select database"); 
$id=$_GET['up']; 
$sno=$_GET['s_no'];
$order = "SELECT * FROM sy_form WHERE reg_no='$id'";
$result = mysql_query($order); 
$row = mysql_fetch_array($result); 
?&gt;


&lt;?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
echo "&lt;form method=post action=ambot.php&gt;";
mysql_select_db("sy_database", $con);
$result = mysql_query("SELECT * FROM sy_form where reg_no='$id'");
echo "&lt;body align=center&gt;";
echo"Student Record";
echo "&lt;table border='1' width=1000px margin=0 padding=0 align=center&gt;
&lt;tr bgcolor=yellow&gt;
&lt;th&gt;Reg No.&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Date of Birth&lt;/th&gt;
&lt;th&gt;Age&lt;/th&gt;
&lt;th&gt;Class&lt;/th&gt;
&lt;th&gt;School&lt;/th&gt;
&lt;/tr&gt;";

while($row = mysql_fetch_array($result))
  {
  echo "&lt;tr align=center&gt;";
echo "&lt;td&gt;&lt;textarea readonly=readonly rows=1 cols=3 name=reg_no&gt;". $row['reg_no'] ."                    &lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=20 name=name&gt;" .$row['name']. "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=20 name=birth&gt;" . $row['birth'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=3 name=age&gt;" . $row['age'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=3 name=sclass&gt;" . $row['sclass'] . "&lt;/textarea&gt;     &lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=20 name=school&gt;" . $row['school'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  }
echo "&lt;/table&gt;";

$result = mysql_query("SELECT * FROM sy_form where reg_no='$id'");
echo "&lt;br&gt;&lt;table border='1' width=1250px margin=0 padding=0 align=center&gt;
&lt;tr bgcolor=yellow&gt;
&lt;th&gt;Father's Name&lt;/th&gt;
&lt;th&gt;Father's Occupation&lt;/th&gt;
&lt;th&gt;Mother's Name&lt;/th&gt;
&lt;th&gt;Mother's Occupation&lt;/th&gt;
&lt;th&gt;Address&lt;/th&gt;
&lt;th&gt;Phone No.&lt;/th&gt;
&lt;th&gt;Cellphone No.&lt;/th&gt;
&lt;th&gt;Email&lt;/th&gt;
&lt;/tr&gt;";

while($row = mysql_fetch_array($result))
  {
  echo "&lt;tr align=center&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=20 name=fname&gt;" . $row['fname'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=10 name=focc&gt;" . $row['focc'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=20 name=mname&gt;" . $row['mname'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=10 name=mocc&gt;" . $row['mocc'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=25 name=address&gt;" . $row['address'] . "&lt;/textarea&gt;        &lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=8 name=phone&gt;" . $row['phone'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=10 name=cp&gt;" . $row['cp'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=20 name=email&gt;" . $row['email'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  }
echo "&lt;/table&gt;";

echo"&lt;br/&gt;Student's Sibling(s)";

$result = mysql_query("SELECT * FROM siblings where reg_no='$id' AND s_no&lt;min($sno)");

echo "&lt;br/&gt;&lt;table border='1' margin=0 padding=0 align=center&gt;
&lt;tr bgcolor=yellow&gt;
&lt;th&gt;Reg No.&lt;/th&gt;
&lt;th&gt;Sibling No.&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Age&lt;/th&gt;
&lt;th&gt;School&lt;/th&gt;
&lt;/tr&gt;";

while($row = mysql_fetch_array($result))
  {
  echo "&lt;tr align=center&gt;";
  echo "&lt;td align=right&gt;&lt;textarea readonly=readonly rows=1 cols=3 name=reg_no&gt;".     $row['reg_no'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=3&gt;" . $row['s_no'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 name=sname&gt;" . $row['sname'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 cols=3 name=sage&gt;" . $row['sage'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;td&gt;&lt;textarea rows=1 name=sschool&gt;" . $row['sschool'] . "&lt;/textarea&gt;&lt;/td&gt;";
  echo "&lt;/tr&gt;";
  }
echo "&lt;/table&gt;";
echo "&lt;input type =submit value =update /&gt;";
echo "&lt;/form&gt;";
mysql_close($con);
?&gt;

&lt;?php
$database="sy_database"; 
$id=$_POST['id']; 
$ip=$_POST['ip']; 
$mac=$_POST['mac']; 
$location=$_POST['location']; 
$use=$_POST['use']; 
mysql_connect ("localhost", "root", ""); 
@mysql_select_db($database) or die( "Unable to select database"); 
$order = "UPDATE sy_form 
SET name='$name' WHERE reg_no='$reg_no'"; 
$result=mysql_query($order); 

if($result){ 
echo "&lt;p style=margin-top:50px;&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;/p&gt;"; 
echo "&lt;a href='view.php'&gt;Back to Records&lt;/a&gt;"; 
} 

else { 
echo "ERROR"; 
echo $name; 
} 
?&gt;
</code></pre>

<p>here's my update code:
    

<pre><code>$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sy_database", $con);

$result = mysql_query("UPDATE  sy_form SET name='$name', birth='$birth', age='$age',         sclass='$sclass',  school='$school' , fname='$fname', focc='$focc', mname='$mname',     mocc='$mocc', address='$address', phone='$phone', cp='$cp', email='$email' where reg_no='$reg'");

$result = mysql_query("UPDATE  siblings SET sname='$sname',sage='$sage', sschool='$sschool' where reg_no='$reg'");
echo("record updated.");
mysql_close($con);
?&gt;
</code></pre>

<p>HERE's my html code for the INSERT:</p>

<pre><code>&lt;html&gt;
    &lt;head&gt;
        &lt;style type = "text/css"&gt;
            body {
            font-family: Arial;
            margin: 0 auto;
            }
            table{
            margin: 0 auto;
            }
            #masterdiv {
            width: 800px;
            margin: 0 auto;
            }
            #header {
            margin: 0 auto;
            width: 796px;
            height: 10;
            background-image: url('header.jpg');
            }
            #fillForm {
            margin: 0 auto;
            width: 800px;
            background-color: #FFFFFF;
            }
            #footer {
            margin: 0 auto;
            width: 798px;
            height: 75px;
            background-image: url('footer.jpg');
            }
        &lt;/style&gt;
    &lt;title&gt;Registration Form&lt;/title&gt;
    &lt;/head&gt;

    &lt;body bgcolor="black"&gt;

        &lt;div id = "masterdiv"&gt;
            &lt;div id = "header"&gt;&lt;/div&gt;
            &lt;div id = "fillForm"&gt;
            &lt;p align="right" style="color:red;font-size:60px;"/&gt; Registration Form&lt;/p&gt;
                &lt;table cellspacing="0" cellpadding="0" border="0"&gt;

                &lt;form action = "cc.php" method ="post"&gt;
                        &lt;tr bgcolor="red"&gt;
                        &lt;td colspan="3"&gt;&amp;nbsp;&lt;/td&gt;

                        &lt;td&gt;&lt;b&gt;Registration No:&lt;/b&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" name = "reg_no"&gt;&lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;Name: &lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" size = "40" name = "name"&gt;&lt;/td&gt;
                        &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                        &lt;td&gt;Date of Birth: &lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" name = "birth"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;Age: &lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" size = "20" name = "age"&gt;&lt;/td&gt;
                        &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                        &lt;td&gt;Class as at January: &lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" name = "sclass"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;School: &lt;/td&gt;
                        &lt;td colspan = "4"&gt;&lt;input type = "text" size = "80" name = "school"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;Father's Name: &lt;/td&gt;
                        &lt;td colspan = "4"&gt;&lt;input type = "text" size = "80" name = "fname"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;Father's Occupation: &lt;/td&gt;
                        &lt;td colspan = "4"&gt;&lt;input type = "text" size = "80" name = "focc"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;Mother's Name: &lt;/td&gt;
                        &lt;td colspan = "4"&gt;&lt;input type = "text" size = "80" name = "mname"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;Mother's Occupation: &lt;/td&gt;
                        &lt;td colspan = "4"&gt;&lt;input type = "text" size = "80" name = "mocc"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
                    &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td colspan = "5"&gt;Siblings (if any)&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                        &lt;td&gt;Name&lt;/td&gt;
                        &lt;td&gt;Age&lt;/td&gt;
                        &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                        &lt;td&gt;School&lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/table&gt;
                &lt;table&gt;
                    &lt;tr&gt;
                        &lt;td&gt;1. &lt;input type = "text" size = "50" name = "sname"&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" size = "25" name = "sage"&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" size = "30" name = "sschool"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;2. &lt;input type = "text" size = "50" name = "sname2"&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" size = "25" name = "sage2"&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" size = "30" name = "sschool2"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;
                        &lt;td&gt;3. &lt;input type = "text" size = "50" name = "sname3"&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" size = "25" name = "sage3"&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type = "text" size = "30" name = "sschool3"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr align = "right"&gt;
                        &lt;td colspan = "3"&gt;Address: &lt;input type = "text" size = "80" name = "address"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr align = "right"&gt;
                        &lt;td colspan = "3"&gt;Phone Number: &lt;input type = "text" size = "80" name = "phone"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr align = "right"&gt;
                        &lt;td colspan = "3"&gt;Cellphone Number (Mother/Father)&lt;input type = "text" size = "80" name = "cp"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr align = "right"&gt;
                        &lt;td colspan = "3"&gt;Email: &lt;input type = "text" size = "80" name = "email"&gt;&lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
                    &lt;tr align = "center"&gt;
                        &lt;td colspan = "3"&gt;
                        &lt;input type = "submit" value = "Submit" name = "Submit"&gt;

                        &lt;/form&gt;


                        &lt;form action="view.php" method="post" target="_blank"&gt;  
                        &lt;input type = "submit" value = "View/Update/Delete" name = "View" &gt;
                        &lt;/form&gt;

                        &lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
                &lt;/table&gt;

            &lt;/div&gt;
            &lt;div id = "footer"&gt;&lt;/div&gt;
        &lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>and here's the PHP code for INSERT:</p>

<pre><code>&lt;?php   

    //student record details
    $name = $_POST['name'];
    $birth = $_POST['birth'];
    $age = $_POST['age'];
    $sclass = $_POST['sclass'];
    $school = $_POST['school'];
    $fname = $_POST['fname'];
    $focc = $_POST['focc'];
    $mname = $_POST['mname'];
    $mocc = $_POST['mocc'];
    $address = $_POST['address'];
    $phone = $_POST['phone'];
    $cp = $_POST['cp'];
    $email = $_POST['email'];
    $reg = $_POST['reg_no'];

    //siblings record
    $sname = $_POST['sname'];
    $sage = $_POST['sage'];
    $sschool = $_POST['sschool'];

    $sname2 = $_POST['sname2'];
    $sage2 = $_POST['sage2'];
    $sschool2 = $_POST['sschool2'];

    $sname3 = $_POST['sname3'];
    $sage3 = $_POST['sage3'];
    $sschool3 = $_POST['sschool3'];

    // connect to mysql
    $conn = mysql_connect("localhost", "root", "")
            or die("ERR: Connection");

    // connect to database        
    $db = mysql_select_db("sy_database", $conn)
          or die("ERR: Database");



    // create mysql query
    // Insert a row of information into the table "sy_form"
    $sql = "INSERT INTO sy_form (reg_no, name, birth, age, sclass, school, fname, focc, mname, mocc, address, phone, cp, email)
    VALUES('".$reg."', '".$name."', '".$birth."', '".$age."', '".$sclass."', '".$school."', '".$fname."', '".$focc."', '".$mname."', '".$mocc."','".$address."','".$phone."','".$cp."','".$email."')";  
        if (!mysql_query($sql,$conn))
          {
          die('Error: ' . mysql_error());
          }

echo "1 record added";
    // execute query
    $exec = mysql_query($sql, $conn); 

    // Insert a row of information into the table "sy_form" (1st sibling)
    $sql_1 = "INSERT INTO siblings (reg_no,s_no, sname, sage, sschool)
    VALUES('".$reg."','', '".$sname."', '".$sage."', '".$sschool."')";   
        if($sname==null)
        return 0;

        else
        $exec = mysql_query($sql_1, $conn);      

    // Insert a row of information into the table "sy_form" (2nd sibling)
      $sql_2 = "INSERT INTO siblings (reg_no,s_no, sname, sage, sschool)
    VALUES('".$reg."','', '".$sname2."', '".$sage2."', '".$sschool2."')";
        if($sname2==null)
        return 0;

        else
        $exec = mysql_query($sql_2, $conn);
    // Insert a row of information into the table "sy_form" (3rd sibling)       
    $sql_3 = "INSERT INTO siblings (reg_no,s_no, sname, sage, sschool)
    VALUES('".$reg."','', '".$sname3."', '".$sage3."', '".$sschool3."')";
        if($sname3==null)
        return 0;

        else
        $exec = mysql_query($sql_3, $conn);


        echo ("The following data has been added to sy_database");
?&gt;
</code></pre>
