php - 使用PHP在mysql中拉取数据显示搜索表单结果

标签 php mysql database forms function

一旦用户填写了“main_search”表单,就会出现一个结果列表,具体取决于他们在上一页中的选择。要么弹出整个列表,要么什么都没有。下面,我的 HTML 从这里开始,然后我需要将结果填充到下一页。请,请,请帮助!!

如需引用,请查看此网站:www.lemassif.com

<form name="main_search" action="displaydata.php" id="nl-form" class="nl-form" method="post">

                <select name="main1">
                    <option value="featured" selected>any city</option>
                    <option value="city1">City 1</option>
                </select>

                <select name="reason">
                    <option value="family" selected>family</option>
                    <option value="romantic">romantic</option>
                    <option value="business">business</option>
                    <option value="leisure">leisure</option>
                </select>

                <select name="budget">
                    <option value="modest" selected>modest</option>
                    <option value="moderate">moderate</option>
                    <option value="lavish">lavish</option>
                </select>

                <div class="nl-submit-wrap">
                    <button class="nl-submit" type="submit" name="submit">Create my trip</button>
                </div>

这是我的 displaydata.php 页面,应该会在下一页填充过滤结果。

<?php

// Include the connection file.
include("func.inc.php");
$sql = mysql_query("SELECT * FROM venues");

if(isset($_POST['submit'])) {

$search_term = mysql_real_escape_string($_POST['$venues']);

$sql .= "WHERE city = '{$search_term}'";
$sql .= "OR reason = '{$search_term}'";
$sql .= "OR budget = '{$search_term}'";

}

$query = mysql_query($sql) or die(mysql_error());

    ?>

<table cellpadding="5" cellspacing="5">

<tr>
    <td><strong>Venue Name</strong></td>
    <td><strong>Image</strong></td>
    <td><strong>Description</strong></td>
</tr>

<?php 

    while($row = mysql_fetch_array($sql)) { ?>

<tr>
    <td><?php echo $row['venue_name']; ?></td>
    <td><?php echo $row['image']; ?></td>
    <td><?php echo $row['description']; ?></td>
</tr>

<?php } 
?>

</table>

这是我的 func.inc.php 文件。

<?php

include_once 'connection.php';

function close(){
    mysql_close();
}

function city_query(){

$myData = mysql_query("SELECT city FROM venues");
while($record = mysql_fetch_array($myData)){
    echo '<option value="' . $record['city'] . '">' . $record['city'] . '</option>';
}};

function reason_query(){

$myData2 = mysql_query("SELECT reason FROM venues");
while($record2 = mysql_fetch_array($myData2)){
    echo '<option value="' . $record3['reason'] . '">' . $record2['reason'] . '</option>';
}};

function budget_query(){

$myData3 = mysql_query("SELECT budget FROM venues");
while($record3 = mysql_fetch_array($myData3)){
    echo '<option value="' . $record3['budget'] . '">' . $record3['budget'] . '</option>';
}};


function search_venues() {

    $city = $_POST['city'];
    $reason = $_POST['reason'];
    $budget = $_POST['budget'];

    //Do real escaping here

$query = "SELECT * FROM venues";
$conditions = array();

if($city !="") {
  $conditions[] = "city='$city'";
}
if($reason !="") {
  $conditions[] = "reason='$reason'";
}
if($budget !="") {
  $conditions[] = "budget='$budget'";
}

$sql = $query;
if (count($conditions) > 0) {
  $sql .= " WHERE " . implode(' AND ', $conditions);
}

$result = mysql_query($sql);

return $result;
}

?>

我错过了什么?提前致谢!

最佳答案

我认为您在 displaydata.php 中构建了格式错误的查询

应该是:

<?php

// Include the connection file.
include("func.inc.php");
$sql = "SELECT * FROM venues "; //<----note here: no mysql_query() and a blank at the end

if(isset($_POST['submit'])) {

$search_term = mysql_real_escape_string($_POST['$venues']);

$sql .= "WHERE city = '{$search_term}' "; //<----note here: blank at the end
$sql .= "OR reason = '{$search_term}' "; //<----note here : blank at the end
$sql .= "OR budget = '{$search_term}' "; //<----note here: blank at the end

}
[....]

关于php - 使用PHP在mysql中拉取数据显示搜索表单结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29288928/

相关文章:

java - 使用从 MySQL 数据库读取列名的方法返回的数组已满 "null"

php - 您将如何使用 facebook php sdk 执行 `debug_token` 调用?

php - shell_exec 一直阻塞直到完成,我希望它是非阻塞的

php - array_search() 不匹配带点的数字

mysql - 选择多列的计数,但仅在列值大于 0 时才包括计数

android - 来自 Android 设备的远程 MySQL 数据库连接

php sql同步等待

php - 如何使用 PHP 和 MySQL 显示包含多个子类别的侧面导航菜单?

php - 尝试创建 MySQL 数据库给出 HTTP/404 请求的 URL 别名未定义

database - 数据库、IO 和 CPU 哪个更贵?