尝试根据下拉列表结果运行 SELECT sql 查询。mysql php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11164533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to run a SELECT sql query based on the dropdown list result. mySQL php
提问by Rob Hatton
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...
我已经有大约 3 年没有设计网站了,所以至少可以说我很生疏。我必须依靠 Dreamweaver CS5 来帮助我。对...
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.)
我想要一个新闻页面,用户/客户将从下拉菜单中选择日期(JAN、FEB、MAR、APR 等...)现在,我的 mySQL 数据库中有一个名为“新闻”的表,其中每一行由这些日期引用。我已经为日期设置了动态列表(下拉列表。)
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:
我想要的是让客户从下拉列表中选择日期,并将结果显示在下面的 Recordset 中。我假设 SQL 查询需要按照以下方式编写:
SELECT date, subject, message FROM news WHERE date = $ XXXXXDROPDOWNLIST XXX $
从新闻中选择日期、主题、消息 WHERE 日期 = $ 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.
正如你所看到的,我做了最后一行,因为我不太明白它应该如何运作。我认为下拉列表需要采用 POST 的形式,结果表需要采用 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> </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.
其中一些可能看起来很奇怪,让我解释一下...动态列表(下拉列表)称为“dropdowndate”,表单称为“choosedate”。有一个名为“submit”的按钮可以提交表单。FORMATTEDDATE 是记录集的名称,它为下拉菜单提供一个动态列表。
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)
我希望在用户 POST 时将该动态列表中的值插入到我提到的查询中...... SELECT Date, Subject, Message FROM news WHERE date = $_POST['dropdowndate']";(这可能是错误的)
Rob
抢
采纳答案by 1321941
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.
$_POST 变量包含表单发布时发送的所有数据。字段值应与您为选择字段指定的名称相对应。
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.
如果您希望在不刷新的情况下获取数据,则需要使用 AJAX,以下解释得很好http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using -jquery/如果您需要更多信息,请询问。
回答by JCarlos
Do you want the records to be loaded when the user select something from the drop down, or you want it to be loaded with the page?
您是否希望在用户从下拉列表中选择某些内容时加载记录,还是希望它与页面一起加载?
In the first case you need to use Ajax, to bring the records in the background.
在第一种情况下,您需要使用 Ajax 将记录置于后台。
In the other case, you can do a statement like the one you posted when the page is loading.
在另一种情况下,您可以执行与加载页面时发布的声明类似的声明。
回答by tira
$select = $_POST['dropdowndate'];
$sql = "SELECT * FROM table WHERE attribute = '$select'" or die (mysql_error());
回答by tira
An example of an HTML dropdown
HTML 下拉列表的示例
<form method="POST">
<select name="animal">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
</select>
</form>
An example how to catch the information with php
一个如何用php捕获信息的例子
<?php
echo $_POST['animal'];
?>

