Hi,
My aim: to display data from a database dependant on the option selected in the drop down.
I have successfully managed this without a hitch, however I also want to be able to display data from a second table dependant on the id of the data displayed from the first table - all at the same time!!
I'm using the following code to populate the drop down box:
PHP:--------------------------------------------------------------------------------
<html>
<body>
<form name="clientlist" method="post" action="clients.php">
<table>
<tr>
<td> <select name="client" onChange="this.form.submit()">
<option select>select client...</option>
<?php
//connect to the DB
include('dbs.php');
//set up the query
$query = "select clients.clientid, clients.company from clients";
//run the query
$result = mysql_query($query, $connection) or die(mysql_error());
//The following lines return the results from the query and assign them to variables
while($row = mysql_fetch_array($result)){
$company = $row['company'];
$clientid = $row['clientid'];
//This line prints out the HTML Code with the returned data
echo "<option value='$clientid'>$company</option>";
};
mysql_free_result($result);
?>
</select> </td>
</tr>
</form><br><br>
--------------------------------------------------------------------------------
and this code to display my data:
PHP:--------------------------------------------------------------------------------
<?php
//connect to the DB
include('dbs.php');
if ($_POST['client'] <> "" {
$id = $_POST['client'];
}
//set up query
$query = "SELECT * FROM clients, contacts where clients.clientid = '$id' AND clients.clientid = contacts.clientid";
//run query
$result = mysql_query($query, $connection) or die(mysql_error());
echo"<table>";
if($row = mysql_fetch_array($result)) {
$clientid = $row['clientid'];
$contactid = $row['contactid'];
$company = $row['company'];
$address1 = $row['address1'];
$address2 = $row['address2'];
$town = $row['town'];
$county = $row['county'];
$postcode = $row['postcode'];
$mainphone = $row['mainphone'];
$fax = $row['fax'];
$mainemail = $row['mainemail'];
$web = $row['web'];
$notes = $row['notes'];
$name = $row['name'];
$phone = $row['phone'];
$ext = $row['ext'];
$mobile = $row['mobile'];
$email = $row['email'];
echo "
<tr><td>$company</td><td>$name</td></tr>
<tr><td>$address1</td><td>$phone</td></tr>
<tr><td>$address2</td><td>$ext</td></tr>
<tr><td>$town </td><td>$mobile</td></tr>
<tr><td>$county</td><td>$email</td></tr>
<tr><td colspan=2>$postcode</td><tr>
<tr><td colspan=2>$mainphone</td><tr>
<tr><td colspan=2>$fax</td><tr>
<tr><td colspan=2>$mainemail</td><tr>
<tr><td colspan=2>$web</td><tr>
<tr><td colspan=2>$notes</td><tr>";
}//end while loop
mysql_free_result($result);
echo "</table>";
?>
</body>
</html>
--------------------------------------------------------------------------------
(I'm not concerned at the moment how the data will display on the page!)
but, it returns the first value from the first table and the second value from the second table (even though id's don't match) and will only display data for the first option in the drop down box and nothing for the second option....
So, I think my problem lies in my query because when I use:
PHP:--------------------------------------------------------------------------------
//set up query
$query = "SELECT * FROM clients where clients.clientid = '$id'";
--------------------------------------------------------------------------------
to display data from the first table it returns data as I expect it to!
Can anyone help me?
(sorry it's such a long post but thought I needed to explain myself clearly!)
Thankyou in advance.....
My aim: to display data from a database dependant on the option selected in the drop down.
I have successfully managed this without a hitch, however I also want to be able to display data from a second table dependant on the id of the data displayed from the first table - all at the same time!!
I'm using the following code to populate the drop down box:
PHP:--------------------------------------------------------------------------------
<html>
<body>
<form name="clientlist" method="post" action="clients.php">
<table>
<tr>
<td> <select name="client" onChange="this.form.submit()">
<option select>select client...</option>
<?php
//connect to the DB
include('dbs.php');
//set up the query
$query = "select clients.clientid, clients.company from clients";
//run the query
$result = mysql_query($query, $connection) or die(mysql_error());
//The following lines return the results from the query and assign them to variables
while($row = mysql_fetch_array($result)){
$company = $row['company'];
$clientid = $row['clientid'];
//This line prints out the HTML Code with the returned data
echo "<option value='$clientid'>$company</option>";
};
mysql_free_result($result);
?>
</select> </td>
</tr>
</form><br><br>
--------------------------------------------------------------------------------
and this code to display my data:
PHP:--------------------------------------------------------------------------------
<?php
//connect to the DB
include('dbs.php');
if ($_POST['client'] <> "" {
$id = $_POST['client'];
}
//set up query
$query = "SELECT * FROM clients, contacts where clients.clientid = '$id' AND clients.clientid = contacts.clientid";
//run query
$result = mysql_query($query, $connection) or die(mysql_error());
echo"<table>";
if($row = mysql_fetch_array($result)) {
$clientid = $row['clientid'];
$contactid = $row['contactid'];
$company = $row['company'];
$address1 = $row['address1'];
$address2 = $row['address2'];
$town = $row['town'];
$county = $row['county'];
$postcode = $row['postcode'];
$mainphone = $row['mainphone'];
$fax = $row['fax'];
$mainemail = $row['mainemail'];
$web = $row['web'];
$notes = $row['notes'];
$name = $row['name'];
$phone = $row['phone'];
$ext = $row['ext'];
$mobile = $row['mobile'];
$email = $row['email'];
echo "
<tr><td>$company</td><td>$name</td></tr>
<tr><td>$address1</td><td>$phone</td></tr>
<tr><td>$address2</td><td>$ext</td></tr>
<tr><td>$town </td><td>$mobile</td></tr>
<tr><td>$county</td><td>$email</td></tr>
<tr><td colspan=2>$postcode</td><tr>
<tr><td colspan=2>$mainphone</td><tr>
<tr><td colspan=2>$fax</td><tr>
<tr><td colspan=2>$mainemail</td><tr>
<tr><td colspan=2>$web</td><tr>
<tr><td colspan=2>$notes</td><tr>";
}//end while loop
mysql_free_result($result);
echo "</table>";
?>
</body>
</html>
--------------------------------------------------------------------------------
(I'm not concerned at the moment how the data will display on the page!)
but, it returns the first value from the first table and the second value from the second table (even though id's don't match) and will only display data for the first option in the drop down box and nothing for the second option....
So, I think my problem lies in my query because when I use:
PHP:--------------------------------------------------------------------------------
//set up query
$query = "SELECT * FROM clients where clients.clientid = '$id'";
--------------------------------------------------------------------------------
to display data from the first table it returns data as I expect it to!
Can anyone help me?
(sorry it's such a long post but thought I needed to explain myself clearly!)
Thankyou in advance.....