php pdo lastInsertId()作为第二个表的输入

时间:2012-08-30 04:33:16

标签: php pdo

我感谢所有的帮助!!我是PDO的新手,当我对2个不同的表执行插入操作时遇到问题。我在第二个表(fk_employee)中有一个字段,我希望从第一个查询(插入)中记录最后一个id。当我运行查询时,我收到以下错误:

Query failed: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; 'address1', 'address2', 'city', 'TN', '77777', ' at line 10

这是我的方法:

        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

CREATE TABLE IF NOT EXISTS `ds_employee_address` (
  `id_employee_address` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `fk_employee` int(10) unsigned NOT NULL,
  `address1` varchar(100) NOT NULL DEFAULT '',
  `address2` varchar(100) NOT NULL DEFAULT '',
  `city` varchar(100) NOT NULL DEFAULT '',
  `state` varchar(100) NOT NULL DEFAULT '',
  `postal_code` varchar(100) NOT NULL DEFAULT '',
  `country` varchar(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`id_employee_address`),
  UNIQUE KEY `fk_employee` (`fk_employee`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 

我确信它很简单,但我一直在敲打我的脑袋而无法弄明白!!

谢谢, REF

1 个答案:

答案 0 :(得分:0)

将该查询更改为以下。

$sql = "INSERT INTO " . TBL_EMPLOYEEADD . " (
                fk_employee,
                address1,
                address2,
                city,
                state,
                postal_code,
                country
              ) VALUES (
                :fk_employee,
                :address1,
                :address2,
                :city,
                :state,
                :postal_code,
                :country
              )";