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
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
Share on other sites

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.