简体   繁体   中英

How to insert data into two different tables using PHP and MYSQL

I am trying to insert data into two different tables using PHP and MySQL but the data is only inserted into the second table only. Can someone please explain to me why and help me fix the problem. So I read this StackOverflow solution but am not sure if this is Object Oriented style PHP because I had no luck making it work for me.

<?php
//outputing php errors in development
error_reporting(-1);
ini_set('display_errors', 1);

//adding database config
include_once 'config.php';


//processing post data
if(isset($_POST) && !empty($_POST))
{
// variables for input data

            $company_name = $_POST['company_name'];
            $company_address = $_POST['company_address'];
            $name_of_vessel = $_POST['name_of_vessel'];
            $official_vessel_number = $_POST['official_vessel_number'];
            $port_of_registry = $_POST['port_of_registry'];
            $gross_tonnage = $_POST['gross_tonnage'];
            $imo_number = $_POST['imo_number'];
            $call_sign = $_POST['call_sign'];
            $permit_number = $_POST['permit_number'];
            $date_of_issue_op = $_POST['date_of_issue_op'];
            $date_of_expiry_op =$_POST ['date_of_expiry_op'];
            $date_of_expiry_sp = $_POST['date_of_expiry_sp'];
            $date_of_issue_sp =$_POST ['date_of_issue_sp'];

// SQL query for inserting data into the database



      $stmt = $link->prepare("INSERT INTO `customers` (`company_name`,`company_address`,`name_of_vessel`,`official_vessel_number`, `port_of_registry`, `gross_tonnage`, `imo_number`, `call_sign`, `permit_number`) VALUES (?,?,?,?,?,?,?,?,?)");

      $stmt->bind_param('sssssssss',$company_name, $company_address, $name_of_vessel, $official_vessel_number, $port_of_registry, $gross_tonnage, $imo_number, $call_sign, $permit_number);

$stmt = $link->prepare("INSERT INTO `permits` (`date_of_issue_op`, `date_of_expiry_op`, `date_of_expiry_sp`, `date_of_issue_sp`) VALUES (?,?,?,?)");

      $stmt->bind_param('ssss',$date_of_issue_op , $date_of_expiry_op, $date_of_expiry_sp, $date_of_issue_sp );
      if($stmt->execute()){
            echo "<span style='background-color:green; padding:6px; color:white; font-size:16px;'>Permit created successfully </span>";
      }else{
            echo "<p align=center>Error inserting data.</p>";
            echo mysqli_error($link);
      }

}else{
       echo "<p align=center>Empty form submitted</p>";
}


?>

You have two times

$link->prepare

and the second overwrite the first

As Mabrouki Fakhri suggested, for example:

// sql query for inserting data into database



$stmt = $link->prepare("INSERT INTO `customers` (`company_name`,`company_address`,`name_of_vessel`,`official_vessel_number`, `port_of_registry`, `gross_tonnage`, `imo_number`, `call_sign`, `permit_number`) VALUES (?,?,?,?,?,?,?,?,?)");

$stmt->bind_param('sssssssss',$company_name, $company_address, $name_of_vessel, $official_vessel_number, $port_of_registry, $gross_tonnage, $imo_number, $call_sign, $permit_number);

$stmt1 = $link->prepare("INSERT INTO `permits` (`date_of_issue_op`, `date_of_expiry_op`, `date_of_expiry_sp`, `date_of_issue_sp`) VALUES (?,?,?,?)");

$stmt1->bind_param('ssss',$date_of_issue_op , $date_of_expiry_op, $date_of_expiry_sp, $date_of_issue_sp );
  if($stmt->execute() && $stmt1->execute()){
        echo "<span style='background-color:green; padding:6px; color:white; font-size:16px;'>Permit created successfully </span>";
  }else{
        echo "<p align=center>Error inserting data.</p>";
        echo mysqli_error($link);
  }

You should have two different statements, (ie stmt1 , stmt2 ) and prepare, bind both of them. then execute both of them on two different statements also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM