douling0053 2012-06-22 21:43
浏览 136
已采纳

尝试根据下拉列表结果运行SELECT sql查询。 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. 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

  • 写回答

4条回答 默认 最新

  • dtrz17917 2012-06-22 21:50
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi