【发布时间】:2015-03-02 16:33:59
【问题描述】:
TABLE1: table1ID | studentnum | lname | fname | mname
TABLE2: table2ID | studentnum | remarks | others
//THIS IS MY FORM 1
$sql = "INSERT INTO TABLE1 (studentnum, lname, fname, mname)
VALUES ('$studentnum','$lname','$fname','$mname')";
$sql1 = $sql = "INSERT INTO TABLE2 (studentnum)
VALUES ('$studentnum')";
mysql_select_db('databasename');
$retval = mysql_query($sql);
if(!$retval ) {
die('Could not enter data: ' . mysql_error());
}
$retval = mysql_query($sql1);
if(!$retval ) {
die('Could not enter data: ' . mysql_error());
}
echo "Data added successfully!";
mysql_close($conn);
SAMPLE OUTPUT:
TABLE1:
table1ID | studentnum | lname | fname | mname
1 | 1001 | My | An | Swer
TABLE2:
table2ID | studentnum | remarks | others
1 | 1001 | |
表 2 现在具有表 1 中的 studentnum 编码数据。 我想要这个输出,我怎样才能从 FORM 2 中做到这一点
//sample output with form1 and form2 values
table2ID | studentnum | remarks | others
1 | 1001 | good | N/A
我有 2 个表格和 2 个表格,请参阅上面的代码。任何人都可以帮助我如何做到这一点?
【问题讨论】:
-
警告:如果您只是学习 PHP,请不要学习过时的
mysql_query接口。这很糟糕,并且在 PHP 的未来版本中被删除。像PDO is not hard to learn 这样的现代替代品。像PHP The Right Way 这样的指南可以帮助解释最佳实践。始终绝对确定您的用户参数是properly escaped,否则您将遇到严重的SQL injection bugs。 -
为什么要在 table2 插入数据之前将部分记录输入到 table2 中?这是没有意义的。准备好要填充的表单数据后,只需在表 2 中创建记录即可。如果您在运行脚本时同时拥有 table1 和 table 2 数据,您还可以跨连接插入以一次将记录添加到两个表中。
标签: php html mysql sql sql-server