将多行同时传递给sql数据库时出错

时间:2017-10-11 07:38:15

标签: php mysql sql phpmyadmin

aahhhh这么令人困惑。因此,当我只使用一组product_1_name,product_1_price,product_1_quantity执行一组时,脚本运行正常!它增加了5作为产品ID,其他一切都被添加到数据库中。其他位置保留为空。

然而,当我一次性添加其他变量,即产品1到7时,脚本不会执行!数据库保持为空并且不会添加到表中。我花了2个小时检查语法错误,但无法找到拼写错误。这与您可以添加的项目数相关吗?

$a = 5;
$productid = $a;


    $product_1_name = mysqli_real_escape_string($conn, $_POST['product_1_name']);   //uid
    $product_1_price = mysqli_real_escape_string($conn, $_POST['product_1_price']); //uid
    $product_1_quantity = mysqli_real_escape_string($conn, $_POST['product_1_quantity']);   //uid

    $product_2_name = mysqli_real_escape_string($conn, $_POST['product_2_name']);   //uid
    $product_2_price = mysqli_real_escape_string($conn, $_POST['product_2_price']); //uid
    $product_2_quantity = mysqli_real_escape_string($conn, $_POST['product_2_quantity']);   //ui3

    $product_3_name = mysqli_real_escape_string($conn, $_POST['product_3_name']);   //uid
    $product_3_price = mysqli_real_escape_string($conn, $_POST['product_3_price']); //uid
    $product_3_quantity = mysqli_real_escape_string($conn, $_POST['product_3_quantity']);   //uid

    $product_4_name = mysqli_real_escape_string($conn, $_POST['product_4_name']);   //uid
    $product_4_price = mysqli_real_escape_string($conn, $_POST['product_4_price']); //uid
    $product_4_quantity = mysqli_real_escape_string($conn, $_POST['product_4_quantity']);   //uid

    $product_5_name = mysqli_real_escape_string($conn, $_POST['product_5_name']);   //uid
    $product_5_price = mysqli_real_escape_string($conn, $_POST['product_5_price']); //uid
    $product_5_quantity = mysqli_real_escape_string($conn, $_POST['product_5_quantity']);   //uid

    $product_6_name = mysqli_real_escape_string($conn, $_POST['product_6_name']);   //uid
    $product_6_price = mysqli_real_escape_string($conn, $_POST['product_6_price']); //uid
    $product_6_quantity = mysqli_real_escape_string($conn, $_POST['product_6_quantity']);   //uid

    $product_7_name = mysqli_real_escape_string($conn, $_POST['product_7_name']);   //uid
    $product_7_price = mysqli_real_escape_string($conn, $_POST['product_7_price']); //uid
    $product_7_quantity = mysqli_real_escape_string($conn, $_POST['product_7_quantity']);   //uid






        $sql = "INSERT INTO products (userid, productname, venue, date, productdescription, name) VALUES ('$userid','$productname', '$venue', '$date', '$productdescription', '$name');";
        mysqli_query($conn, $sql);

        $sql = "INSERT INTO productproducts (productid, product_1_name, product_1_price, product_1_quantity, 
                            product_2_name, product_2_price, product_2_quantity, 
                            product_3_name, product_3_price, product_3_quantity, 
                            product_4_name, product_4_price, product_4_quantity, 
                            product_5_name, product_5_price, product_5_quantity, 
                            product_6_name, product_6_price, product_6_quantity, 
                            product_7_name, product_7_price, product_7_quantity) 
                            VALUES (
                            '$productid','$product_1_name', '$product_1_price', '$product_1_quantity',
                            '$product_2_name', '$product_2_price', '$product_2_quantity', 
                            '$product_3_name', '$product_3_price', '$product_3_quantity', 
                            '$product_4_name', '$product_4_price', '$product_4_quantity',  
                            '$product_5_name', '$product_5_price', '$product_5_quantity', 
                            '$product_6_name', '$product_6_price', '$product_6_quantity', 
                            '$product_7_name', '$product_7_price', '$product_7_quantity' 

                            );";

        mysqli_query($conn, $sql);

        header("Location: ../members.php?product=success");
        exit();



} else {
    header("Location: ../signup.php");
    exit();
}






?>

我以前制作数据库的代码:

CREATE TABLE productevents (
    o_id int(11) not null AUTO_INCREMENT PRIMARY KEY,
    productid int(11),
    product_1_name varchar(256),
    product_1_price int(11),
    product_1_quantity int(11),

    product_2_name varchar(256),
    product_2_price int(11),
    product_2_quantity int(11),

    product_3_name varchar(256),
    product_3_price int(11),
    product_3_quantity int(11),

    product_4_name varchar(256),
    product_4_price int(11),
    product_4_quantity int(11),


    product_5_name varchar(256),
    product_5_price int(11),
    product_5_quantity int(11),

    product_6_name varchar(256),
    product_6_price int(11),
    product_6_quantity int(11),

    product_7_name varchar(256),
    product_7_price int(11),
    product_7_quantity int(11)





);

