PHP:不能在一些MySQL表中插入多条记录

时间:2018-05-24 09:56:45

标签: php mysqli relational-database

我的基于网络的CRM软件项目存在问题,我可以管理一个人的工作,联系信息和公司信息。第一个输入按预期工作,但从第二个输入开始,没有新记录插入jobcompany表。

<?php
  require_once '../conn.php';


//defining variables...



if($_SERVER["REQUEST_METHOD"] == "POST"){


  //validation of input


}

  if(empty($err_first_name) && empty($err_last_name) && empty($err_media_type) && empty($err_priority)){


    mysqli_autocommit($conn, FALSE);

    $sql1 = "INSERT INTO person (first_name, last_name, media_type, magazine_sub, newsletter_sub, birthday, priority, remark, tel, mobile, fax, email, wechat)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        if ($stmt1 = mysqli_prepare($conn, $sql1) or die(mysqli_error($conn))) {
          mysqli_stmt_bind_param($stmt1, 'sssiissssssss', $param_first_name, $param_last_name, $param_media_type, $param_magazine_sub, $param_newsletter_sub, $param_birthday, $param_priority, $param_remark, $param_tel, $param_mobile, $param_fax, $param_email, $param_wechat);

          $param_first_name = $first_name;
          $param_last_name = $last_name;
          $param_media_type = $media_type;
          $param_magazine_sub = $magazine_sub;
          $param_newsletter_sub = $newsletter_sub;
          $param_birthday = $birthday;
          $param_priority = $priority;
          $param_remark = $remark;
          $param_tel = $tel;
          $param_mobile = $mobile;
          $param_fax = $fax;
          $param_email = $email;
          $param_wechat = $wechat;
        }
          mysqli_stmt_execute($stmt1);
          $person_id = mysqli_insert_id($conn);
          mysqli_stmt_close($stmt1);



    $sql2 = "INSERT INTO address (type, street, city, state, zip, country)
             VALUES (?, ?, ?, ?, ?, ?)";

        if ($stmt2 = mysqli_prepare($conn, $sql2) or die(mysqli_error($conn))) {
          mysqli_stmt_bind_param($stmt2, 'ssssss', $param_address_type, $param_street, $param_city, $param_state, $param_zip, $param_country);

          $param_address_type = $address_type;
          $param_street = $street;
          $param_city = $city;
          $param_state = $state;
          $param_zip = $zip;
          $param_country = $country;
        }

        mysqli_stmt_execute($stmt2);
        $address_id = mysqli_insert_id($conn);
        mysqli_stmt_close($stmt2);



  $sql3 = "INSERT INTO company (name_de)
           VALUES (?)";
      if ($stmt3 = mysqli_prepare($conn, $sql3) or die(mysqli_error($conn))){
        mysqli_stmt_bind_param($stmt4, 's', $param_name_de);

        $param_name_de = $name_de;
      }

      mysqli_stmt_execute($stmt3);
      $company_id = mysqli_insert_id($conn);
      mysqli_stmt_close($stmt3);


  $sql4 = "INSERT INTO job (person_id, company_id, department, position)
           VALUES ($person_id, $company_id, ?, ?)";
      if ($stmt4 = mysqli_prepare($conn, $sql4) or die(mysqli_error($conn))){
        mysqli_stmt_bind_param($stmt4, 'ss', $param_dep, $param_pos);

        $param_dep = $department;
        $param_pos = $position;
      }

      mysqli_stmt_execute($stmt4);
      mysqli_stmt_close($stmt4);

  $sql5 = "INSERT INTO person_address (person_id, address_id)
           VALUES ($person_id, $address_id)";

  mysqli_query($conn, $sql5);

  header("location: person.php");

  }
  mysqli_commit($conn);
  mysqli_close($conn);

}

 ?>


/*HTML-Form*/

如果有帮助,这是数据库的关系模式:

enter image description here

受影响的表的表定义:

CREATE TABLE IF NOT EXISTS `chkdcrm`.`job` (
  `job_id` INT(11) NOT NULL AUTO_INCREMENT,
  `person_id` INT(11) NOT NULL,
  `company_id` INT(11) NOT NULL,
  `department` VARCHAR(45) NOT NULL,
  `position` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`job_id`, `company_id`, `person_id`),
  CONSTRAINT `job_person_id`
    FOREIGN KEY (`person_id`)
    REFERENCES `chkdtest`.`person` (`person_id`)
    ON DELETE CASCADE,
  CONSTRAINT `company_id`
    FOREIGN KEY (`company_id`)
    REFERENCES `chkdtest`.`company` (`company_id`)
    ON DELETE CASCADE)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `chkdcrm`.`company` (
  `company_id` INT(11) NOT NULL AUTO_INCREMENT,
  `name_de` VARCHAR(150) NOT NULL UNIQUE,
  `website` VARCHAR(255) NULL UNIQUE,
  `c_tel` VARCHAR(20) NOT NULL UNIQUE,
  `c_email` VARCHAR(150) NOT NULL UNIQUE,
  `service_region` ENUM('EU', 'Western Europe', 'Germany and German-speaking region', 'Others') NOT NULL,
  `registration_nr` VARCHAR(15) NULL UNIQUE,
  `c_remark` TEXT NULL,
  `date_added` DATETIME NOT NULL DEFAULT NOW(),
  `member_type` ENUM('board', 'counsil', 'class a', 'class b', 'automotive counsil', 'support', 'none') NOT NULL,
  `industry` ENUM(//blablabla) NOT NULL,
  `parent_company` INT(11) NULL,
  PRIMARY KEY (`company_id`),
  CONSTRAINT `parent_company`
    FOREIGN KEY (`parent_company`)
    REFERENCES `chkdtest`.`company` (`company_id`)
    ON DELETE SET NULL)
ENGINE = InnoDB;

1 个答案:

答案 0 :(得分:0)

我从UNIQUE表中删除了所有company约束,因为表单中的输入只将name_de插入表中,默认情况下所有其他唯一字段都设置为NULL通过第二个条目将有2个NULL字段,它与UNIQUE约束冲突。

但我不确定删除UNIQUE约束会对我数据库的完整性产生多大影响。