7

我有两张表,一张用于学生记录,一张用于学生的兄弟姐妹。

当我想更新表格时,我会将它们回显到 textareas。

我对学生表执行此操作没有任何问题,但对于兄弟姐妹表,如果学生有两个或更多兄弟姐妹,我的更新将失败,因为 textareas 的名称成为最后一行(最后一个兄弟姐妹)。

我该如何处理这种一对多的关系?

这是我的代码:

<?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); 
?>


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

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

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

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

echo"<br/>Student's Sibling(s)";

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

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

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

<?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 "<p style=margin-top:50px;><BR><BR><BR></p>"; 
echo "<a href='view.php'>Back to Records</a>"; 
} 

else { 
echo "ERROR"; 
echo $name; 
} 
?>

这是我的更新代码:

$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);
?>

这是我插入的 html 代码:

<html>
    <head>
        <style type = "text/css">
            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');
            }
        </style>
    <title>Registration Form</title>
    </head>

    <body bgcolor="black">

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

                <form action = "cc.php" method ="post">
                        <tr bgcolor="red">
                        <td colspan="3">&nbsp;</td>

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

                        </form>


                        <form action="view.php" method="post" target="_blank">  
                        <input type = "submit" value = "View/Update/Delete" name = "View" >
                        </form>

                        </td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                </table>

            </div>
            <div id = "footer"></div>
        </div>
    </body>
</html>

这是 INSERT 的 PHP 代码:

<?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");
?>
4

1 回答 1

0

您可以将 [] 添加到字段名称的末尾以将其作为数组发布。您没有发布任何更新代码,所以这里有一个简单的通用示例:

HTML:

<input type="text" name="sibling[unique_id][reg_no]" />
<input type="text" name="sibling[unique_id][name]" />
<input type="text" name="sibling[unique_id][birth]" />
... and so on  

PHP:

// connect to your database as normal
// do your student update as normal

// now we're looping through siblings
foreach($_POST['sibling'] AS $sibling){
    $reg_no = $sibling['reg_no'];
    $name = $sibling['name'];
    $birth = $sibling['birth'];
    ... and so on to set all your variables

    // and now you can run your update query
}

如果兄弟姐妹有自己的唯一 ID,您可以在括号中指定,name="sibling[unique_id]"然后在 php 中您将拥有$_POST['sibling']['unique_id']

于 2012-09-22T03:39:38.083 回答