$status = 1;
$user_name = aaa;
$mysql_qry = "INSERT INTO l_status (user_id , status) values ('select id from user where username in (''$user_name'')','$status')";
if($conn->query($mysql_qry) === TRUE ) {
echo "inserted.";
}
else {
echo "Error" . $mysql_qry . "<br>" . $conn->error;
}
我想在l_status表中插入id和status,我需要从用户表中选择id。
值已插入,但每次都是id = 0。 在用户表中id对于不同的用户是不同的。
请帮助我,这是php的新手。
答案 0 :(得分:0)
拆分:
$mysql_qry = 'SELECT id FROM user WHERE username = "'.$user_name.'"';
while($res = $conn->query($mysql_qry){
$id = $res['id'];
$mysql_qry2 = "INSERT INTO l_status (user_id , status) values ('$id','$status')";
if($conn->query($mysql_qry2) === TRUE ) {
echo "inserted.";
}else {
echo "Error" . $mysql_qry2 . "<br>" . $conn->error;
}
}
或在一个查询中:
$mysql_qry = "INSERT INTO l_status (user_id , status) values ((select id from user where username = '$user_name') ,'$status')";
请等一下你的$ var ='test'; - &GT;你必须引用它们。 看看你的
$user_name = 'aaa';
答案 1 :(得分:0)
$user_name = 'aaa';
$mysql_qry = "INSERT INTO chaosgam_ghs.login_status (user_id , status) values ((select user_id from chaosgam_ghs.user_table where username = '$user_name'),'$status')";
此代码中显示了2个错误。