【发布时间】:2016-02-13 20:39:43
【问题描述】:
我正在尝试针对同一个 id 获取单选按钮和复选框值,但两个值都存储在两个不同的 id 中。 问题出在哪里? 帮帮我
$sql1 ="INSERT INTO student (name,fathername)
VALUES ('$name','$fathername')";
$sql2 = "SELECT last_insert_id() as id";
$res1 = mysqli_query($conn, $sql1); //here you insert
$res2 = mysqli_query($conn, $sql2); //here you fetch the ID you inserted
$id = mysqli_fetch_array($res2)['id'];
$sql3 = "INSERT INTO information (user_id,email)
VALUES ('$id','$email')"; //here you use that said ID in your second query
$res3 = mysqli_query($conn, $sql3); //aaand you insert
Now the problem starts from here, both values are stored against different ids.
//For insertion multiple values of checkbox
$sql6="INSERT INTO information (checkbox) VALUES ('" . $checkBox . "')";
$res6 = mysqli_query($conn, $sql6);
//For insertion radio button value
$sql7 ="INSERT INTO information (radio) VALUES ('" . $gender . "')";
$res7 = mysqli_query($conn, $sql7);
如果我尝试插入这样的值 ('$id','" . $checkBox . "') 和 ('$id','" . $gender . "'),它会在数据库中返回空值。
【问题讨论】:
-
Y 不在一个插入查询中?
-
INSERT INTO 信息 (user_id,email,check box,radio) VALUES ('$id','$email','$checkbox','$gender')
-
@devpro 哦,谢谢!我在没有任何具体原因的情况下运行两个不同的查询。仍然有一个小问题,这两个字段都不适用于最后一个插入 id 即'$id'。 ?
-
恭喜兄弟...编码愉快
-
@devpro 和你一样 :)
标签: php mysqli sql-insert