Forum Moderators: coopster
I display a list of items on one page, and when they are clicked on, the user is taken to a more detailed page. Only, it's not getting the proper information to correspond with the 'id' used.
Here's a link to the list page...when you click on an image or the 'model', you can see the proper 'id' is being displayed in the address bar, but not the info in the body.
here's the code in the details page;
<?
$sql = "SELECT id, model, year, colour, km, price, description, imageurl, image2, image3, image4 FROM usedbikes WHERE id= '$id' LIMIT 1";
$result = mysql_query($sql) or die('Error executing query:<br/>'.$sql.'<br/>MySQL said: '.mysql_error());
if (mysql_num_rows($result) < 1)
{
die('No product matching that description. Please try back later.</td></tr>');
}
$myrow = array();
$record_count = 1;
$records_per_row = 1; $id = $myrow['id'];
$model = $myrow['model'];
$year = $myrow['year'];
$colour = $myrow['colour'];
$km = $myrow['km'];
$price = $myrow['price'];
$description = $myrow['description'];
$imageurl = $myrow['imageurl'];
$image2 = $myrow['image2'];
$image3 = $myrow['image3'];
$image4 = $myrow['image4'];
if ($record_count == 1)
{
// first usedbike, open <tr>
echo '<tr valign="middle">';
}
// display usedbike pic and information
echo '<td align="left" valign="top" width="60%" class="bodytext" colspan="4" style="background-color:#E6771A; border-style:solid; border-width:1px; border-color:#000000; padding:4px;">';
echo '<b>Model:</b> <a href="ub_detail.php?id=' .$id. '">' .$model. '</a><br>';
echo '<b>Year:</b> ' .$year. '<br>';
echo '<b>Colour:</b> ' .$colour. '<br>';
echo '<b>Kilometers:</b> ' .$km. '<br>';
echo '<b>Price (CDN$):</b> ' .$price. '<br><br>';
echo '' .$description. '</td>';
echo '<td align="center" valign="middle" width="40%" colspan="3" style="background-image:url(images/bgrd.gif); border-style:solid; border-width:1px; border-color:#000000; padding:4px;">';
echo '<a href="ub_detail.php?id=' .$id. '"><img src="' .$thumburl. '" alt="' .$model. ' ' .$year. '" width="200" height="140" border="0" class="imgbrd"></a><br></td>';
if($record_count == $records_per_row)
{
// We just displayed the last item for this row, close the <tr>
echo '</tr>';
}
?>
I'm out of ideas for this one...any help is greatly appreciated!
thanks!
[edited by: jatar_k at 3:51 pm (utc) on Aug. 12, 2005]
[edit reason] no peronal urls thanks [/edit]
my guess is that it is just the id that is in the url not being passed properly to your query
$id exists in the $_GET superglobal array so it will need to be grabbed from there
$id = $_GET['id'];
$sql = "SELECT id, model, year, colour, km, price, description, imageurl, image2, image3, image4 FROM usedbikes WHERE id= '$id' LIMIT 1";
on another note, you don't really want to use that variable from the url asis in your query, I would at least test to be sure that it is numeric so may be something like this
$id = $_GET['id'];
if (!ctype_digit($id)) die('don't try to stick letters in there');
$sql = "SELECT id, model, year, colour, km, price, description, imageurl, image2, image3, image4 FROM usedbikes WHERE id= '$id' LIMIT 1";
I use ctype functions for type checking but you could do the same thing with is_numeric as well. You would need to add a little nicer error handling than just a die on the type check but you get the idea.
<?
$id = (int)$_GET["id"];//will return 0 on non numeric id and null. mysql will return 0 rows for id=0
$sql = "SELECT id, model, year, colour, km, price, description, imageurl, image2, image3, image4 FROM usedbikes WHERE id= '$id' LIMIT 1";
$result = mysql_query($sql) or die('Error executing query:<br/>'.$sql.'<br/>MySQL said: '.mysql_error());
if (mysql_num_rows($result) < 1)
{
die('No product matching that description. Please try back later.</td></tr>');
}
$myrow = mysql_fetch_array($result);//once is enough as you have limit 1. Else use while($myrow = mysql_fetch_array($result)){...}
$record_count = 1;
$records_per_row = 1;$id = $myrow['id'];
$model = $myrow['model'];
$year = $myrow['year'];
$colour = $myrow['colour'];
$km = $myrow['km'];
$price = $myrow['price'];
$description = $myrow['description'];
$imageurl = $myrow['imageurl'];
$image2 = $myrow['image2'];
$image3 = $myrow['image3'];
$image4 = $myrow['image4'];
...
PS. Jatar - the easiest things are the hardest to find :)
I've updated it to the following; same result however. All id's are definately numbers.
$id = (is_numeric($_GET['id']))? $_GET['id'] : 0;
$sql = "SELECT model, year, colour, km, price, description, imageurl, image2, image3, image4 FROM usedbikes WHERE id='$id' LIMIT 1";
$result = mysql_query($sql) or die('Error executing query:<br/>'.$sql.'<br/>MySQL said: '.mysql_error());
if (mysql_num_rows($result) < 1)
{
die('No product matching that description. Please try back later.</td></tr>');
}
$myrow = array();
$record_count = 1;
$records_per_row = 1;
My main concern is just getting this stuff displayed...it just isn't passing the info according to the proper id. If there's any other info i can post to make this easier, please let me know.
Thanks again!