# Insert Dropdown list data into MySQL



## inplaytoday

Hi, I'm unable to find any info for this on Google.

I know how to use php and an html form to write text data to a database, but how can this be done with a pre-populated (using database) Select Dropdown List.

I need a registered user to be able to:

1. Select A Value from displayed Dropdown List.
2. Use PHP to send that selection to a different Table in my database (MySQL).

There are plenty of tutorials on the net about how to populate dropdown lists, but none that tell how to 'grab' a selected value and record it into a database.

Here is the code I've used so far:
Code:

Volvo
Saab
Fiat
Audi

Using update.php that looks like this:
Code:
<?php
$Car = $_POST['cars']

mysql_connect ('localhost', 'username', 'password') or die ('Error: ' .mysql_error());
mysql_select_db ("data_base");

$query="INSERT INTO user (FirstName)VALUE ('".$Car."')";
mysql_query($query) or die ('Error submitting');

echo "You Chose: " .$Car ;

?>


----------



## sonexpc

so your code works OK?

and if you want everything done in the same page .... you can use javascript to grap the value and use AJAX to forward to the result page...


----------



## inplaytoday

thanks for the response sonexpc. it turns out the only problem with my code was just missing one ';' which anyone in this forum could have told me with a quick look since it was in the first few lines.

im planning to use AJAX to make the form more dynamic/professional, studying IBM's guides on it right now...


----------



## spinster

inplaytoday said:


> thanks for the response sonexpc. it turns out the only problem with my code was just missing one ';' which anyone in this forum could have told me with a quick look since it was in the first few lines.
> 
> im planning to use AJAX to make the form more dynamic/professional, studying IBM's guides on it right now...


hi there!

how did you got it to work? I've studied your problem since I have it too! I think I found one missing semicolon but I still can't get it to work... Care to share your solution? Thanks.


----------



## curious_anubhav

Hi there,
I am making an issue tracking system in php Mysql using Wamp 5.x 
I have two forms - add issue and edit issue.
Add issue form has 11 attributes. Edit issue form has 4 attributes. My database table has 11 attributes. 
I have following problems :
1. In the add issue and edit issue forms, there are a couple of dropdown boxes. The data from the dropdown boxes when comes into the database isn't really in text form but in number form (position in the dropdown). how to make it capture the text from dropdown?

2.How do I add the functionality for searching for issues by ID number (primary key) and by district or by type which are the fields in the database table.

3. How do I get the entire table entries into a web page for viewing for the public? although I am using authentication for users. As in the entire list of issues with all its columns.

Hoping for someone to resolve these asap.
Thanks in advance.


----------



## spinster

<?php // starting php 
// If using .css file you can use the div-sections e.g. by writing this into your .css: #remove { place your css definitions here }
echo "
*Remove something from your database*
";

// Making a function that handles the db connection.
// You can configure your phpMyAdmin/database priviledges and set some
// user, pwd, db and server information. Just make sure they match these or the connection will fail. 
// This function can be put to its own file and we could just call it like this:
// require("yourFile.php"); 
// and it would replace the below 'make_dbConn()' -function.
function make_dbConn() {

$db_user = "your_user";
$db_pwd = "your_pwd";
$db_database = "your_db";
$db_server = "your_server e.g. localhost";

// Try to connect into MySQL db.
$my_db_Conn = mysql_connect($db_server,$db_user,$db_pwd);

// Testing the connection.
if(!$my_db_Conn) 
die("Failed." .mysql_error());

// Testing the selection of db.
if(!mysql_select_db($db_database, $my_db_Conn ))
die("'my_databse' selection failed." .mysql_error());

// Returning the db conn.
return $my_db_Conn ;
}

// Lets put the make_dbConn(); -function - database connection - into the $myConn; -variable.
$myConn=make_dbConn();

// Using MySQL to fetch data from database.
// Database: some_database
// Database table: some_databaseTable
// Database fields: data_ID, web_site, user
function get_fromDB($data_ID, $web_site, $user){
// Fetching the data into the $list -variable.
$list = mysql_query("SELECT * FROM some_databaseTable ORDER BY data_ID");

// Quick check.
if(!$list) die("DB Select Failed: ".mysql_error());

// Returns the $list
return $list;
}

// FORM.
// Note: Form name must match to one in input types name.
echo "";
// Simple php/html table:
echo "
Select data to be removed from database:";
// Fetch the data from fields: 'data_ID', 'web_site', 'user' into the $list -variable.
$list = get_fromDB('data_ID', 'web_site', 'user');
// While there are rows in db...
while ($row = mysql_fetch_array($list)) {
// Showing the data in dropdown.
// In this case we want to show the 'web_site' field from the database.
echo "$row[web_site]";
} 
// Closing the dropdown select
echo "
";
echo "
";
// Dropdown - select the removed data
echo "
";

// Opening the dropdown select with optional 'style' -attributes.
echo "
";

// ...closing database connection.
mysql_close($myConn);

// Couple of brakes and submit section and closing the table & form.
echo "

";
echo "
";
echo "
";
echo "";
echo "";

// Don't forget to close your php skript.
?>


----------

