【发布时间】:2018-03-22 18:56:40
【问题描述】:
啊啊啊好混乱。因此,当我只使用一组 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', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' );
【问题讨论】:
-
顺便说一句:您的代码中似乎存在重大安全漏洞。您的
product_price应该来自数据库而不是来自用户表单,更不用说您没有使用 pdo -
请在问题中包含您遇到的错误。
-
我希望这只是示例代码,实际上并没有被用于任何东西......所以你是否回应了实际的 $sql 并检查了它并尝试通过 phpmyadmin 或其他方式直接在你的桌子上使用它你有数据库实用程序吗?
-
您的表格是否真的包含在 1 行中添加 7 个产品及其属性所需的所有列?您应该真正考虑规范化您的数据库结构。结合问题标题,您的代码似乎没有任何意义。
-
@jeroen 是的,确实如此:) 我还让他们接受 null 以便以防万一也可以填写其余部分。我已经添加了我用来制作数据库的代码
标签: php mysql sql phpmyadmin