在我的搜索中获取错误(php)

时间:2017-08-15 08:19:42

标签: php mysql search

我正在创建搜索字段,以便从表id中搜索firstnamelastnameemailregistration,但会收到错误:

  

注意:未定义的变量:第13行的D:\ Xampp \ htdocs \ Registration_system \ dashboard.php中的valuetosearch

     

注意:未定义的变量:第14行的D:\ Xampp \ htdocs \ Registration_system \ dashboard.php中的查询

     

警告:mysqli_query():第27行的D:\ Xampp \ htdocs \ Registration_system \ dashboard.php中的空查询

     

警告:mysqli_fetch_array()要求参数1为mysqli_result,第61行的D:\ Xampp \ htdocs \ Registration_system \ dashboard.php中给出布尔值

<style type="text/css">

    table, tr, th, td
    {

      border: 1px solid black;
    }
</style>
<?php

if (isset($_POST['valuetosearch'])) {
    $valuetosearch =$_POST['valuetosearch'];
    $valuetosearch = "SELECT * FROM `registration` WHERE CONCAT(`firstname`, `lastname`, `email`, `phonenumber`) LIKE '%".$valuetosearch."'"; 
    $search_result= filtertable($query);
}

else
{
$query= "SELECT * FROM `registration`";
$search_result= filtertable($query);

}

function filtertable($query){

require_once'config.php';
$filter_result= mysqli_query($CONN, $query);
return $filter_result;

}
?>


<h1>Welcome on dashboard </h1>
<ul>
<li><a href="dashboard.php">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="dashboard.php">Profile</a></li>
<li><a href="login.php">Logout</a></li>
</ul>

<form action="dashboard.php" method="POST">

<input type="text" name="valuetosearch" placeholder="Search here..."> 
<input type="submit" name="search" value="submit"> <br><br>

<table>

<tr>

<th>id</th>
<th>First name</th>
<th>Last name</th>
<th>email</th>
<th>Phone number</th>

</tr>
<?php while($row= mysqli_fetch_array($search_result)): ?>
<tr>
    <td><?php echo $row['id']; ?></td>
    <td><?php echo $row['firstname']; ?></td>
    <td><?php echo $row['lastname']; ?></td>
    <td><?php echo $row['email']; ?></td>

</tr>

<?php endwhile; ?>
</table>

</form>

3 个答案:

答案 0 :(得分:2)

if (isset($_POST['search'])) { ... }替换为

if (isset($_POST['search'])) {
$query = "SELECT * FROM `registration` WHERE CONCAT(`firstname`, `lastname`, `email`, `phonenumber`) LIKE '%".$_POST['valuetosearch']."'"; 
$search_result = filtertable($query);}

答案 1 :(得分:1)

$valuetosearch在您定义它的同一行中使用两次(将您的字符串存储为sql中的变量)

$query未定义

答案 2 :(得分:1)

您的代码中存在许多错误 第一个错误在你的代码中

if (isset($_POST['search'])) {
    $valuetosearch = "SELECT * FROM `registration` WHERE CONCAT(`firstname`, `lastname`, `email`, `phonenumber`) LIKE '%".$valuetosearch."'"; 
    $search_result= filtertable($query);
}

应该是

if (isset($_POST['search'])) {
    $query = "SELECT * FROM `registration` WHERE CONCAT(`firstname`, `lastname`, `email`, `phonenumber`) LIKE '%".$valuetosearch."'"; 
    $search_result= filtertable($query);
}