Colm Osiris
11-18-2006, 10:18 AM
I think this is a PHP question, and not MySQL?
I have a MySQL database which I'm searching via PHP, and it produces a list perfectly well.
I want to be able to click on a button on any of the found records in the list, to go to a detail page for that record.
I have a problem:
The ID field I'm passing as a hidden field is always from the last record in the list, regardless of which record's button I click. I imgaine this is because by the time the button is pressed, the ID field has been incremented? But how do I get round it please?
Many thanks in advance. :-)
arne2
11-18-2006, 10:27 AM
Yes, it's probably because you increment your number and echo it after going through all the records. If you post your code here i will change it so it returns the correct id.
Colm Osiris
11-18-2006, 04:06 PM
Thank you very much.
I know I'm not doing anything with the ID yet, apart from printing it, but I will put that code in once I know I'm passing the right data. Then I will test for the ID field, and if it's present, will make the detail page.
<?php
$key=$_POST['key'];
echo $key . '<br><br>';
$num=0;
$user="xxxxxxxx";
$password="xxxxxxxx";
$database="xxxxxxxx";
$search_company=$_POST['company'];
$search_classification=$_POST['classification'];
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die("Unable to select database");
// get data - classification only (clicked link from results not found list)
if ((isset($_GET['classification'])) && ($_GET['classification']!=''))
{
$search_classification=$_GET['classification'];
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die("Unable to select database");
$query="SELECT * FROM contacts WHERE (classification='$search_classification') ORDER BY company ASC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
}
else
// post data
{
// both classification and company
if (($search_classification != "") && ($search_company != ""))
{
$query="SELECT * FROM contacts WHERE (company LIKE '$search_company%') or (classification LIKE '$search_classification%') ORDER BY company ASC";
}
// classification only
if (($search_classification != "") && ($search_company == ""))
{
$query="SELECT * FROM contacts WHERE (classification LIKE '$search_classification%') ORDER BY company ASC";
}
// company only
if (($search_classification == "") && ($search_company != ""))
{
$query="SELECT * FROM contacts WHERE (company LIKE '$search_company%') ORDER BY company ASC";
}
// run the query
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
}
// results found
if ($num!==0)
{
// results header...
echo '<h2>' . $num . ' results for ';
// ...both classification and company
if (($search_classification != "") && ($search_company != ""))
{echo 'classification "' . $search_classification . '" and company "' . $search_company . '" :</h2>';}
// ...classification only
if (($search_classification != "") && ($search_company == ""))
{echo 'classification "' . $search_classification . '" :</h2>';}
// ...company only
if (($search_classification == "") && ($search_company != ""))
{echo 'company "' . $search_company . '" :</h2>';}
// results list
echo '<hr>';
$i=0;
while ($i < $num)
{
// get the results
$id=mysql_result($result,$i,"contactID");
$company=mysql_result($result,$i,"company");
$address=mysql_result($result,$i,"address");
$phone=mysql_result($result,$i,"phone");
$classification=mysql_result($result,$i,"classification");
$website=mysql_result($result,$i,"website");
// display the results
echo
'id: ' . $id . '<br>
company: ' . $company . '<br>
address: ' . $address . '<br>
phone: ' . $phone . '<br>
classification: ' . $classification . '<br>
<a href="http://' . $website . '">http://' . $website . '</a><br><br>
open within frame:<br>
<a href="frame.php?website=' . $website .'" target="_blank">' . $website . '</a><br><br>
<input type="Submit" value="details">
<input type="hidden" name="key" value="' . $id . '"><br>
<hr>
';
$i++;
}
}
else
{
// no results found
echo
'<h2>Sorry! No Results Found. Please Select from one of the following Classifications</h2>
<a href="results.php?classification=supermarket">Supermarket</a><br>
<a href="results.php?classification=department store">Department Store<br></a>
<a href="results.php?classification=designers">Designers</a>
';
}
?>
Colm Osiris
11-18-2006, 04:44 PM
It's okay, I've got it!
Someone on another forum told me I needed separate forms for each button, and it works brilliantly.
Thanks for reading my question :-)