Rob Hatton Rob Hatton - 11 months ago 39
MySQL Question

Trying to run a SELECT sql query based on the dropdown list result. mySQL php

I haven't designed a website for about 3 years now, so I am quite rusty to say the least. I have to fall back on Dreamweaver CS5 to help me out.

I want a page for news, and the user/customer will select from a dropdown menu the date (JAN, FEB, MAR, APR etc...) Now, I have a table in my mySQL database called 'news' where each row is referenced by these dates. I have already set up a Dynamic List for the date (a dropdown list.)

What I want is for the customer to select the date from the dropdown, and for the results to show in a Recordset underneath. I am assuming that the SQL query needs to be wrote something along the lines of:

SELECT date, subject, message
FROM news

As you can see, I made the last line up because I can't quite grasp how it should function. I am thinking that the dropdown list needs to be in a form which will POST and the table of results needs to be in a form which will GET.

Could somebody more technical than me please enlighten my dillema?

Thanks, Rob.


mysql_select_db($database_rcc, $rcc);
$dropdowndate = mysql_query($query_dropdowndate, $rcc) or die(mysql_error());
$row_dropdowndate = mysql_fetch_assoc($dropdowndate);
$totalRows_dropdowndate = mysql_num_rows($dropdowndate);

mysql_select_db($database_rcc, $rcc);
$query_newsitems = "SELECT `Date`, Subject, Message FROM news WHERE date = $_POST['dropdowndate']";
$newsitems = mysql_query($query_newsitems, $rcc) or die(mysql_error());
$row_newsitems = mysql_fetch_assoc($newsitems);
$totalRows_newsitems = mysql_num_rows($newsitems);

<form id="choosedate" name="choosedate" method="post" action="#">
<label for="dropdowndate"></label>
<select name="dropdowndate" id="dropdowndate">
do {
<option value="<?php echo $row_dropdowndate['FORMATTEDDATE']?>"<?php if (!(strcmp($row_dropdowndate['FORMATTEDDATE'], $row_dropdowndate['FORMATTEDDATE']))) {echo "selected=\"selected\"";} ?>><?php echo $row_dropdowndate['FORMATTEDDATE']?></option>
} while ($row_dropdowndate = mysql_fetch_assoc($dropdowndate));
$rows = mysql_num_rows($dropdowndate);
if($rows > 0) {
mysql_data_seek($dropdowndate, 0);
$row_dropdowndate = mysql_fetch_assoc($dropdowndate);
<input type="submit" name="submit" id="submit" value="Submit" />
<form id="form1" name="form1" method="get" action="">
<table border="0" cellpadding="5" cellspacing="2">
<?php do { ?>
<td><?php echo $row_newsitems['Date']; ?></td>
<td><?php echo $row_newsitems['Subject']; ?></td>
<td><?php echo $row_newsitems['Message']; ?></td>
<?php } while ($row_newsitems = mysql_fetch_assoc($newsitems)); ?>

Some of this may look weird so let me explain... The dynamic list (dropdown) is called 'dropdowndate' and the form is called 'choosedate' There is a button called 'submit' to submit the form. FORMATTEDDATE is the name given to the recordset which gives the dropdown menu a dynamic list.

I want the value from that dynamic list when user's POST, to insert into the query as i mentioned ... SELECT
, Subject, Message FROM news WHERE date = $_POST['dropdowndate']"; (THIS BIT IS PROBABLY WRONG)


Answer Source
SELECT date, subject, message FROM news WHERE date = $_POST['fieldvalue']

The $_POST variable contains all the data sent when the form is posted. The field value should correspond to the name you give the select field.

This would be open to injection, so please ensure you use reasonable security measures.

If you wish to get the data without refreshing, you will need to use AJAX the following explains it beautifully if you need more info on that just ask.