如何使用PHP和MYSQL将数据插入两个不同的表中

时间:2019-01-02 13:14:34

标签: php mysql

我正在尝试使用PHP和MySQL将数据插入到两个不同的表中,但是数据仅插入到第二个表中。有人可以向我解释原因并帮助我解决问题。所以我读了这个StackOverflow solution,但是不确定这是否是面向对象风格的PHP,因为我没有运气使它对我有用。

<?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>";
}


?>

3 个答案:

答案 0 :(得分:1)

您有两次

$link->prepare

第二个覆盖第一个

答案 1 :(得分:0)

例如Mabrouki Fakhri建议:

// 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);
  }

答案 2 :(得分:0)

您应该有两个不同的语句(即stmt1,stmt2),并准备,绑定它们。然后在两个不同的语句上同时执行它们。