duan7264 2015-03-26 21:10
浏览 100

使用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 2015-03-26 21:23
    关注

    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
    
    }
    [....]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件