这是一个不同的问题,不重复,请不要标记为重复!
你好:)我传递从表单获得的用户输入。此表单有1到7的选项。如果用户选择1,他们会得到一个产品,他们输入一个名称,价格和数量。如果他们选择两个,他们输入两个产品的信息......等等......七,你输入七个产品的信息
一切都可以通过数据库。但是,仅在通过所有7个时才有效。如果您只输入一个产品信息集,则它不起作用。即使字段可以为“空”。
我需要使用$ product_1_name,$ product_1_price和$ product_1_quantity的一些指导。我需要让它们传递到数据库。当没有值传递给函数时,代码就会变得怪异。
我可以请一些帮助吗?
SQL SET UP CODE:
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)
);
CODE:
if (isset($_POST['submit'])) {
include_once 'dbh.inc.php';
$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 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 (
'$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);
echo $sql;
exit();
错误来自ECHO $ SQL:
Notice: Undefined index: product_3_name in test.php on line 29
Notice: Undefined index: product_3_price in test.php on line 30
Notice: Undefined index: product_3_quantity in test.php on line 31
Notice: Undefined index: product_4_name in test.php on line 33
Notice: Undefined index: product_4_price in test.php on line 34
Notice: Undefined index: product_4_quantity in test.php on line 35
Notice: Undefined index: product_5_name in test.php on line 37
Notice: Undefined index: product_5_price in test.php on line 38
Notice: Undefined index: product_5_quantity in test.php on line 39
Notice: Undefined index: product_6_name in test.php on line 41
Notice: Undefined index: product_6_price in test.php on line 42
Notice: Undefined index: product_6_quantity in test.php on line 43
Notice: Undefined index: product_7_name in test.php on line 45
Notice: Undefined index: product_7_price in test.php on line 46
Notice: Undefined index: product_7_quantity in test.php on 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', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' );
答案 0 :(得分:0)
您应该在每个变量赋值之前添加if (isset($_POST['product_1_name']))
,以检查该值是否为空。
这样的事情:
$product_1_name = '';
if (isset($_POST['product_1_name'])) {
$product_1_name = mysqli_real_escape_string($conn, $_POST['product_1_name']);
}