Forum Moderators: coopster
At the moment to link to a specific product, all I can do is change the url:
[domain.co.uk...]
By changing X and Y, I can make specific products be diplayed.
What I want to do is choose to display a product by a specific field(e.g. Product Name=Widget)
This doesn't work:
[domain.co.uk...]
What is the correct way of achieving this?
So long as you have a field in your database with the product name, you should be able to access it using a query string. Are you familiar with query strings?
In your example, product_name is a variable containing the value of 'widget'. You would use it like this on your query page:
$product_name = $_GET['product_name'];
$query = mysql_query("SELECT FROM table WHERE name = '$product_name' LIMIT 1") or die(mysql_error());
$row = mysql_fetch_object($query);
echo $row->name;
and so on. No need for the limit clause if you have more than one product with the same name. If you do, a loop construct will be needed to fetch all rows.
Hope that helps.
dc
I have a database with a table in it called "dvdtable" In this table is a DVD list, which contains 100s of DVDs with fields such as product_name, description etc.
I want to use Dreamweaver to create a page so that when I go to:
[domain.co.uk...] then a page for the film Widget is displayed, or when I go to
[domain.co.uk...] then a page for the film Foo is displayed.
I hope this makes sense.
The dvds.php currently contains the following code:
<?php require_once('Connections/dvdbase.php');?>
<?php
$currentPage = $_SERVER["PHP_SELF"];
mysql_select_db($database_products, $products);
$product_name = $_GET['product_name'];
$query = mysql_query("SELECT * FROM dvdtable WHERE product_name = '$product_name'") or die(mysql_error());
$row = mysql_fetch_object($query);
?>
<HTML>
<HEAD></HEAD>
<BODY>
<?php echo $row['product_name'];?>
<?php echo $row['description'];?>
</BODY>
</HTML>
However, when I look at this page nothing is displayed.
There is nothing wrong with my SQL connection, or anything, as when I use DW default code to generate pages from my database, displaying all products, or X products at a time there is no problem.
Thanks in advance.
You are using mysql_fetch_object, but using the wrong command to display the data. mysql_fetch_object displays the data using a class type operand.
Try changing:
<?php echo $row['product_name'];?>
<?php echo $row['description'];?>
to:
<?php echo $row->product_name;?>
<?php echo $row->description;?>
See if that helps.