Log in

View Full Version : Plaincart Search Box/ Output Product Link



slimline77
06-03-2010, 11:59 AM
Hi all,
Almost finished my site but there's one problem I can't solve. I used a tutorial called Plaincart (phpwebcomerce.com) to build a shopping cart and I put in a product search box so users can search for a product in the catalogue. The search outputs basic info of the product and I want to output a link to the product in the catalogue so user can get more details. This is where I'm having the problem.

$_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI'];

$catId = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0;
$pdId = (isset($_GET['p']) && $_GET['p'] != '') ? $_GET['p'] : 0;

require_once 'include/header.php';

?>

<?php
$db = mysql_connect('localhost', 'root', '') or
die ('Unable to connect. Check your connection parameters.');

mysql_select_db('dvdff2', $db) or die(mysql_error($db));
?>
<?php
$search = (isset($_GET['search'])) ? $_GET['search'] : '';

$sql = 'SELECT
pd_id
FROM
tbl_product
WHERE
MATCH (pd_name, pd_dir, pd_cast) AGAINST ("' .
mysql_real_escape_string($search, $db) . '" IN BOOLEAN MODE)
ORDER BY
MATCH (pd_name, pd_dir, pd_cast) AGAINST ("' .
mysql_real_escape_string($search, $db) . '" IN BOOLEAN MODE) DESC';
$result = mysql_query($sql, $db) or die(mysql_error($db));

if (mysql_num_rows($result) == 0) {
// echo '<p><strong>No movies found that match the search terms.</strong></p>';//
//I had to leave this out for search as every button that was clicked caused it to come up
} else {
while ($row = mysql_fetch_array($result)) {
output_movie($db, $row['pd_id'], TRUE);
}
}
mysql_free_result($result);
?>

<?php
function output_movie($db, $pd_id, $preview_only = FALSE) {
if (empty($pd_id)) {
return;
}
$sql = 'SELECT
pd_id, pd_name, pd_year, pd_dir, pd_cast, pd_class, pd_price

FROM
tbl_product
WHERE
pd_id = ' . $pd_id;
$result = mysql_query($sql, $db) or die(mysql_error($db));

if ($row = mysql_fetch_assoc($result)) {
extract($row);


echo '<h3 class="blue">' . htmlspecialchars($pd_name) . '</h2>';
echo '<p><b>Release Year:</b> ' . htmlspecialchars($pd_year) . '</p>';
echo '<p><b>Starring:</b> ' . htmlspecialchars($pd_cast) . '</p>';
echo '<p><b>Director:</b> ' . htmlspecialchars($pd_dir) . '</p>';
echo '<p><b>Classification:</b> ' . htmlspecialchars($pd_class) . '</p>';
echo '<p><b>Price: </b> ' . htmlspecialchars($pd_price) . '</p>';

echo 'View More <a href="index.php?c=$catId&p=$pd_id">' . htmlspecialchars($row['pd_name']) . '</a>';


}
mysql_free_result($result);
}
?>

The last bit of code is what's causing the problem. I get the error
Unknown column '$pd_id' in 'where clause'
I don't understand this as $pd_id is not a column but a variable in my code.
I saw in the code there was instances of $pd_id and $pdId but I changed that and it didn't fix the problem. I know the link code is wrong but I'm not sure how to solve this. I know it's hard as Plaincart is probably unfamiliar to most of you but if anybody has any ideas I would be very grateful for your help.
Kind Regards.

bluewalrus
06-03-2010, 01:17 PM
Whats the out put if you echo $sql in the fuction?

slimline77
06-03-2010, 06:34 PM
Hi Bluewalrus,
Sorry I don't think I explained properly, the search function works as in I get the product details displayed including a link to product detail. However when I click the link I get the error
Unknown column '$pd_id' in 'where clause'
so the link address is obviously wrong. The c=$catId&p=$pd_id is wrong but I don't know why?
I tried to echo $sql but I'm not sure exactly how to as it just echoed the word $sql. I'm not very good at php.
Thanks for replying, sorry I took so long to answer as I'm working really hard to try and finish.

rickybacker60
06-06-2010, 12:49 PM
Hi slimline 77,
I have used plaincart before and have a fair understanding of how it works.

Have a look at the code here:

else {
while ($row = mysql_fetch_array($result)) {
output_movie($db, $row['pd_id'], TRUE);
}



I think the variable $pd_id now has to be set at this point like so

else {
while ($row = mysql_fetch_array($result)) {
$pd_id=$row['pd_id'];
output_movie($db, $pd_id, TRUE);
}

That may be the answer to the problem.

Also, I may be mistaken here but looking at the link:
echo 'View More <a href="index.php?c=$catId&p=$pd_id">' . htmlspecialchars($row['pd_name']) . '</a>';

Should this be returning to the 'store.php' page and not the index.php page, therefore this line should be:

echo 'View More <a href="store.php?c=$catId&p=$pd_id">' . htmlspecialchars($row['pd_name']) . '</a>';

I hope this helps