echosql结果:

Notice: Undefined index: product_3_name in website.xx.xxon line 29

Notice: Undefined index: product_3_price in website.xx.xxon line 30

Notice: Undefined index: product_3_quantity in website.xx.xxon line 31

Notice: Undefined index: product_4_name in website.xx.xxon line 33

Notice: Undefined index: product_4_price in website.xx.xxon line 34

Notice: Undefined index: product_4_quantity in website.xx.xxon line 35

Notice: Undefined index: product_5_name in website.xx.xxon line 37

Notice: Undefined index: product_5_price in website.xx.xxon line 38

Notice: Undefined index: product_5_quantity in website.xx.xxon line 39

Notice: Undefined index: product_6_name in website.xx.xxon line 41

Notice: Undefined index: product_6_price in website.xx.xxon line 42

Notice: Undefined index: product_6_quantity in website.xx.xxon line 43

Notice: Undefined index: product_7_name in website.xx.xxon line 45

Notice: Undefined index: product_7_price in website.xx.xxon line 46

Notice: Undefined index: product_7_quantity in website.xx.xxon line 47
INSERT INTO productevents (productid, product_1_name, product_1_price, product_1_quantity, product_2_name, product_2_price, product_2_quantity, product_3_name, product_3_price, product_3_quantity, product_4_name, product_4_price, product_4_quantity, product_5_name, product_5_price, product_5_quantity, product_6_name, product_6_price, product_6_quantity, product_7_name, product_7_price, product_7_quantity) VALUES ( '5','product1', '1', '1', 'product2', '2', '2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' );

2 个答案:

答案 0 :(得分:0)

这是针对您的特定问题。

  1. 你从来没有按照我的要求回复$ sql并将其复制并粘贴到phpmyadmin来测试SQL。也许我不够清楚。这是懒人(包括我自己)的标准调试
  2. 如果你有...你会发现你的SQL语句是......注意我已经为你的变量使用了虚拟值......

    INSERT INTO productproducts (
      productid, 
      product_1_name,product_1_price,product_1_quantity,
      product_2_name, product_2_price,product_2_quantity, 
      product_3_name, product_3_price,product_3_quantity, 
      product_4_name, product_4_price,product_4_quantity, 
      product_5_name, product_5_price,product_5_quantity, 
      product_6_name, product_6_price,product_6_quantity, 
      product_7_name, product_7_price,product_7_quantity) 
      VALUES ( 
      '1',
      'One', '1', '1',
      'Two', '2', '2',
      'Three', '3', '3', 
      'Four', '4', '4', 
      'Five', '5', '5', 
      'Six', '6', '6', 
      'Seven', '7', '7' 
    )
    

    现在您会发现它会生成错误,并且在密切观察时您没有名为 productproducts的表,您已将其称为 productevents

    所以你的表名错了......

    1. 什么是价格?
    2. 是整数(整数)吗?必须是因为您已将价格定义为整数。那么你怎么阻止人们进入4.99美元呢?什么阻止某人进入蓬松的兔子

        

      验证所有用户输入至关重要。

      1. 您的数据库结构是另一个问题......

答案 1 :(得分:-1)

你在查询结束前添加了额外的分号。你应该尝试

$sql = "INSERT INTO productproducts (productid, product_1_name, product_1_price, product_1_quantity, 
                        product_2_name, product_2_price, product_2_quantity, 
                        product_3_name, product_3_price, product_3_quantity, 
                        product_4_name, product_4_price, product_4_quantity, 
                        product_5_name, product_5_price, product_5_quantity, 
                        product_6_name, product_6_price, product_6_quantity, 
                        product_7_name, product_7_price, product_7_quantity) 
                        VALUES (
                        '$productid','$product_1_name', '$product_1_price', '$product_1_quantity',
                        '$product_2_name', '$product_2_price', '$product_2_quantity', 
                        '$product_3_name', '$product_3_price', '$product_3_quantity', 
                        '$product_4_name', '$product_4_price', '$product_4_quantity',  
                        '$product_5_name', '$product_5_price', '$product_5_quantity', 
                        '$product_6_name', '$product_6_price', '$product_6_quantity', 
                        '$product_7_name', '$product_7_price', '$product_7_quantity' 

                        )";

希望它适合你。