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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.