用一种形式插入2个表,其中2个sql语句不起作用

时间:2018-09-16 14:55:02

标签: php mysql sql mysqli

我正在尝试从一种形式一次插入2个表中。
表#1 = bath_orders
表#2 =客户
我显然正在尝试同时建立电子邮件列表。

PHP跳过第一个INSERT INTO命令,然后跳到第二个,并继续执行页面上的其余代码。

每次执行时,我都会在错误日志文件中找到以下几种错误:

  

PHP注意:未定义变量:在线

我搜索了该主题,发现在大多数情况下,人们会错误地尝试

$sql = "INSERT INTO bath_orders, customers ...." 

但这不是我的代码中的情况。
我的代码如下:

<?php
$servername = "localhost";
$username = "my_user_name";
$password = "my_password";
$dbname = "my_database";

//  connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check 
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

if(isset($_POST['submit'])){


$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$phone = mysqli_real_escape_string($conn, $_POST['phone']);
$custaddress = mysqli_real_escape_string($conn, $_POST['custaddress']);
$city = mysqli_real_escape_string($conn, $_POST['city']);
$state = mysqli_real_escape_string($conn, $_POST['state']);
$zip = mysqli_real_escape_string($conn, $_POST['zip']);
$pic1 = mysqli_real_escape_string($conn, $_POST['pic1']);
$pic2 = mysqli_real_escape_string($conn, $_POST['pic2']);
$pic3 = mysqli_real_escape_string($conn, $_POST['pic3']);
$startdate = mysqli_real_escape_string($conn, $_POST['startdate']);
$walltiledemo = mysqli_real_escape_string($conn, $_POST['walltiledemo']);
$walltile = mysqli_real_escape_string($conn, $_POST['walltile']);
$otherwalltile = mysqli_real_escape_string($conn, $_POST['otherwalltile']);
$otherwalltileh = mysqli_real_escape_string($conn, $_POST['otherwalltileh']);
$otherwalltilew = mysqli_real_escape_string($conn, $_POST['otherwalltilew']);
$walltileinst = mysqli_real_escape_string($conn, $_POST['walltileinst']);
$pandemo = mysqli_real_escape_string($conn, $_POST['pandemo']);
$paninst = mysqli_real_escape_string($conn, $_POST['paninst']);
$flrtileinst = mysqli_real_escape_string($conn, $_POST['flrtileinst']);
$flrtile = mysqli_real_escape_string($conn, $_POST['flrtile']);
$otherflrtile = mysqli_real_escape_string($conn, $_POST['otherflrtile']);
$tub = mysqli_real_escape_string($conn, $_POST['tub']);
$newtub = mysqli_real_escape_string($conn, $_POST['newtub']);
$othertub = mysqli_real_escape_string($conn, $_POST['othertub']);
$door = mysqli_real_escape_string($conn, $_POST['door']);
$newdoor = mysqli_real_escape_string($conn, $_POST['newdoor']);
$otherdoor = mysqli_real_escape_string($conn, $_POST['otherdoor']);
$floortiledemo = mysqli_real_escape_string($conn, $_POST['floortiledemo']);
$floortileinst = mysqli_real_escape_string($conn, $_POST['floortileinst']);
$floortile = mysqli_real_escape_string($conn, $_POST['floortile']);
$otherfloortile = mysqli_real_escape_string($conn, $_POST['otherfloortile']);
$otherfloortileh = mysqli_real_escape_string($conn, $_POST['otherfloortileh']);
$otherfloortilew = mysqli_real_escape_string($conn, $_POST['otherfloortilew']);
$vanity = mysqli_real_escape_string($conn, $_POST['vanity']);
$newvanity = mysqli_real_escape_string($conn, $_POST['newvanity']);
$othervanity = mysqli_real_escape_string($conn, $_POST['othervanity']);




$sql = "INSERT INTO bath_orders (firstname, lastname, email, phone , custaddress , city , state , zip , pic1 , pic2 , pic3 ,  startdate , walltiledemo , walltile , otherwalltile , otherwalltileh , otherwalltilew , walltileinst , pandemo , paninst , flrtileinst , flrtile , otherflrtile , tub , newtub , othertub , door , newdoor , otherdoor , floortiledemo , floortileinst , floortile , otherfloortile , otherfloortileh , otherfloortilew , vanity , newvanity , othervanity)
VALUES ('$fname', '$lname', '$email', '$phone', '$custaddress' , '$city' , '$state' , '$zip' , '$pic1' , '$pic2' , '$pic3' , '$startdate' , '$walltiledemo' , '$walltile' , '$otherwalltile' , '$otherwalltileh' , '$otherwalltilew' , '$walltileinst' , '$pandemo' , '$paninst' , '$flrtileinst' , '$flrtile' , '$otherflrtile' , '$tub'  , '$newtub' , '$othertub' , '$door' , '$newdoor' , '$otherdoor' , '$floortiledemo' , '$floortileinst' , '$floortile' , '$otherfloortile' , '$otherfloortileh' , '$otherfloortilew' , '$vanity' , '$newvanity' , '$othervanity' )";

$sql = "INSERT INTO customers (fname, lname, email, phone , custaddress , city , state , zip , comments )
VALUES ('$fname', '$lname', '$email', '$phone', '$custaddress' , '$city' , '$state' , '$zip' , '$comments')";

我已经研究了这个主题,但是..我不明白我在做什么错。
它说要执行2个单独的INSERT INTO语句,我认为这就是我正在做的事情。
有人可以帮助我了解2个单独的语句应该是什么样子。
TYIA

这里是小提琴的链接

https://jsfiddle.net/fiveonadub/836n4ycf/2/

3 个答案:

答案 0 :(得分:0)

您需要用户mysqli_query()mysqli_query()函数对数据库执行查询。

将第一个查询存储在$sql1中,将第二个查询存储在$sql2

mysqli_query($conn, $sql1); mysqli_query($conn, $sql2);

答案 1 :(得分:0)

我不明白您的php如何跳过第一个INSERT INTO命令并跳至第二个。您没有使用mysqli_query()函数。将这两个查询的输出存储到2个不同的变量中,然后将该变量作为参数传递给mysqli_query()。然后只有您的查询将被执行。

-谢谢

答案 2 :(得分:0)

这里似乎有两件事出错。

首先,仅向变量分配一个看起来像sql的字符串并不能使它在服务器上执行sql,您需要调用一些方法或函数来实现该目的。

第二,我只知道这一点,因为我快速将代码复制/粘贴到IDE中,但是未定义$ comments变量。