使用PHP在mysql中提取数据来显示搜索表单结果

时间:2015-03-26 21:10:59

标签: 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;
}

?>

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

1 个答案:

答案 0 :(得分:0)

我认为您在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

}
[....]