trampolinejoe Posted November 23, 2008 Share Posted November 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133884-solved-for-loop-help-inserting-into-the-db-inside-a-loop/ Share on other sites More sharing options...
ratcateme Posted November 23, 2008 Share Posted November 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133884-solved-for-loop-help-inserting-into-the-db-inside-a-loop/#findComment-696929 Share on other sites More sharing options...
trampolinejoe Posted November 23, 2008 Author Share Posted November 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133884-solved-for-loop-help-inserting-into-the-db-inside-a-loop/#findComment-696931 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.