简体   繁体   English

用Select(PDO)和php插入

[英]Insert Into with Select(PDO) and php

Ok so I need to insert some values into a table but i need to search for one of the data in the database so i tried to do it like this 好的,所以我需要在表中插入一些值,但是我需要搜索数据库中的数据之一,所以我试图这样做

$bookid = $_GET['var'];
$username = $_GET['username'];

$quer2 = "Insert Into reserved (username,bookid) VALUES ((SELECT userid FROM users WHERE   username=:username),:var)";
$query2 = $dbc->prepare($quer2);
$query2  ->bindParam(':username',$username);
$query2  ->bindParam(':var',$bookid);
$query2 ->execute();

The problem is that I get this error 问题是我得到了这个错误

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "username" violates not-null constraint DETAIL: Failing row contains (6, null, 2014-09-22 13:06:33.262). SQLSTATE [23502]:不为null违反:7错误:“用户名”列中的null值违反了非null约束详细信息:失败行包含(6,null,2014-09-22 13:06:33.262)。

I tried the query in the database and it works so I guess that there is some error in bindParam but I can't understand what it is... So if anyone could help me I would appreciate it, thank you 我在数据库中尝试了该查询,因此可以正常工作,所以我想bindParam中存在一些错误,但是我不明白它是什么...所以如果有人可以帮助我,我将不胜感激,谢谢

change 更改

$query2  ->bindParam(':var',$var);

to

$query2  ->bindParam(':var',$bookid);

update your query to something like this 将您的查询更新为这样的内容

Insert Into reserved (username,bookid)
SELECT userid, :var FROM users WHERE username=:username

just bind the params and run the query. 只需绑定参数并运行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM