PHP搜索在多个数据库表中不起作用

时间:2018-04-17 09:48:54

标签: php html mysql

我正在尝试执行一个搜索表单,您可以在其中搜索多个数据库表,并且当我只搜索一个表时它正在工作但是当我尝试使用两个表时我得到一个“未定义的索引”错误。

这是html代码:

        <table class="table table-striped">
        <tr>
            <th>Customer Name</th>
            <th>Email</th>
            <th>Address</th>
            <th></th>
        </tr>

        <?php print("$searchoutput"); ?>


        </table>

这是html标签上面的php代码。 搜索的代码在第二个php标记中:

 <?php include("includes/database.php"); ?>
 <?php

$query = "SELECT 
        costumers.id,
        costumers.first_name,
        costumers.last_name,
        costumers.email,
        costumer_addresses.address,
        costumer_addresses.address2,
        costumer_addresses.city,
        costumer_addresses.state,
        costumer_addresses.zipcode
        FROM costumers 
        Inner Join costumer_addresses
        on costumer_addresses.costumer=costumers.id
        order by join_date DESC";

$result=$mysqli->query($query) or die ($mysqli->error.__LINE__);

?>
<?php 
    $searchoutput='';
    if(isset($_POST['search'])){
    $searchq=$_POST['search'];
    $searchq=preg_replace("#[^0-9a-z]#i", "", $searchq);

   $query = mysqli_query($mysqli,"SELECT * FROM costumer_addresses WHERE address LIKE '%$searchq%' OR address2 LIKE '%$searchq%' OR city LIKE '%$searchq%' OR state LIKE '%$searchq%' 
        UNION 
        SELECT * FROM costumers WHERE first_name LIKE '%".$searchq."%' OR last_name LIKE '%".$searchq."%' OR email LIKE '%".$searchq."%' ") OR die($mysqli->error.__LINE__);


    $count = mysqli_num_rows($query);
    if($count == 0){
        $searchoutput = 'There was no search results!';
    }else{
        while($row = mysqli_fetch_array($query)){
            $first_name=$row['first_name'];
            $last_name=$row['last_name'];
            $email=$row['email'];
            $address=$row['address'];
            $address2=$row['address2'];
            $city=$row['city'];
            $state=$row['state'];

            $searchoutput.='<tr>';
            $searchoutput.='<td>'.$row['first_name'].' '.$row['last_name'].'</td>';
            $searchoutput.='<td>'.$row['email'].'</td>';
            $searchoutput.='<td>'.$row['address'].' '.$row['city'].' '.$row['state'].'</td>';
            $searchoutput.='<td><a href="edit_customer.php?id='.$row['id'].'" class="btn btn-default btn-sm">Edit</a></td>';
            $searchoutput.='</tr>';


        }
    }
}
?>

1 个答案:

答案 0 :(得分:0)

请使用

$query = mysqli_query($mysqli,"SELECT * FROM costumers where first_name like '%$searchq%' or last_name like '%$searchq%' or email like '%$searchq%' 
    UNION 
    SELECT * FROM costumer_addresses where address like '%".$searchq."%' or address2 like '%".$searchq."%' or city like '%".$searchq."%' or state like '%".$searchq."%' ") or die($mysqli->error.__LINE__);

而不是

  $query = mysqli_query($mysqli,"SELECT * FROM costumers where first_name like '%$searchq%' or last_name like '%$searchq%' or email like '%$searchq%' 
    UNION 
    SELECT * FROM costumer_addresses where address like '%$searchq%' or address2 like '%$searchq%' or city like '%$searchq%' or state like '%$searchq%' ") or die($mysqli->error.__LINE__);