Jump to content

inner join search help


chemicaluser

Recommended Posts

I have a database with two tables, they are designed like this

 

Table1 has two fields (log_number, name)

Table2 has two fields (log_number, city)

 

I would like to do a search for a specific log number and display only one result for that matching log number (that we searched for) and display something like this 

 

Log Number: 59

Name: Mark

City: Chicago

 

this is my code so far

 

 

my search form

 

<form method="get" action="search.php" />   
      Log Number 
     <input type="text" id="log_number" name="log_number" />
     <input type="submit" name="submit" value="Search" />

 

the search.php file

 

      <?php
require ('/sqlconnect.php');
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

        $log_number = $_GET['log_number'];
        $name = $_GET['name'];
        $city = $_GET['city'];

  $query = "SELECT * FROM table1, table2 WHERE table1.log_number = table2.log_number";

  $result = mysqli_query ($dbc, $query);
     while ($row = mysqli_fetch_array($result)) {       

    echo '<b>Log Number:</b> '.$row['log_number'].'</td>';
    echo '<br/>';
    echo '<b>Name:</b> '.$row['name'].'</td>';
    echo '<br/>';
    echo '<b>City:</b> '.$row['city'].'</td>';
      
}
  mysqli_close($dbc);

?>

 

 

the problem I am having with my current code is that it does not display the results of the log number i searched for, but instead returns information for all the entries (two in my case) that have matching log numbers in both table1 and table2

when i search for log number 59, i get the following result

 

Log Number: 59

Name: Mark

City: Chicago

 

Log Number: 77

Transport Company: Doug

Customer Number: San Francisco

 

 

 

the result that I want is when i search for log number 59 ... i want it to only display that one record

Log Number: 59

Name: Mark

City: Chicago

 

 

hope someone can help :)

Link to comment
https://forums.phpfreaks.com/topic/233326-inner-join-search-help/
Share on other sites

So i have figured out what the problem was with my query ... i was not searching for the log number

 

my original query code

$query = "SELECT * FROM table1, table2 WHERE table1.log_number = table2.log_number";

 

should have been

$query = "SELECT * FROM table1, table2 WHERE table1.log_number = table2.log_number and table1.log_number like '$search'";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.