php pdo lastInsertId()作为第二个表的输入
我感谢所有的帮助!!我是PDO新手,在对两个不同的表执行插入时遇到问题。我在第二个表(fk_employee)中有一个字段,我想要第一个查询(insert)中最后记录的id。运行查询时,我收到以下错误:php pdo lastInsertId()作为第二个表的输入,php,pdo,Php,Pdo,我感谢所有的帮助!!我是PDO新手,在对两个不同的表执行插入时遇到问题。我在第二个表(fk_employee)中有一个字段,我想要第一个查询(insert)中最后记录的id。运行查询时,我收到以下错误: 查询失败:SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解在“;”附近使用的正确语法第10行的“地址1”、“地址2”、“城市”、“TN”、“77777” 以下是我的方法: function compl
查询失败:SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以了解在“;”附近使用的正确语法第10行的“地址1”、“地址2”、“城市”、“TN”、“77777”
以下是我的方法:
function completeCallback()
{
// finally, all form data is valid and the user has confirmed they
// want to proceed. Now we do all the final processing here.
$name_title = $this->getValue('name_title');
$emp_lastname = $this->getValue('emp_lastname');
$emp_suffix = $this->getValue('emp_suffix');
$emp_firstname = $this->getValue('emp_firstname');
$emp_middlename = $this->getValue('emp_middlename');
$emp_prefername = $this->getValue('emp_prefername');
$address1 = $this->getValue('address1');
$address2 = $this->getValue('address2');
$city = $this->getValue('city');
$state = $this->getValue('state');
$postal_code = $this->getValue('postal_code');
$country = $this->getValue('country');
$conn = parent::connect();
$sql = "INSERT INTO " . TBL_EMPLOYEES . " (
name_title,
emp_lastname,
emp_firstname,
emp_suffix,
emp_prefername,
emp_middlename
) VALUES (
:name_title,
:emp_lastname,
:emp_firstname,
:emp_suffix,
:emp_prefername,
:emp_middlename
)";
try {
$st = $conn->prepare( $sql );
$st->bindValue( ":name_title", $name_title, PDO::PARAM_STR );
$st->bindValue( ":emp_lastname", $emp_lastname, PDO::PARAM_STR );
$st->bindValue( ":emp_firstname", $emp_firstname, PDO::PARAM_STR );
$st->bindValue( ":emp_suffix", $emp_suffix, PDO::PARAM_STR );
$st->bindValue( ":emp_prefername", $emp_prefername, PDO::PARAM_STR );
$st->bindValue( ":emp_middlename", $emp_middlename, PDO::PARAM_STR );
if($st->execute()){
$lastInsertID = $conn->lastInsertId();
}
$sql = "INSERT INTO " . TBL_EMPLOYEEADD . " (
fk_employee,
address1,
address2,
city,
state,
postal_code,
country
) VALUES (
:fk_employee;
:address1,
:address2,
:city,
:state,
:postal_code,
:country
)";
$st = $conn->prepare( $sql );
$st->bindValue( ':fk_employee', $lastInsertID, PDO::PARAM_INT );
$st->bindValue( ':address1', $address1, PDO::PARAM_STR );
$st->bindValue( ':address2', $address2, PDO::PARAM_STR );
$st->bindValue( ':city', $city, PDO::PARAM_STR );
$st->bindValue( ':state', $state, PDO::PARAM_STR );
$st->bindValue( ':postal_code', $postal_code, PDO::PARAM_STR );
$st->bindValue( ':country', $country, PDO::PARAM_STR );
if($st->execute()){
$lastInsertID = $conn->lastInsertId();
}
} catch ( PDOException $e ) {
parent::disconnect( $conn );
die( "Query failed: " . $e->getMessage() );
}
}
这是我的两张桌子:
CREATE TABLE IF NOT EXISTS `ds_employee` (
`id_employee` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name_title` varchar(20) DEFAULT NULL,
`emp_lastname` varchar(100) NOT NULL DEFAULT '',
`emp_suffix` varchar(50) DEFAULT NULL,
`emp_firstname` varchar(100) NOT NULL DEFAULT '',
`emp_prefername` varchar(100) NOT NULL DEFAULT '',
`emp_middlename` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id_employee`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
表2
我相信这很简单,但我一直在敲我的头,想不出来
谢谢,
REF将该查询更改为以下内容
$sql = "INSERT INTO " . TBL_EMPLOYEEADD . " (
fk_employee,
address1,
address2,
city,
state,
postal_code,
country
) VALUES (
:fk_employee,
:address1,
:address2,
:city,
:state,
:postal_code,
:country
)";
我很抱歉用这么愚蠢的问题来打扰你!!非常感谢你。就这样!!无关:fk_员工是否建议我认为它的功能?
$sql = "INSERT INTO " . TBL_EMPLOYEEADD . " (
fk_employee,
address1,
address2,
city,
state,
postal_code,
country
) VALUES (
:fk_employee,
:address1,
:address2,
:city,
:state,
:postal_code,
:country
)";