duan7264
duan7264
2015-03-26 21:10

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

Once users fill out the "main_search" form, a list of results should populate, depending on their selection on the previous page. Either the entire list pops up or nothing at all. Below, my HTML starts here and then I need the results to populate on the next page. Please, please, please help!!

For a reference, check this site out: 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>

Here is my displaydata.php page that should populate the filtered results on the next page.

<?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>

Here is my func.inc.php file.

<?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;
}

?>

What am I missing? Thanks in advance!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doubi1910 doubi1910 6年前

    I think you're builting a malformed query in displaydata.php

    It should be:

    <?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
    
    }
    [....]
    
    点赞 评论 复制链接分享