Please Help! Submitting MySQL query from dropdown box
Posted: Mon Sep 26, 2011 11:46 am
I am new to PHP. I'm sure that this is a very basic problem, but it's been driving me crazy. Please Help!
The form in my html document looks like this:
---------------------------------------------------------------------
-----------------------------------------------------
My php doc (msql_connect2.php) looks like this:
---------------------------------------------------
--------------------------------------------
1. I have the database set up in MySQL.
2. If I input: mysql_query("INSERT INTO basic (first_name, last_name) VALUES ('$first_name', '$last_name')"); the database creates a new entry when the form is filled out for "First name and Last Name"
3. The database will not create a new entry if I include a query for list as shown in the php example above.
4. When logged into phpMyAdmin I am able to create a database entry which includes whatever option I choose from "list"
5. How can I make it so that when a user fills out the form and selects an option from the list, their choice is inserted into the form "basic"?
Any help would be very much appreciated! Thank you in advance:o)
Jane
The form in my html document looks like this:
---------------------------------------------------------------------
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="msql_connect2.php" method="post">
<p>First Name:
<input type="text" name="first_name" /><br /><br />
Last Name: <input type="text" name="last_name" /><br /><br />
<label for="list">List:</label>
<select name="list" id="list">
<option value="bob">bob</option>
<option value="jo">jo</option>
<option value="jane">jane</option>
</select>
<br /><br />
<input type="submit" />
<br />
</form>
</body>
</html>
My php doc (msql_connect2.php) looks like this:
---------------------------------------------------
Code: Select all
<?php
$db_host = "localhost";
$db_username = "user";
$db_pass = "password";
$db_name = "database";
$connect = mysql_connect ("$db_host","$db_username","$db_pass") or die("Could not connet to MySQL");
mysql_select_db("$db_name") or die ("No database");
if($connect){
echo "yes";
}else{
echo "no";
}
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$list = $_POST["list"];
mysql_select_db("$database", $connect);
if($connect){
echo "connected to db";
}else{
echo "no not connected to db";
}
mysql_query("INSERT INTO basic (first_name, last_name, list) VALUES ('$first_name', '$last_name', '$list')");
?>1. I have the database set up in MySQL.
2. If I input: mysql_query("INSERT INTO basic (first_name, last_name) VALUES ('$first_name', '$last_name')"); the database creates a new entry when the form is filled out for "First name and Last Name"
3. The database will not create a new entry if I include a query for list as shown in the php example above.
4. When logged into phpMyAdmin I am able to create a database entry which includes whatever option I choose from "list"
5. How can I make it so that when a user fills out the form and selects an option from the list, their choice is inserted into the form "basic"?
Any help would be very much appreciated! Thank you in advance:o)
Jane