Home PHP search is not working in multiple database tables
Reply: 0

PHP search is not working in multiple database tables

user3249
1#
user3249 Published in April 26, 2018, 1:41 pm

This question already has an answer here:

  • PHP: “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” 24 answers

I'm trying to do a search form where you can search in multiple database tables, and its working when i search in only one table but when i try with two tables im getting a "Undefined index" error.

This is the html code :

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

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


        </table>

And this is the php code above html tag. The code for searching is in the second php tag:

 <?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>';


        }
    }
}
?>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30964 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO