Jump to content

[help]undefined variable when saving session data to DB


Diether

Recommended Posts

hi guys Good day, , i have two data from same form that i want to insert in the different table in my database.

1. data came from the form( firstname,lastname,phonenumber etc.)

2. data came from session variable(item id, quantity, etc)

 

im getting this error, please help me to solve this. thanks

Notice: Undefined variable: itemId in C:\xampp\htdocs\sample\delivery_details.php on line 16

 

Notice: Undefined variable: qty in C:\xampp\htdocs\sample\delivery_details.php on line 17

 

Notice: Undefined variable: itemid in C:\xampp\htdocs\sample\delivery_details.php on line 19

 

Notice: Undefined variable: qty in C:\xampp\htdocs\sample\delivery_details.php on line 19

 

the code i use:

   <?php
   if (isset($_POST['submit'])){
   $firstName = $_POST['firstName'];
   $lastName = $_POST['lastName'];
   $phoneNumber = $_POST['phoneNumber'];
   $emailAddress = $_POST['emailAddress'];
   $Address = $_POST['Address'];
   $query = mysql_query("INSERT INTO customer (id, first_name, last_name, phone_number,email_address,address)
									    VALUES ('','$firstName', '$lastName',$phoneNumber,'$emailAddress','$Address')");

   $getID = mysql_query("SELECT id FROM customer where first_name = '$firstName' and last_name  = '$lastName'");

   $num_row = mysql_num_rows($getID);
   if($num_row !=0){
	    while($row = mysql_fetch_array($getID)){
				 $_SESSION["cart_array"]["item_id"] = $itemId;
				 $_SESSION["cart_array"]["quantity"] = $qty;
				 $sql1 = mysql_query("INSERT INTO order (id,quantity,item_id)
									    VALUES ('',$itemid,$qty )");
	    }

	    }
   }
   ?>

Link to comment
Share on other sites

Heres my problem. i have two data from same form that i want to insert in the different table in my database.

1. data came from the form( firstname,lastname,phonenumber etc.)

2. data came from session variable(item id, quantity, total etc)

when the customer clicks the FINISH button, the data will be insert in the two separate tables in the database.

 

Here's my screenshot of my form:

http://img6.uploadho...96ab1f360bd.jpg

 

that code above is the solution to my problem.

Link to comment
Share on other sites

You got 1 of 3.

 

Your code still doesn't make sense for your described problem. I think you need to use mysql_last_insert_id(), and not run a select and a loop.

You should have TWO INSERTS and that's it.

 

As it says, you haven't defined $qty of $itemId. Where are they coming from?

 

However, I can't follow the logic of your code. What are you trying to do?

 

Finally, you should never run queries in loops. 

Link to comment
Share on other sites

Assuming that the session data is set, you are assigning those variables incorrectly. I believe you meant this:


$itemId = $_SESSION["cart_array"]["item_id"];
$qty = $_SESSION["cart_array"]["quantity"];

 

Also, in your query, $itemid should be $itemId, as variable names are case sensitive.

 

Hopefully this gets you on the right track. And don't forget to include session_start(); at the beginning of your scripts that are making use of sessions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.