下拉列表和搜索字段php

时间:2014-03-21 14:12:53

标签: php mysql search drop-down-menu

:) 我是新来的,我对php非常新。

我正在尝试制作一个搜索表单: 包含两个项目的下拉列表:类别和位置; 文本字段; 搜索按钮。 它应该像这样工作: 当"类别"如果选中,则输入文本并仅将其搜索到类别中。 当" location"如果选中,您的字词将在国家/地区,州和邮政编码之间进行搜索。

我有一个包含列的表:id,name,category,country,zipcode,state。

有人可以帮我理解为什么它没有显示任何结果吗?

这是我的代码:

    <form action='search4.php' method='POST' name='form_filter'>

 <b>Search</b><br>
 <select name="selectVal">
 <option value="category">category</option>
 <option value="location">Country, state or zipcode</option>
 </select>
 <input type='text' name='search' placeholder='Enter text here...' size='50'><br>
 <input type='submit' value='Send'>

 </form>

<?php

// database connection

$db_host = "myhost";
$db_user = "myuser";
$db_password = "mypsw";
$db_name = "myname";

//connecting to database

$db = mysql_connect($db_host, $db_user, $db_password) or die ('Error - connection failed');
mysql_select_db($db_name, $db) or die ('Database selection error');

// retrieving search value we sent using get

$research = $_GET['research'];

// check if it has been sent, then it is ok

if ( $research == 'ok' ) {

// retrieving search value we sent using post

$search = $_POST['search'];

// check if the field has been filled

if ( $search == TRUE && $search != "" ) {

// character lenght more than 3

if ( strlen($search) >= 3 ) {

$search =  mysql_escape_string(stripslashes($search)); 
}

if(isset($_POST['value'])) { 
    if($_POST['value'] == 'category') { 
        // query to get all categories  
        $query = "SELECT * FROM table_name WHERE category='$search'";   
    }   
    elseif($_POST['value'] == 'location') {   
        // query to get all country/state/zipcode records   
        $query = "SELECT * FROM table_name WHERE country='$search' OR zip_code='$search' OR state='$search'";   
    } else {   
        // query to get all records   
        $query = "SELECT * FROM table_name";   
    }   
    $sql = mysql_query($query);   

    while ($row = mysql_fetch_array($query)){  
        $Id = $row["Id"];  
        $country = $row["country"];  
        $category = $row["category"];  
        $name = $row['name'];
        $zip_code = $row['zip_code'];
        $state = $row['state'];


echo "Name: $name<br>";
echo "Zip_code : $zip_code<br>";
echo "State : $state<br>";
echo "Country: $country<br>";
echo "Category: $category<hr>";
    } 

}
}
} 
?> 

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

您需要了解如何在php中使用<select>

如果您有此表格:

<form method='post'>
<select name='example'>
    <option value='e1'>example1</option>
    <option value='e2'>example2</option>
</select>
</form>

你需要像那样打印:

echo $_POST['example'];

如果用户选择了example1,则值为e1。

如果用户选择了example2,则值为e2。

您正在使用脚本$_POST['value']。它只是不存在。

答案 1 :(得分:1)

试试这个,而不是:

HTML表格:

<form action='search4.php' method='POST' name='form_filter'>    
    <b>Search</b><br>
    <select name="selectVal">
        <option value="category">category</option>
        <option value="location">Country, state or zipcode</option>
    </select>
    <input type='text' name='search' placeholder='Enter text here...' size='50'><br>
    <input type='submit' value='Send'>  
</form>

表格处理:

<?php

    // database connection
    $db_host        = "myhost";
    $db_user        = "myuser";
    $db_password    = "mypsw";
    $db_name        = "myname";

    //connecting to database

    $db             = mysql_connect($db_host, $db_user, $db_password) or die ('Error - connection failed');
    mysql_select_db($db_name, $db) or die ('Database selection error');

    /*********************************************/
    /***WHY DO YOU NEED THIS RESEARCH VARIABLE?***/
    /*****WHAT IS ITS PURPOSE IN THIS SCRIPT?*****/
    /*********************************************/
    //GET CLEAN VERSIONS OF ALL NECESSARY VARIABLES:
    $search         = isset($_POST['search'])       ? htmlspecialchars(trim($_POST['search']))      : null;
    $catLocation    = isset($_POST['selectVal'])    ? htmlspecialchars(trim($_POST['selectVal']))   : null;
    $query          = "SELECT * FROM table_name WHERE ";

    //YOU INDICATED YOU'D NEED TO RUN THE SEARCH-QUERY IF THE SEARCH-TERM AND SEARCH-SCOPE ARE DEFINED IE: NOT NULL; HOWEVER IF THE SEARCH TERM IS NOT GIVEN, YOU SELECT EVERYTHING IN THAT TABLE... (BAD PRACTICE, THOUGH)
    if($catLocation){
        if($search){
            if($catLocation == "category"){
                $query .= " category LIKE '%" . $search . "%'";
            }else if($catLocation == "location"){
                $query .=  " country LIKE '%" . $search . "%' OR zip_code LIKE '%" . $search . "%' OR state LIKE '%" . $search . "%'";
            }
        }else{
            $query .= "1";            
        }

        $sql        = mysql_query($query);
        //HERE AGAIN WAS AN ERROR... YOU PASSED mysql_fetch_array A STRING $query INSTEAD OF A RESOURCE: $sql
        while ($row = mysql_fetch_array($sql)){
            $Id         = $row["Id"];
            $country    = $row["country"];
            $category   = $row["category"];
            $name       = $row['name'];
            $zip_code   = $row['zip_code'];
            $state      = $row['state'];

            echo "Name: $name<br>";
            echo "Zip_code : $zip_code<br>";
            echo "State : $state<br>";
            echo "Country: $country<br>";
            echo "Category: $category<hr>";
        }

    }