adrian28uk Posted June 23, 2007 Share Posted June 23, 2007 I am putting together a shopping cart for educational purposes. I decided to redo some of my earlier efforts as looking at the code, 10 lines could be put in to 4. You know what its like. I am sending the values from two drop down menus to an array. Then I am trying to use a for each loop to extract each number against the database which in turn should output the product option name, however I cannot get this to work. I'm seriously scratching my head here. The array is ready to go, I tested this by using print_r ($product_option); $product_option = $_POST['choice']; foreach ($product_option as $number) { include "dbconn.inc.php"; $dbcnx = @mysql_connect("$servername","$username", "$password"); if (!$dbcnx) { echo( "" ); exit(); } //Connect to database if (! @mysql_select_db("$database") ) { echo( "<P>Unable to locate the shopping basket " . "" ); exit(); } $query = mysql_query("SELECT * FROM products_options WHERE product_number=$number); while ( $row = mysql_fetch_array($query) ) { echo $row['product_option_name']; } } Look forward to your response. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 What specific error u get. And try to remove @ from ur code lines. It will show u the warning error. Change this line : $query = mysql_query("SELECT * FROM products_options WHERE product_number=$number); To this: $query = mysql_query("SELECT * FROM products_options WHERE product_number=$number) or die (mysql_error()); Quote Link to comment Share on other sites More sharing options...
adrian28uk Posted June 23, 2007 Author Share Posted June 23, 2007 I am getting no error. Just a blank screen. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 Can u post ur updated code. Quote Link to comment Share on other sites More sharing options...
adrian28uk Posted June 23, 2007 Author Share Posted June 23, 2007 Certainly. By the way that error checking is great. Thanks for the tip. I have updated some variables etc on the code below. Basically $product_choice will equal two numbers taken from the form for example 453,8721 Using these numbers, each one is put in to the foreach loop where in turn queries the database. So for example code 453 will print out Colour Red code 8721 will print out Size large Like I said before I can create the array, and when I print_r($array) I can see the information. The only problem I am having is that it is not getting any information from the database. If I replace $_POST['choice'] with two static numbers it works, but not when I post it from the form. Many many thanks. Code is below. // Code $product_choice = $_POST['choice']; foreach ($product_choice as $numbers) { include "dbconn.inc.php"; $dbcnx = @mysql_connect("$servername","$username", "$password"); if (!$dbcnx) { echo( "" ); exit(); } //Connect to database if (! @mysql_select_db("$database") ) { echo( "<P>Unable to locate the shopping basket " . "" ); exit(); } $query = mysql_query("SELECT * FROM Products_Options WHERE product_randid=$numbers") or die (mysql_error()); while ( $row = mysql_fetch_array($query) ) { echo $row['product_option_name']; } } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 23, 2007 Share Posted June 23, 2007 Ok first of all move your db connection code out side of the foreach. You don't want to make a new connection to the database every time you loop through the foreach loop. Just have the query within the loop. New code: <?php // we moved all the db connection/selection code outside of the foreach // don't wont to connect/select database each time we loop through the foreach include 'dbconn.inc.php'; $dbcnx = mysql_connect($servername, $username, $password) or die('Connection Failed'); mysql_select_db($database) or die('"Unable to locate the shopping basket'); $product_choice = $_POST['choice']; foreach ($product_choice as $numbers) { // Build the query $query = 'SELECT * FROM Products_Options WHERE product_randid=' . $numbers; // Run the query we built above $result = mysql_query($query, $dbcnx) or die (mysql_error()); // check that the query returned only one record if(mysql_num_rows($result) == 1) { // one record has been returned, lets get it. // no need for a while loop here as we are only getting one record // if multiple records are being returned we'd use a while loop $row = mysql_fetch_assoc($result); // display the returned product echo $row['product_option_name']; } } ?> Quote Link to comment Share on other sites More sharing options...
adrian28uk Posted June 25, 2007 Author Share Posted June 25, 2007 Never got a chance to say thank you very much for helping. The code did the trick. Brilliant. Quote Link to comment 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.