将数据插入多个不同的数据表

时间:2019-02-14 23:53:52

标签: php mysql

我试图将数据插入多个数据表中。它仅适用于单个数据表,我想知道如何将数据插入两个数据表中。在过去的几个小时中,我一直在努力解决此问题,但似乎无法深入了解它。如果有人有任何建议,请告诉我。 :)

    <?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost","ivodatat","","");


if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Inputs for security 
$fname = mysqli_real_escape_string($link, $_REQUEST['fname']);
$sname = mysqli_real_escape_string($link, $_REQUEST['sname']);
$address = mysqli_real_escape_string($link, $_REQUEST['address']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);
$phone = mysqli_real_escape_string($link, $_REQUEST['phone']);
$mac = mysqli_real_escape_string($link, $_REQUEST['mac']);
$installer = mysqli_real_escape_string($link, $_REQUEST['installer']);
$status = mysqli_real_escape_string($link, $_REQUEST['status']);

// Insert Query 
$sql1 = "INSERT INTO leadlist (fname, sname, address, email, phone, mac, installer, status) VALUES ('$fname', '$sname', '$address', '$email', '$phone', '$mac', '$installer', '$status')";

$sql2 = "INSERT INTO $installer (fname, sname, address, email, phone, mac, installer, status) VALUES ('$fname', '$sname', '$address', '$email', '$phone', '$mac', '$installer', '$status')";



if (mysqli_multi_query($link, $sql1, $sql2)){



                mysqli_close($conn);
                header("Location: installercontrol.php");




                exit;

} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close The Connection
mysqli_close($link);
?>

1 个答案:

答案 0 :(得分:0)

要使用mysqli_multi_query,您需要将查询附加到彼此,因为它只需要一个查询参数。从手册中:

  

执行一个或多个由分号连接的查询。

尝试以下方法:

mysqli_multi_query($link, $sql1 . ';' . $sql2)

您可能还应该更新错误消息:

echo "ERROR: Could not able to execute $sql1;$sql2. " . mysqli_error($link);