I am trying to pull information from a drop down list (pulling from one MySQL table) and insert that selected item, along with some other information, into another MySQL table.
The drop down list appears, I can select an item and hit submit, and it appears as though it goes through. However, when I look at the DB, all items are inserted into the DB EXCEPT the one I selected from the drop down list.... I'm sure it's something simple, but I'm stumped...
Database setup:
--
-- Table structure for table academy_main
DROP TABLE IF EXISTS academy_main;
CREATE TABLE IF NOT EXISTS academy_main (
id int(11) NOT NULL auto_increment,
course_title varchar(150) collate latin1_general_ci default NULL,
course_desc text collate latin1_general_ci,
location_st varchar(20) collate latin1_general_ci default NULL,
location_co varchar(75) collate latin1_general_ci default NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=14 ;
--
-- Table structure for table academy_sched
DROP TABLE IF EXISTS academy_sched;
CREATE TABLE IF NOT EXISTS academy_sched (
id int(11) NOT NULL auto_increment,
course_title varchar(150) collate latin1_general_ci default NULL,
course_date date default NULL,
location_st varchar(20) collate latin1_general_ci NOT NULL,
location_co varchar(20) collate latin1_general_ci NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=31 ;
Here is my PHP Code for the main page:
<?php
// open the connection
$conn = mysql_connect("localhost", "username", "password");
// pick the database to use
mysql_select_db("test",$conn);
// create the SQL statement
$sql = "SELECT course_title FROM academy_main";
// execute the SQL statement
$result = mysql_query($sql, $conn) or die(mysql_error());
//go through each row in the result set and display data
$options="";
while ($row=mysql_fetch_array($result))
{
$id=$row["id"];
$title=$row["course_title"];
$options.="<option VALUE=\"$id\">".$title.'</option>';
}
?>
<body>
<form action="insert2.php" method="post">
<SELECT NAME=id>
<OPTION VALUE=0>Please choose course to schedule
<?=$options?>
</SELECT>
<br />
<br />
Date of Course (YYYY-MM-DD): <br /><input type="text" name="course_date"><br>
<input type="hidden" name="location_st" value="az"><br>
<input type="hidden" name="location_co" value="maricopa"><br>
<input type="Submit">
</form>
</body>
Here is the code for the page that is called from the FORM POST:
<?
$username="username";
$password="password";
$database="test";
$title=$_POST['course_title'];
$date=$_POST['course_date'];
$state=$_POST['location_st'];
$county=$_POST['location_co'];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO academy_sched VALUES ('','$title','$date','$state','$county')";
mysql_query($query);
mysql_close();
if(!$query){
echo 'There was a error inserting the data into the database';
} else {
echo 'Your course has been added';
echo $options;
}
?>
Thanks so much!!