Jump to content

[SOLVED] for loop help / inserting into the db inside a loop


Recommended Posts

Hey Guys,

 

I really need the help of somebody out there as i am so awful at coding. :-X

 

Basically I am creating a order form which has a multi-select field, I need to post multiple variables to the database if the user selects more then one form the multi select box.

 

I have a table in my database that stores the orderID and the productID so that seems to work and they can order as many products as they want.

 

So I made something such as this.

 

	for($i=0; $i <= count($product); $i++){

$product = $_POST['product'][$i]; 

//sql insert query add a row to the orderedProduct table
	$query = "INSERT INTO OrderProducts(OrderId, ProductId) VALUES ('$connote','$product')";
		  

     //Store the order inside the database. 
         mysql_query($query) or die("PRODUCT ORDER You're submition has encountered an error."); 

      echo "$query";//TESTING TO CHECK


}	

 

If they select more then one item it works perfectly fine.

 

If they select only a single product then it's abit more of a problem.

 

the loop will run the following mysql queries

 

INSERT INTO OrderProducts(OrderId, ProductId) VALUES ('TZA122743145','150')
INSERT INTO OrderProducts(OrderId, ProductId) VALUES ('TZA122743145','')

 

So, it seems to post another row but not actually give a productId. (because only a single product is in the array as only one product is selected)

 

The idea is that it should only post as many rows into the table as the user has ordered.

 

The confusing thing is;

If $i is equal to 0 and the size of the array is more then that then it should add 1 to $i so that it equals 1 and then run the loop.

Once the loop has run once it should then check to see again and it will see that $i is equal to the size of the array and run again. (presuming 1 item is selected)

 

So as i figured this wont work (my current example) I tried to make it so that the loop only runs if $i less then the array size. (so the loop should only run once)

 

for($i=0; $i < count($product); $i++)

 

But that isnt running at all! I just get a blank page. ???

 

As you may have notice i am echoing my query to see what its exactly running.

 

I really hope somebody can help me. ::)

 

 

 

where does product come from?

should it be

for($i=0; $i < count($_POST['product']); $i++)

and also you have no check being done to stop SQL injections add a

$product = $_POST['product'][$i]; 

should be

$product = mysql_real_escape_string($_POST['product'][$i]); 

 

Scott.

 

Scott!

 

Thanks for your help champ, I see my mistake now, i am also reading up about checking to stop sql injections after your suggestion.

 

Thanks again, it all works well now.

 

cheers,

Joe.

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.