Jump to content

Drop down box + MySQL


TheHaloEffect

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/52523-drop-down-box-mysql/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/52523-drop-down-box-mysql/#findComment-259229
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.