我有 2 张桌子:
tbl用户:
+------+-----------+
| id | Name |
+------+-----------+
tblItems(此表接受多个复选框值,具体取决于选择的用户数量):
+------+-----------+---------------+
| id | items | name_id |
+------+-----------+---------------+
name_id
将获得 id 中的值tblUser
。
我使用此代码来获取 id 的值tblUser
to name_id
:
for ($i=0; $i<sizeof($checkbox);$i++){
$sql2="INSERT INTO tbl_trainings VALUES (NULL, '".$checkbox[$i]."', (SELECT id FROM tbl_info))";
$result2=mysql_query($sql2);
}
它适用INSERT
于数据库中看起来像这样的第一个数据:
+------+-----------+---------------+
| id | items | name_id |
+------+-----------+---------------+
| 1 | Bucket | 1 |
+------+-----------+---------------+
| 2 | Tree | 1 |
+------+-----------+---------------+
| 3 | House | 1 |
+------+-----------+---------------+
但在下一秒或下一秒INSERT
的数据就会出错。错误是
子查询从mysql_error();
顺便说一句,这是完整的代码:
if($_POST["Submit"]=="Submit"){
$sql1="INSERT INTO tblUser VALUES (NULL, '$fname', '$lname')";
$result1=mysql_query($sql1);
for ($i=0; $i<sizeof($checkbox);$i++){
$sql2="INSERT INTO tblItems VALUES (NULL, '".$checkbox[$i]."', (SELECT id FROM tblUser))";
$result2=mysql_query($sql2);
}
}
if($result2 && result1){
echo"<center>";
echo"<h1>";
echo "SUCCESSFUL!";
echo"</h1>";
echo"</center>";
}
else {
echo "ERROR". mysql_error();
}
数据库中所需的输出将是:
+------+-----------+---------------+
| id | items | name_id |
+------+-----------+---------------+
| 1 | Bucket | 1 |
+------+-----------+---------------+
| 2 | Tree | 1 |
+------+-----------+---------------+
| 3 | House | 1 |
+------+-----------+---------------+
| 4 | Tree | 2 |
+------+-----------+---------------+
| 5 | Air plane | 2 |
+------+-----------+---------------+
| 6 | Bucket | 3 |
+------+-----------+---------------+
任何帮助,将不胜感激。提前致谢。