我试图从一个窗体一次插入到两个表中。
表#1=浴室订单
表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')";
我研究过这个问题,但是。。我不明白我做错了什么。
它说要在语句中执行两个独立的insert,我想这就是我要做的。
有人能帮我理解两个单独的语句应该是什么样的吗。
泰亚
这是一个小提琴的链接
https://jsfiddle.net/fiveonadub/836n4ycf/2/
3条答案
按热度按时间ehxuflar1#
这里有两件事似乎出了问题。
首先,简单地将一个看起来像sql的字符串赋给一个变量并不能使它在服务器上执行sql,您需要调用一些方法或函数来实现这一点。
其次,我之所以知道这一点,是因为我快速地将代码复制/粘贴到ide中,但是$comments变量没有定义。
ghhkc1vu2#
我不明白您的php是如何跳过第一个insert-into命令并跳到第二个insert-into命令的。您没有使用mysqli\u query()函数来执行任何一个操作。将这两个查询的输出存储到两个不同的变量中,然后将该变量作为参数传递给mysqli\u query()。只有你的查询才会被执行。
-谢谢你,
gwbalxhn3#
你需要使用
mysqli_query()
. 这个mysqli_query()
函数对数据库执行查询。将第一个查询存储在
$sql1
排名第二$sql2
mysqli_query($conn, $sql1); mysqli_query($conn, $sql2);