【问题标题】:Multiple Search Tab in PHP MySQLiPHP MySQLi 中的多个搜索选项卡
【发布时间】:2018-12-05 01:44:58
【问题描述】:

我想在 PHP MySQLi 中添加多个搜索选项。它有多种形式的名字、姓氏和电子邮件。如果我输入,仅名字,仅姓氏,仅电子邮件以及名字和电子邮件,它已经可以工作了。但是当我输入姓氏并通过电子邮件发送错误时。你能检查我的情况吗?谢谢!

`<title>Results</title>
 <?php require 'header/headerResults.php'; ?>

 <?php
 $conn = mysqli_connect('localhost', 'root', '', 'db');

 //search code
 if($_REQUEST ['submit']){
 $firstname = mysqli_real_escape_string($conn, $_POST['name']);
 $lastname = mysqli_real_escape_string($conn, $_POST['lname']);
 $email = mysqli_real_escape_string($conn, $_POST['email']);

// query for searching
$sele = "SELECT * FROM `examinee_detail` WHERE ";
$fn = "`firstname` LIKE '%$firstname%'";
$ln = "`lastname` LIKE '%$lastname%'";
$em = "`email` LIKE '%$email%'";
$both = "`lastname`,`email` LIKE `%$lastname%`,`%$email%`";

    // since there are three search bar, this are the conditions.
    if(empty($firstname)){
        $make = '<p>You must type a word to search!</ps>';
        }else{
            $make = "<div class = 'container mt-5'>
                    <div class = 'card mt-3'>
                        <div class = 'card-header'>
                            <h2>No results found!</h2>
                        </div>";
            $sele = $sele . $fn;}

    if(empty($lastname)){
        $make = '<p>You must type a word to search!</p>';
        }else{
            $make = "<div class = 'container mt-5'>
                    <div class = 'card mt-3'>
                        <div class = 'card-header'>
                            <h2>No results found!</h2>
                        </div>";


    if(!empty($firstname)){ $sele = $sele . " OR ";}

        $sele = $sele . $ln;
            }

    if(empty($email)){
        $make = '<p>You must type a word to search!</p>';
        }else{
            $make = "<div class = 'container mt-5'>
                    <div class = 'card mt-3'>
                        <div class = 'card-header'>
                            <h2>No results found!</h2>
                        </div>";
    if(!empty($firstname)){ $sele = $sele . " OR ";}
        $sele = $sele . $em;
        }
            $result = mysqli_query($conn, $sele);
                if($mak = mysqli_num_rows($result) > 0){


    echo "<div class = 'container mt-5'>
                    <div class = 'card mt-3'>
                        <div class = 'card-header'>
                            <h2>Results</h2>
                        </div>
                            <div class = 'card-body' style = 'background-color: white'>
                                <table class = 'table table-bordered'>
                                    <tr>

                                        <th>First Name</th>
                                        <th>Last Name</th>
                                        <th>E-mail</th>
                                        <th>Action</th>"; 

    while($row = mysqli_fetch_assoc($result)){


    echo "<tr>
            <td>".$row["firstname"]."</td>
            <td>".$row["lastname"]."</td>
            <td>".$row["email"]."</td>
            <td><a href='ExamineeProfile.php?view=".$row['email']."' class='btn btn-primary'>View</a></td></tr>";}?>
 <div class = "container">
 <a href = "search.php" class = "btn btn-info">Search again</a>
 <a href = "examinees2.php" class = "btn btn-primary">Show full list</a><br><br>
 </div>

<?php
}else{
//echo'<h2> Search Result</h2>';

print ($make);
}
//mysqli_free_result($result);
//mysqli_close($conn);

}
?>

`

【问题讨论】:

  • 虽然 mysqli_real_escape_string 总比没有好,但您绝对应该考虑切换到准备好的语句,这更难获得潜在的恶意输入。

标签: php search mysqli


【解决方案1】:

它给你一个错误的原因是你的变量$both有一个不正确的查询。

你需要改变它:

$both = "`lastname`,`email` LIKE `%$lastname%`,`%$email%`";

到这里:

$both = "`lastname` LIKE '%$lastname%' AND `email` LIKE '%$email%'"; 

【讨论】:

  • 如果我将 $both 放在我的代码中,我在哪里可以把它放在我的条件中?
  • 您需要在代码中添加类似的内容才能使用 $both 变量 if(!empty($lastname) && !empty($email)){ $make = "

    没有找到结果!

    "; $sele .= $两者; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
相关资源
最近更新 更多