2017-08-15 62 views
0

我正在做一個搜索欄來搜索idfirstnamelastnameemail從表registration,但得到的錯誤:得到錯誤在我搜索(PHP)

Notice: Undefined variable: valuetosearch in D:\Xampp\htdocs\Registration_system\dashboard.php on line 13

Notice: Undefined variable: query in D:\Xampp\htdocs\Registration_system\dashboard.php on line 14

Warning: mysqli_query(): Empty query in D:\Xampp\htdocs\Registration_system\dashboard.php on line 27

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\Xampp\htdocs\Registration_system\dashboard.php on line 61

<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> 
+0

u能進一步解釋一下嗎? –

回答

1

$valuetosearch使用兩個時間在你定義它的同一行(用來存儲你的字符串和作爲一個變量在你的sql中)

$query沒有定義

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); 
} 
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);} 
+0

+0

@MélikZarkouna對不起,我沒有得到你。你能澄清一下嗎? – sndsabin

+0

他想要搜索它的值的輸入不是'搜索'看我的答案,你會得到它 –