简体   繁体   English

如何在PHP和MySQL中一次将数据插入到两个或多个表中?

[英]How can I insert data into two or more tables at once in PHP and MySQL?

I am trying to instert data from an html form using php into mysql database. 我正在尝试使用php将html表单中的数据插入mysql数据库。 Only one table(customer table) is being inserted and not package table as well.Am asking for help on how I could insert into both tables at once. 仅插入一个表(客户表),而不插入包表。Am寻求帮助,我如何一次将其插入两个表中。 Here is the code...Please help 这是代码...请帮助

<?php
include "includes/connection.php";

//Customer Table
$firstName = $_POST['inputFirstName'];
$lastName = $_POST['inputLastName'];
$gender = $_POST['inputGender'];
$address = $_POST['inputAddress'];
$mobileNumber = $_POST['inputMobilePhone'];
$workAddress = $_POST['inputAddress'];
$age = $_POST['inputAge'];

//Package Information
$PackageName = $_POST['inputPackageName'];
$PackageWeight = $_POST['inputPackageWeight'];
$PackagePrice = $_POST['inputPackagePrice'];
$DepartureDestination = $_POST['inputDepartureDestination'];
$finalDestination = $_POST['inputFinalDestination'];
//$DeliveryOption = $_POST['inputDeliveryOption'];

//Receiver Information

$receiverFirstName = $_POST['inputReceiverFirstName'];
$receiverLastName = $_POST['inputReceiverLastName'];
$receiverAddress = $_POST['inputReceiverAddress'];
$receiverPhone = $_POST['inputReceiverPhone'];




if(!$_POST['submit']) {
    echo "Please fill out the form";
    header ('Location: user.php');
}
else {


        $sql = "INSERT INTO cus_sender (SenderID,StaffID,SenderFirstName,SennderLastName,Address,Phone,SEX,Age,Time)
                                        VALUES (NULL,NULL,:firstName,:lastName,:address,:mobileNumber,:gender,:age,'')";
        $sql2= "INSERT INTO package(PID,SenderID,StaffID,PackageName,PackageWeight,Price,DepartureTown,DeliveryTown,DeliveryMethod)
                                        VALUES (NULL,NULL,NULL,:PackageName,:PackageWeight,:PackagePrice,:DepartureDestination,:finalDestination)"; //Add delivery option

$q = $db->prepare($sql); 

$q->execute(array(':firstName'=>$firstName, 
             ':lastName'=>$lastName,
             ':address'=>$address,
             ':mobileNumber'=>$mobileNumber,
             ':gender'=>$gender,
             ':age'=>$age)); 

$q2 = $db->prepare($sql2); 

$q2->execute(array(':PackagePrice'=>$PackageName, 
             ':PackageWeight'=>$PackageWeight,
             ':PackagePrice'=>$PackagePrice,
             ':DepartureDestination'=>$DepartureDestination,
             ':finalDestination'=>$finalDestination));
             //':DeliveryOption'=>$DeliveryOption)); To be added later


echo "<p>Customer has been added!</p>";

header ('Location: http://localhost/BNW/newCustomer.php');


}
?>
By this way you can insert values in more than one table with single query.

<?php

$db = new mysqli('localhost', 'user', 'pass', 'test');

$start = microtime(true);
$a = 'a';
$b = 'b';

$sql = $db->prepare('INSERT INTO multi (a,b) VALUES(?, ?)');
$sql->bind_param('ss', $a, $b);
for($i = 0; $i < 10000; $i++)
{
    $a = chr($i % 1);
    $b = chr($i % 2);
    $sql->execute();
}
$sql->close();

echo microtime(true) - $start;

$db->close();

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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