This is a different issue and NOT a duplicate please dont mark as duplicate!
Hello :) I am passing on user input obtained from a form. This form has options from 1 to 7. If the user picks 1 they get one product which they enter a name,price and quantity. If they select two, they enter information for two products...and so on....seven you enter information for seven products
Everything works upto the point of passing through the database. However, is only works if all 7 are passed. If you enter just one product information set, it doesn't work..even when the fields are able to be 'null'.
I need some guidance with the $product_1_name, $product_1_price and $product_1_quantity. I need to make them pass through to the database. The code freaks out when there are no values passed onto the functions.
Can I please have some assistance?
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();
ERROR FROM 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', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' );