Home » Php » PHP: Can't insert more than one record into some of the MySQL table

PHP: Can't insert more than one record into some of the MySQL table

Posted by: admin June 30, 2018 Leave a comment

Questions:

I am having problem with my web-based CRM software project, where I can manage a person’s job, contact information and company information. The first input works as intended, but starting from the second input, only the address record gets updated. Where is the problem?

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


//defining variables...



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


  if(empty(trim($_POST["first_name"]))){
      $err_first_name = 'Please enter contacts first name!';
  }
  else {
    $first_name = trim($_POST["first_name"]);
  }


  if(empty(trim($_POST["last_name"]))){
      $err_last_name = 'Please enter contacts last name!';
  }
  else {
    $last_name = trim($_POST["last_name"]);
  }


  if(empty(trim($_POST["name_de"]))){
      $err_name_de = "Please enter contact's company!";
  }
  else {
    $name_de = trim($_POST["name_de"]);
  }

  if(empty(trim($_POST["department"]))){
      $err_department = "Please enter contact's department!";
  }
  else {
    $department = trim($_POST["department"]);
  }

  if(empty(trim($_POST["position"]))){
      $err_position = "Please enter contact's position!";
  }
  else {
    $position = trim($_POST["position"]);
  }

  if(isset($_POST["media_type"]) && $_POST["media_type"] == 'null'){
    $err_media_type = "Please select which type of media this contact is!";
  }
  else {
    $media_type = $_POST["media_type"];
  }

  $magazine_sub = isset($_POST["magazine_sub"]) ? 1:0;

  $newsletter_sub = isset($_POST["newsletter_sub"]) ? 1:0;


  $birthday = date("Y-m-d", strtotime($_POST["birthday"]));


  if(isset($_POST["priority"]) && $_POST["priority"] == 'not_selected'){
    $err_priority = "Please assign the priority level!";
  }
  else {
    $priority = $_POST["priority"];
  }

  $remark = $_POST["remark"];

  $tel = trim($_POST["tel"]);


  if(empty(trim($_POST["mobile"]))){
    $err_mobile = "Please enter contact's mobile phone number!";
  }
  else {
    $mobile = trim($_POST["mobile"]);
  }

  if(empty(trim($_POST["email"]))){
    $err_email = "Please enter contact's email address!";
  }
  else {
    $email = trim($_POST["email"]);
  }

  $wechat = trim($_POST["wechat"]);

  $fax = trim($_POST["fax"]);


  if(isset($_POST["type"]) && $_POST["type"] == 'x'){
    $err_address_type = "Please choose address type!";
  }
  else {
    $address_type = $_POST["type"];
  }

  if(empty(trim($_POST["street"]))){
    $err_street = "Please enter a valid address!";
  }
  else {
    $street = trim($_POST["street"]);
  }

  if(empty(trim($_POST["city"]))){
    $err_city = "Please enter a valid city!";
  }
  else {
    $city = trim($_POST["city"]);
  }

  if(empty(trim($_POST["state"]))){
    $err_state = "Please enter a valid province or state!";
  }
  else {
    $state = trim($_POST["state"]);
  }

  if(empty(trim($_POST["zip"]))){
    $err_zip = "Please enter the ZIP code!";
  }
  else {
    $zip = trim($_POST["zip"]);
  }

  if(empty(trim($_POST["country"]))){
    $err_country = "Please enter a valid country!";
  }
  else {
    $country = trim($_POST["country"]);
  }

  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)) {
          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)) {
          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);



  $sql4 = "INSERT INTO company (name_de)
           VALUES (?)";
      if ($stmt4 = mysqli_prepare($conn, $sql4)){
        mysqli_stmt_bind_param($stmt4, 's', $param_name_de);

        $param_name_de = $name_de;
      }

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


  $sql5 = "INSERT INTO job (person_id, company_id, department, position)
           VALUES ($person_id, $company_id, ?, ?)";
      if ($stmt5 = mysqli_prepare($conn, $sql5)){
        mysqli_stmt_bind_param($stmt5, 'ss', $param_dep, $param_pos);

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

      mysqli_stmt_execute($stmt5);
      mysqli_stmt_close($stmt5);

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

  mysqli_query($conn, $sql6);

  header("location: person.php");

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

}

 ?>


/*HTML-Form*/

If it helps, here is the relational schema of the database:

enter image description here

Answers: