如何从一个表单将数据插入两个表?

时间:2012-09-03 14:05:10

标签: php mysql forms

好吧,现在,我创建了一个表单,就是这样:

<html>
<body bgcolor="lightblue">


<form align="center" action="connect2.php"  method="post" style="bgcolor:red">


        <div style="margin-left:433px" >
        Registration No.: <input type="text" name="reg" /><br />
        </div>
        </br>
        Name: <input type="text" name="name" size="50" style="margin-left:7px" />  
        <span style="margin-left:6px">
        Date of Birth: <input type="text" name="birth" size="30" /></br>
        </span>
            <div style="margin-left:12px">
                Age: <input type="text" name="age" size="15" style="margin-left:7px" />
                <span style="margin-left:167px">
                Class at August 2012 <input type="text" name="class" size="30" />
                </span>
            </div>
        School: <input type="text" name="school" size="102" style="margin-left:5px" />
        </br></br>
        Father's Name: <input type="text" name="father" size="94" style="margin-left:5px" /> </br>
        Father's Occupation: <input type="text" name="focc" size="89" />
        </br></br>
        Mother's Name:<input type="text" name="mother" size="94" style="margin-left:3px" /> </br>
        Mother's Occupation: <input type="text" name="mocc" size="88" />
        </br></br>

        <center>

            <table bgcolor="pink" width="95" height="100"style="margin-left:0px; margin-top:5px" border="0">
                <tr>
                    <td colspan="3">
                    Siblings, if any:
                    </td>
                </tr>
                <tr align="center"style="font-weight:bold">
                    <td width="5"></td>
                    <td width="20">Name</td>
                    <td width="50">Age</td>
                    <td width="20">School</td>
                </tr>
                <tr align="right">
                    <td>1.</td>
                    <td align="center"><input type="text" name="ssname" size="44" /></td>
                    <td align="center"><input type="text" name="sage1" size="5" /></td>
                    <td align="center"><input type="text" name="sschool1" size="44" /></td>
                </tr>
                <tr align="right">
                    <td>2.</td>
                    <td align="center"><input type="text" name="sname2" size="44" /></td>
                    <td align="center"><input type="text" name="sage2" size="5" /></td>
                    <td align="center"><input type="text" name="sschool2" size="44" /></td>
                </tr>
                <tr align="right">
                    <td >3.</td>
                    <td align="center"><input type="text" name="sname3" size="44" /></td>
                    <td align="center"><input type="text" name="sage3" size="5" /></td>
                    <td align="center"><input type="text" name="sschool3" size="44" /></td>
                </tr>
            </table>
        </center>
        </br>
        Address: <input type="text" name="address" size="101" /> </br>
        <p align="left" style="margin-left:271px">
        Phone Number: <input type="text" name="phone" size="20" /> </br>
        Cell Number (Mother/Father): <input type="text" name="cp" size="20" /></br>
        Email (Mother/Father): <input type="text" name="email" size="28"/>
        </p>
        <input type="Submit" name="submit" value="submit" />

        </form>




</body>
</html>

然后这是我的PHP代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<body> 
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("sy_database", $con);

$sq$sql="INSERT INTO sy_form (reg_no, name, birth, age, sclass, school, fname, focc, mname, mocc, address, phone, cp, email)
VALUES('$_POST[reg]','$_POST[name]','$_POST[birth]','$_POST[age]','$_POST[class]','$_POST[school]','$_POST[fname]','$_POST[focc]','$_POST[mname]','$_POST[mocc]','$_POST[address]','$_POST[phone]','$_POST[cp]','$_POST[email]'),
INSERT INTO siblings(sname,sage,sschool) values('$_POST[ssname]','$_POST[sage1]','$_POST[sschool1]')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

echo "1 record added";

mysql_close($con);
?>
</body> 
</html>

我需要将数据插入该单个表单的两个表中。 第一张表适用于想要注册的人,第二张表适用于该人的兄弟姐妹。

怎么会出现AGE?并且1月份的班级也没有出现。 手机号码总是2147483647?

gawd,我太毁了! 有谁可以帮我这个?我真的很想学。 :(

1 个答案:

答案 0 :(得分:1)

啊,你不能合并这样的查询。试试以下

$sql1="INSERT INTO sy_form (reg_no, name, birth, age, sclass, school, fname, focc, mname, mocc, address, phone, cp, email)".
"VALUES('$_POST[reg]','$_POST[name]','$_POST[birth]','$_POST[age]','$_POST[class]','$_POST[school]','$_POST[fname]','$_POST[focc]','$_POST[mname]','$_POST[mocc]','$_POST[address]','$_POST[phone]','$_POST[cp]','$_POST[email]')";

$sql2="INSERT INTO  siblings(sname,sage,sschool)values('$_POST[ssname]','$_POST[sage1]','$_POST[sschool1]')";

if (!mysql_query($sql1,$con))
{
    die('Error: ' . mysql_error());
}
if (!mysql_query($sql2,$con))
{
    die('Error: ' . mysql_error());
}

那刚刚开始,让程序运行。有很多东西需要学习。