TheHaloEffect Posted May 22, 2007 Share Posted May 22, 2007 I've got a database named: nutrition_db I've got a table in there named: brands and inside that I have: brand I've made a script which allows a user to add food to a list, but I'd like a drop down box which has a list of all the brands from my database in, in alphabetical order. I've googled everywhere and searched around on here, but I can't find the solution that suites me. Can anyone help me with this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/52523-drop-down-box-mysql/ Share on other sites More sharing options...
taith Posted May 22, 2007 Share Posted May 22, 2007 echo '<select>'; $query=mysql_query("SELECT * FROM `brands` ORDER BY `brand`"); while($row=mysql_fetch_assoc($query)){ echo '<option>'.$row[brand].'</option>'; } echo '</select>'; Quote Link to comment https://forums.phpfreaks.com/topic/52523-drop-down-box-mysql/#findComment-259172 Share on other sites More sharing options...
TheHaloEffect Posted May 22, 2007 Author Share Posted May 22, 2007 Wow...thanks for the uber speedy responce! The brands don't show in the list though Quote Link to comment https://forums.phpfreaks.com/topic/52523-drop-down-box-mysql/#findComment-259182 Share on other sites More sharing options...
TheHaloEffect Posted May 22, 2007 Author Share Posted May 22, 2007 On second thought....The box appears...but the brands don't. Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/52523-drop-down-box-mysql/#findComment-259189 Share on other sites More sharing options...
TheHaloEffect Posted May 22, 2007 Author Share Posted May 22, 2007 I've renamed the column to brands, and updated the other scripts, and it all works fine, i can submit new brands into the database, and display the contents of the database into another script...but the drop down box still does bugger all Below is the addfood script without the drop down box added to it <html> <head><title>Submit Food</title></head> <body> <?php $self = $_SERVER['PHP_SELF']; $food = $_POST['food']; $amount = $_post['amount']; $energy = $_POST['energy']; $protein = $_POST['protein']; $carbs = $_POST['carbs']; $carbsats = $_POST['carbsats']; $fat = $_POST['fat']; $fatsats = $_POST['fatsats']; $fibre = $_POST['fibre']; $sodium = $_POST['sodium']; $salt = $_POST['salt']; if( ( $food == NULL ) ){ $form ="<h4>Add Food</h4>"; $form.="<form action=\"$self\""; $form.=" method=\"post\">Food name: "; $form.="<input type=\"text\" name=\"food\""; $form.=" value=\"$food\"><br> Amount(g): "; $form.="<input type=\"text\" name=\"amount\""; $form.=" value=\"$amount\"><br> Energy(kcal): "; $form.="<input type=\"text\" name=\"energy\""; $form.=" value=\"$energy\"><br>Protein(g): "; $form.="<input type=\"text\" name=\"protein\""; $form.=" value=\"$protein\"><br>Carbohydrate: "; $form.="<input type=\"text\" name=\"carbs\""; $form.=" value=\"$carbs\"><br> of which sugars: "; $form.="<input type=\"text\" name=\"carbsats\""; $form.=" value=\"$carbsats\"><br> Fat: "; $form.="<input type=\"text\" name=\"fat\""; $form.=" value=\"$fat\"><br> of which saturates: "; $form.="<input type=\"text\" name=\"fatsats\""; $form.=" value=\"$fatsats\"><br> Fibre: "; $form.="<input type=\"text\" name =\"fibre\""; $form.=" value=\"$fibre\"><br> Sodium: "; $form.="<input type=\"text\" name=\"sodium\""; $form.=" value=\"$sodium\"><br> Salt: "; $form.="<input type=\"text\" name=\"salt\""; $form.=" value=\"$salt\"><br><br>"; $form.="<input type=\"submit\" value=\"submit!\">"; $form.="</form>"; echo $form; } else { // MySQL details $mysql_host="localhost"; $mysql_user="test"; $mysql_pass="test"; $mysql_dbname="nutrition_db"; //Connect to MySQL $conn = @mysql_connect( $mysql_host, $mysql_user, $mysql_pass ) or die("Could not connect to MySQL"); //Selects the database $db = @mysql_select_db( $mysql_dbname, $conn ) or die("Could not select database"); //creates the query $sql = "insert into nutrition (food, amount, energy, protein, carbs, carbsats, fat, fatsats, fibre, sodium, salt) values ('".$food."', '".$iron."', '".$energy."', '".$protein."', '".$carbs."', '".$carbsats."', '".$fat."', '".$fatsats."', '".$fibre."', '".$sodium."', '".$salt."')"; $result = @mysql_query( $sql, $conn ) or die("Could not execute query"); if( $result !== FALSE) echo( "$food added" ); } ?> <br> <a href="index.php">Back</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/52523-drop-down-box-mysql/#findComment-259229 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.