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

27,731
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 http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/ if you need more info on that just ask.

Share:
27,731
Rob Hatton
Author by

Rob Hatton

Updated on November 04, 2020

Comments

  • Rob Hatton
    Rob Hatton over 3 years

    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. Right...

    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 WHERE date = $ XXXXXDROPDOWNLIST XXX $

    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.

    Code

    mysql_select_db($database_rcc, $rcc);
    $query_dropdowndate = "SELECT DATE_FORMAT(date, '%M %Y') AS FORMATTEDDATE FROM news GROUP BY FORMATTEDDATE ORDER BY Date DESC  ";
    $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">
        <?php
    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>
        <?php
    } 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);
      }
    ?>
      </select>
      <input type="submit" name="submit" id="submit" value="Submit" />
    </form>
    <p>&nbsp;</p>
    <form id="form1" name="form1" method="get" action="">
      <table border="0" cellpadding="5" cellspacing="2">
        <tr>
          <td>Date</td>
          <td>Subject</td>
          <td>Message</td>
        </tr>
        <?php do { ?>
          <tr>
            <td><?php echo $row_newsitems['Date']; ?></td>
            <td><?php echo $row_newsitems['Subject']; ?></td>
            <td><?php echo $row_newsitems['Message']; ?></td>
          </tr>
          <?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 Date, Subject, Message FROM news WHERE date = $_POST['dropdowndate']"; (THIS BIT IS PROBABLY WRONG)

    Rob

  • Rob Hatton
    Rob Hatton almost 12 years
    Thanks for the input so far, I appreciate your help - even though I appear to be making a right dog's dinner of this php page!
  • 1321941
    1321941 almost 12 years
    @RobHatton I just read it, the part I am unsure of is, are you trying to do this without refreshing the page(AJAX) or does that not matter and you just want to refresh the form as normal. If you tell me that I can be more helpful :)
  • tira
    tira over 7 years
    can i ask question here ? its really urgent to me :'(