Hi there,
I came upon this thread while looking for code for a project I am completing for university. I have made a form with a drop down menu so that the user can query the database about which course is available in each month. However I keep getting this error message
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /Applications/xampp/xamppfiles/htdocs/paul/month_drop_down.php on line 41
The php code I actually got from this page: http://www.webmasterworld.com/forum88/4831.htm I have tried to adapt it to suit my purposes and it seems that all my database information is correct but for the likes of me I cannot work out why the php code is not processing the form data from the drop down menu.
Would anyone here please be able to have a quick look at my code and see if there is anything that may be stopping it from functioning?
There are two fields in my table which are called 'month' and 'course', the table itself is called months.
Any help would be greatly appreciated!
Form code:
<form name="month_search" method="post" action="month_drop_down.php"><table width="600" align="center" border="0" cellspacing="1" cellpadding="2">
<tr><td width=300><select name="month" id="selCourse" style="width: 200px;"><option value="Select month to attend..." selected>Select month to attend...</option><option value="jan">January</option><option value="feb">February</option><option value="mar">March</option><option value="apr">April</option><option value="may">May</option><option value="jun">June</option><option value="jul">July</option><option value="aug">August</option><option value="sep">September</option><option value="oct">October</option><option value="nov">November</option><option value="dec">December</option></select></td><td><input type="submit" name="search" value="Search"/></td>
</tr>
</table></form>
PHP code:
<?php
// get variable after selecting something from the dropdown with name 'month'
$select = $_POST['search'];
// if something has been chosen
if (!empty($select)) {
// get the chosen value
$month = $_POST['month'];
// select the type from the database
// database connection details (change to whatever you need)
$HOST = 'localhost';
$DATABASE = 'sworphe_paul';
$USER = 'sworphe_paul';
$PASSWORD = 'sworphe_paul';
// connect to database
if(!$conn=mysql_connect('localhost','sworphe_paul' ,'sworphe_paul')) {
echo("<li>Can't connect to $HOST as $USER");
echo("<li>mysql Error: ".mysql_error());
die;
}
// select database
if (!mysql_select_db($DATABASE,$conn)) {
echo("<li>We were unable to select database $DATABASE");
die;
}
// if everything successful create query
// this selects all rows where the type is the one you chose in the dropdown
// means that it will select all columns, ie name and type as i said above
$sql_query = "SELECT FROM months WHERE type='$month'";
// get the data from the database
$result = mysql_query($sql_query,$conn);
// output data
while (($deails = mysql_fetch_assoc($result))) {
// print out the name
echo('Courses available in '.$details['month'].' - ');
// print out the type
echo('Type: '.$details['course'].'<br>');
}
// close mysql connection
mysql_close($conn);
}
?>
Thank you so much in advance! 😕