[英]php MySQL INSERT value referencing another table
In the code below, I'm most concerned about the last three variables. 在下面的代码中,我最关心最后三个变量。 The php variables (
$ae, $pe, and $de
) represent a user's full name as a string. php变量(
$ae, $pe, and $de
)将用户的全名表示为字符串。
I'm trying to insert into job_schedule
the user's ID that is from the table users
. 我试图将来自表
users
的用户ID插入到job_schedule
。
Currently my result inserts all of the data into the table but places "0" into each record of the last three columns (AE, PE, DE) as if it was not able to find the USERID
. 当前,我的结果将所有数据插入到表中,但是将“ 0”放入最后三列(AE,PE,DE)的每个记录中,好像无法找到
USERID
。
using PDO: 使用PDO:
$res = $db_qms->prepare("INSERT INTO `job_schedule` (`JID`, `HPL`, `WO`, `DESCRIP`, `MFG_LINE`, `CUSTOMER`, `AE`, `PE`, `DE`) VALUES (:jid, :hpl, :wo, :descrip, :mfg, :customer, :ae, :pe, :de)");
$res->execute(array(
":jid" => $id,
":hpl" => $hpl,
":wo" => $wo,
":descrip" => $descrip,
":mfg" => $mfg,
":customer" => $customer,
":ae" => "SELECT `USERID` FROM `users` WHERE " . $ae . " LIKE (CONCAT(`users`.`FIRSTNAME`, ' ' , `users`.`LASTNAME`))",
":pe" => "SELECT `USERID` FROM `users` WHERE " . $pe . " LIKE (CONCAT(`users`.`FIRSTNAME`, ' ' , `users`.`LASTNAME`))",
":de" => "SELECT `USERID` FROM `users` WHERE " . $de . " LIKE (CONCAT(`users`.`FIRSTNAME`, ' ' , `users`.`LASTNAME`))"
));
I also tried to see if my SELECT statement was correct. 我还尝试查看我的SELECT语句是否正确。 So I ran the following code into phpMyAdmin SQL section and I was able to pull a value.
因此,我将以下代码运行到phpMyAdmin SQL部分中,并能够提取一个值。 So the Select statement works.
因此,Select语句有效。
"SELECT `USERID` FROM `users` WHERE "John Doe" LIKE (CONCAT(`users`.`FIRSTNAME`, ' ' , `users`.`LASTNAME`))"
The result provided a value for USERID
结果提供了
USERID
的值
I have no idea where to go from here. 我不知道从这里去哪里。 How do I insert the user's ID from
users.USERID
when I am given the user's full name? 当获得用户的全名时,如何从
users.USERID
插入用户的ID?
Try using INSERT SET syntax instead. 尝试改用INSERT SET语法。
$rest = $db_qms->prepare(
"INSERT INTO `job_schedule` " .
"SET " .
"`JID` = :jid, " .
"`HPL` = :hpl, " .
"`WO` = :wo, " .
"`DESCRIP` = :descrip, " .
"`MFG_LINE` = :mfg, "
"`CUSTOMER` = :customer, " .
"`AE` = (SELECT `USERID` FROM `users` WHERE (:ae LIKE CONCAT(`users`.`FIRSTNAME`, ' ', `users`.`LASTNAME`))), " .
"`PE` = (SELECT `USERID` FROM `users` WHERE (:pe LIKE CONCAT(`users`.`FIRSTNAME`, ' ', `users`.`LASTNAME`))), " .
"`DE` = (SELECT `USERID` FROM `users` WHERE (:de LIKE CONCAT(`users`.`FIRSTNAME`, ' ', `users`.`LASTNAME`)));"
);
$res->execute(array(
":jid" => $id,
":hpl" => $hpl,
":wo" => $wo,
":descrip" => $descrip,
":mfg" => $mfg,
":customer" => $customer,
":ae" => $ae,
":pe" => $pe,
":de" => $de
));
I have never seen an sql statement use a string in the where clause as you are doing. 我从未见过像sql语句那样在where子句中使用字符串。
I would write the sql differently: 我会写不同的SQL:
":ae" => "SELECT USERID
FROM users
WHERE CONCAT(users.FIRSTNAME, ' ', users.LASTNAME) LIKE \\"" . $ae . "\\""; “:AE”=> “SELECT
USERID
FROM users
WHERE CONCAT(users.FIRSTNAME, '',users.LASTNAME)LIKE \\” “$ AE ”\\“”;。。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.