gpt4 book ai didi

php - 如何从一个表单向两个表中插入数据?

转载 作者:行者123 更新时间:2023-11-29 14:18:04 30 4
gpt4 key购买 nike

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

<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>

我需要将数据从该单一表单插入到两个表中。第一张 table 是给想要注册的人的,第二张 table 是给那个人的 sibling 的。

为什么AGE不会出现?一月份的类(class)也没有出现。手机号码总是2147483647?

哎呀,我真是毁了!有人可以帮我解决这个问题吗?我真的很想学习。 :(

最佳答案

啊,你不能像那样合并查询。尝试以下操作

$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());
}

好吧,这才刚刚开始,让你的程序运行。有很多东西需要学习。

关于php - 如何从一个表单向两个表中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12249418/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com