PHP MySQLi 中的多个搜索选项卡



我想在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);
}
?>

'

它之所以给你一个错误,是因为你的变量$both有一个不正确的查询。

您需要从中更改它:

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

对此:

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

最新更新