0

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', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' );
d joe
  • 45
  • 1
  • 7
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – aynber Oct 12 '17 at 20:21
  • 1
    `product_3_name`, etc, does not exist in your $_POST. Make sure that the names are correct in your form and exist in your post. Additionally, you're open for SQL injection and quoting headaches. Since you're using mysqli, take advantage of [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [bind_param](http://php.net/manual/en/mysqli-stmt.bind-param.php). **This will take care of any pesky quoting issues that may occur.** – aynber Oct 12 '17 at 20:22
  • 1
    From look of things, you might need to rethink your database design. Also SQL injection is around the corner – Rotimi Oct 12 '17 at 20:24
  • @Akintunde hello :) What do you mean by redesign the database? In what way? – d joe Oct 12 '17 at 20:27
  • 1
    Would highly recommend against creating a column for each product. If you add a new product you will have to update your application *and* database. You already have a productid column to uniquely identify your products so adding multiple columns for each one is unnecessary. Just have one name, price and quantity column. – Jacob H Oct 12 '17 at 20:28
  • @aynber hey :) They are correct in the form, however, if they do not exist if say the user picks 2 products, product 3-7 values will not exist. Hope this makes sense. So i need to figure a way to only run the post if the number is selected. So if 2 products are selected, only run two posts. – d joe Oct 12 '17 at 20:30

1 Answers1

0

You should add if (isset($_POST['product_1_name'])) before each variable assignment, to check if the value is empty or not.

Something like this :

$product_1_name = '';
if (isset($_POST['product_1_name'])) {
    $product_1_name = mysqli_real_escape_string($conn, $_POST['product_1_name']);
}
Altilus
  • 1
  • 1
  • It's now saying there's an error with the 'INSERT INTO' function – d joe Oct 12 '17 at 20:54
  • 1
    You did this for all your variables? I used the product_1_name variable in my exemple, but the problem appears to be for product_3, 4,5,6,7, – Altilus Oct 12 '17 at 20:56
  • Yes I did this for all. "Get Notice: Undefined variable: event_6_price in test.php on line 115 "(which is inside the INSERT INTO function). If I only enter information for 1 product, get this error for products 2-7. – d joe Oct 12 '17 at 20:58
  • I don't see this variable in your INSERT statement? because it seems like this variable doesn't exists (missing the $?) – Altilus Oct 12 '17 at 21:05
  • Ummm I dont understand the error either! Its referring to '$product_3_name', '$product_3_price', '$product_3_quantity' which is on row 113 – d joe Oct 12 '17 at 21:43