MYSQL PHP使用外键插入父表和子表

时间:2016-01-23 12:48:18

标签: php mysql database sql-insert

当我尝试将数据插入子表“driver_client_detail”和“driver_client_communicate”时,我遇到了问题。

这是SQL表:

CREATE TABLE IF NOT EXISTS `choosedriver`.`driver` (
  `driver_id` INT NOT NULL AUTO_INCREMENT,
  `description` LONGTEXT NOT NULL,
  `city_id` INT NOT NULL,
  PRIMARY KEY (`driver_id`),
  INDEX `fk_driver_city1_idx` (`city_id` ASC))
ENGINE = MyISAM
AUTO_INCREMENT = 19
DEFAULT CHARACTER SET = latin1;


CREATE TABLE IF NOT EXISTS `choosedriver`.`driver_client_detail` (
  `driver_client_detail_id` INT NOT NULL AUTO_INCREMENT,
  `email` VARCHAR(255) NOT NULL,
  `pass` VARCHAR(255) NOT NULL,
  `date_inscription` DATETIME NOT NULL,
  `first_name` VARCHAR(45) NOT NULL,
  `last_name` VARCHAR(60) NOT NULL,
  `company` VARCHAR(45) NOT NULL,
  `driver_id` INT NOT NULL,
  `client_id` INT NOT NULL,
  PRIMARY KEY (`driver_client_detail_id`, `driver_id`, `client_id`),
  INDEX `fk_driver_client_detail_driver1_idx` (`driver_id` ASC),
  INDEX `fk_driver_client_detail_client1_idx` (`client_id` ASC),
  CONSTRAINT `fk_driver_client_detail_driver1`
    FOREIGN KEY (`driver_id`)
    REFERENCES `choosedriver`.`driver` (`driver_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_driver_client_detail_client1`
    FOREIGN KEY (`client_id`)
    REFERENCES `choosedriver`.`client` (`client_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


CREATE TABLE IF NOT EXISTS `choosedriver`.`driver_client_communicate` (
  `phone` VARCHAR(30) NOT NULL,
  `phonetwo` VARCHAR(30) NOT NULL,
  `address` VARCHAR(255) NOT NULL,
  `client_id` INT NOT NULL,
  `driver_id` INT NOT NULL,
  PRIMARY KEY (`client_id`, `driver_id`),
  INDEX `fk_driver_client_communicate_client1_idx` (`client_id` ASC),
  INDEX `fk_driver_client_communicate_driver1_idx` (`driver_id` ASC))
ENGINE = MyISAM
DEFAULT CHARACTER SET = latin1;

这是我插入数据的PHP代码:

date_default_timezone_set('Europe/Madrid');
  $date_inscription = date('Y-m-d H:i:s');

    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $company = "asdasdas";
    $pass = md5($_POST['pass']);
    $description = " asdasdasd ";
    $phone = " 0712981289 ";
    $email = $_POST['email'];

    $req1 = $bdd->prepare('INSERT INTO driver (description) VALUES (:description)');
  $req1->execute(array(
    'description'=>$description,
  ));
  $last_id = $bdd->lastInsertId();
  $req1->closeCursor();


  $req2 = $bdd->prepare('
    INSERT INTO driver_client_communicate (phone,driver_id) VALUES (:phone,:last_id)
    ');
  $req2->execute(array(
    'phone'=>$phone,
    'last_id'=>$last_id
  ));
  $req2->closeCursor();


  $driver_id = $last_id;
  $req3 = $bdd->prepare('
    INSERT INTO driver_client_detail (email,pass,date_inscription,first_name,last_name,company,driver_id) VALUES (:email,:pass,:date_inscription,:first_name,:last_name,:company,:driver_id)
    ');
  /* $req3->execute(array(
    'driver_email'=>$driver_email,
    'pass'=>$pass,
    'date_inscription'=>$date_inscription,
    'first_name'=>$first_name,
    'last_name'=>$last_name,
    'company'=>$company,
    'last_id'=>$last_id
  ));
  $req3->closeCursor();*/

     $req3->bindParam('email', $email, PDO::PARAM_STR);
$req3->bindParam('pass', $pass, PDO::PARAM_STR);
$req3->bindParam('date_inscription', $date_inscription, PDO::PARAM_STR);
$req3->bindParam('first_name', $first_name, PDO::PARAM_STR);
$req3->bindParam('last_name', $last_name, PDO::PARAM_STR);
$req3->bindParam('company', $company, PDO::PARAM_STR);
$req3->bindParam('driver_id', $driver_id, PDO::PARAM_INT);
$req3->execute();

我收到此错误:

  

(!)致命错误:带消息的未捕获异常'PDOException'   'SQLSTATE [23000]:完整性约束违规:1452无法添加或   更新子行:外键约束失败   (klkjljdriver_client_detail,CONSTRAINT   fk_driver_client_detail_driver1 FOREIGN KEY(driver_id)参考   driverdriver_id)在没有动作的情况下删除没有动作   第215行的C:\ wamp \ www \ klkjlj \ driver \ login.php

     

(!)PDOException:SQLSTATE [23000]:完整性约束违规:   1452无法添加或更新子行:外键约束失败   (klkjljdriver_client_detail,CONSTRAINT   fk_driver_client_detail_driver1 FOREIGN KEY(driver_id)参考   driverdriver_id)在没有动作的情况下删除没有动作   第215行的C:\ wamp \ www \ klkjlj \ driver \ login.php

0 个答案:

没有答案