带下拉列表的搜索栏。搜索结果不是从数据库中提取并显示

时间:2018-05-27 23:05:16

标签: php html mysql

所以我有搜索栏,我希望在mysql数据库中搜索记录并在网页上显示它们。它应该允许用户选择他们正在搜索的字段,但它不是在另一端显示记录。有什么想法吗?

HTML:

<?php
include("config.php");
$link = mysqli_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysqli_error($link));

// select the database
mysqli_select_db($link, $database)
or die ("Could not select database because ".mysqli_error($link));

  $search         = isset($_POST['search'])       ? htmlspecialchars(trim($_POST['search']))      : null;
    $catLocation    = isset($_POST['selectVal'])    ? htmlspecialchars(trim($_POST['selectVal']))   : null;
    $query          = "SELECT * FROM $table 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 == "first_name"){
                $query .=  "first_name LIKE '%" . $search . "%'";
            }
			else if($catLocation == "surname"){
                $query .=  "surname LIKE '%" . $search . "%'";
            }
			else if($catLocation == "address"){
                $query .=  "address LIKE '%" . $search . "%'";
            }
			else if($catLocation == "phonenumber"){
                $query .=  "phonenumber LIKE '%" . $search . "%'";
            }
        }
		
		else{
            $query .= "1";            
        }

        $sql        = mysqli_query($query);
        //HERE AGAIN WAS AN ERROR... YOU PASSED mysql_fetch_array A STRING $query INSTEAD OF A RESOURCE: $sql
        while ($row = mysqli_fetch_array($sql)){
            $firstname  = $row["first_name"];
            $surname    = $row["surname"];
            $address   = $row["address"];
            $phonenumber = $row['phonenumber'];
            

            echo "First Name :  $firstname<br>";
            echo "Surname : $surname<br>";
            echo "Address : $address<br>";
            echo "Phone Number: $phonenumber<br>";
          
        }

    }
	
?>

PHP

caching_sha2_password

代码不会提供任何错误,只是应该是一个空白区域。还想知道是否有人知道是否可以将first_name和surname作为字段并且搜索说“Emma Watson”并且能够返回两个字段的结果,如果其中一个单词在那里?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

请查看以下更新的代码

from booklet.Booklet import Booklet
from booklet.Question import Question
from booklet.Question import AnotherClass

在单个(名称)中合并2个字段(名字和姓氏),以便在两个字段中进行搜索

include("config.php");
$link = mysqli_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysqli_error($link));

// select the database
mysqli_select_db($link, $database)
or die ("Could not select database because ".mysqli_error($link));

$search         = isset($_POST['search'])       ? htmlspecialchars(trim($_POST['search']))      : null;
$catLocation    = isset($_POST['selectVal'])    ? htmlspecialchars(trim($_POST['selectVal']))   : null;
$query          = "SELECT * FROM $table WHERE ";

//**If you want to merge for first name and surname then you need to merge both query with OR condition as below**
if($catLocation){
    if($search){
        if($catLocation == "category"){
            $query .= " category LIKE '%" . $search . "%'";
        }
        else if($catLocation == "name"){
            $query .=  " ( first_name LIKE '%" . $search . "%' OR surname LIKE '%" . $search . "%' ) ";
        }
        else if($catLocation == "address"){
            $query .=  "address LIKE '%" . $search . "%'";
        }
        else if($catLocation == "phonenumber"){
            $query .=  "phonenumber LIKE '%" . $search . "%'";
        }
    }   
    else{
        $query .= "1";            
    }

    $sql = mysqli_query($link, $query); // **Adding reference connection variable**

    while ($row = mysqli_fetch_array($sql)){
        $firstname  = $row["first_name"];
        $surname    = $row["surname"];
        $address   = $row["address"];
        $phonenumber = $row['phonenumber'];


        echo "First Name :  $firstname<br>";
        echo "Surname : $surname<br>";
        echo "Address : $address<br>";
        echo "Phone Number: $phonenumber<br>";

    }

}