SQL& PHP帮助!!从1个表格插入4个表格? UPDATE

时间:2016-03-17 04:09:59

标签: php mysql

我现在已经完成了所有的php和sql并且正确地插入到所有4个表中但是我得到from utils.db import * queryset.filter(mycolname__len__gte=10) 的错误但它仍然插入到该表中?谁能解释为什么会发生这种情况?

继承我的PHP代码`

NOTICE undefined index: product ON LINE 39

`

和Heres my html`

<?php

    $con = mysqli_connect('localhost', 'root', '');

    if(!$con)
    {
        echo'Not connected to Server';
    }

    if(!mysqli_select_db($con, 'horizonphotography'))

    {
        echo'DataBase Not Selected';
    }
/*personal */
    $firstname = mysqli_real_escape_string($con, $_POST['firstname']);
    $lastname = mysqli_real_escape_string($con, $_POST['lastname']);
    $mobile = mysqli_real_escape_string($con, $_POST['mobile']);
    $homephone = mysqli_real_escape_string($con, $_POST['homephone']);
    $email = mysqli_real_escape_string($con, $_POST['email']);
/*personal */

/*shipping */
    $region = mysqli_real_escape_string($con, $_POST['region']);
    $address = mysqli_real_escape_string($con, $_POST['address']);
    $city = mysqli_real_escape_string($con, $_POST['city']);
    $postcode = mysqli_real_escape_string($con, $_POST['postcode']);
/*shipping */

/*creditcard info */
    $nameoncard = $_POST['nameoncard'];
    $ccnumber = $_POST['ccnumber'];
    $month = $_POST['month'];
    $year = $_POST['year'];
    $code = $_POST['code'];
/*credit card info */

/*Product info */
    $product = $_POST['product'];
/*product info */

/*personal */
    $sql1 = "INSERT INTO paymentPersonal (firstname, lastname, mobile, homephone, email) VALUES ('$firstname', '$lastname', '$mobile', '$homephone', '$email')";

/*shipping */
     $sql2 = "INSERT INTO paymentsShipping (address, region, city, postcode) VALUES ('$region', '$address', '$city', '$postcode')";

/*credit card details*/
     $sql3 = "INSERT INTO paymentsPayment (nameoncard, ccnumber, year, month, code) VALUES ('$nameoncard', '$ccnumber', '$month', '$year', '$code')";

/*product details*/
     $sql4 = "INSERT INTO paymentsProduct (product) VALUES ('$product')";


   if(!mysqli_query($con,$sql1)) echo 'Not Inserted into paymentPersonal';
    else if(!mysqli_query($con,$sql2)) echo 'Not Inserted into paymentsShipping';
    else if(!mysqli_query($con,$sql3)) echo 'Not Inserted into paymentsPayment';
    else if(!mysqli_query($con,$sql4)) echo 'Not Inserted into paymentsProduct';
    else echo 'Thank You For Your Order! You Will recive a confirmaton E-Mail Shortly';

    header("refresh:3; url=shop.php");
?>  

TIA!

2 个答案:

答案 0 :(得分:1)

您的所有查询都已分配到$sql,因此只会插入最后一个查询,因为它会覆盖之前的$sql

您需要将其更改为$sql1$sql2$sql3$sql4

/*personal */
    $sql1 = "INSERT INTO paymentPersonal (firstname, lastname, mobile, homephone, email) VALUES ('$firstname', '$lastname', '$mobile', '$homephone', '$email')";

/*shipping */
     $sql2 = "INSERT INTO paymentsShipping (address, region, city, postcode) VALUES ('$region', '$address', '$city', '$postcode')";

/*credit card details*/
       $sql3 = "INSERT INTO paymentsPayment (nameoncard, ccnumber, year, month, code) VALUES ('$nameoncard', '$ccnumber', '$month', '$year', '$code')";

/*product details*/
     $sql4 = "INSERT INTO paymentsProduct (product) VALUES ('$product')";


    if(!mysqli_query($con,$sql1)) echo 'Not Inserted into paymentPersonal';
    else if(!mysqli_query($con,$sql2)) echo 'Not Inserted into paymentsShipping';
    else if(!mysqli_query($con,$sql3)) echo 'Not Inserted into paymentsPayment';
    else if(!mysqli_query($con,$sql4)) echo 'Not Inserted into paymentsProduct';
    else echo 'Thank You For Your Order! You Will recive a confirmaton E-Mail Shortly';

这将回显未插入的表名,否则它将回显'谢谢您的订单! ......'。

重要:您需要阻止MySQL注入,例如下面:

$firstname = mysqli_real_escape_string($con, $_POST['firstname']);

答案 1 :(得分:0)

除了luweiqi的答案,你还需要执行所有四个查询...

if(!mysqli_query($con,$sql1) || !mysqli_query($con,$sql2) || !mysqli_query($con,$sql3) || !mysqli_query($con,$sql4))
    {
        echo 'Not Inserted';
    }

此方法的问题在于,如果一个或多个查询失败,则显示“未插入”。您可能希望设置更精细的内容以检查每个查询的成功/失败并相应地响应用户。但是,除此之外,使用编号查询和多次执行将完成工作。