hi, does anyone would help me fixing my code, i got a problem in my search bar, when i want to click the add to cart button of the product searched or the items there's nothing happened, all i want is to add the product search in my cart. hope someone would help me, here's my code.

thanks

my html code:

<form  method="post" action="search.php"> 
<table cellpadding="0px" cellspacing="0px"> 
<tr> 
<td style="border-style:solid none solid solid;border-color:#4B7B9F;border-width:1px;">
<input type="text" name="name" style="width:190px; border:0px solid; height:28px; padding:0px 3px; position:relative;"> 
</td>
<td style="border-style:solid;border-color:#4B7B9F;border-width:1px;"> 
<input type="submit" value="" style="border-style: none; background: url('img/searchbutton3.gif') no-repeat; width: 24px; height: 20px;">
</td>
</tr>
</table>
</form>

my php code:

require_once 'library/config.php';
require_once 'library/cart-functions.php';




$letter = $_POST['name'];
if(!empty($letter)){   
$sql = "SELECT * FROM tbl_product WHERE pd_name like '%$letter%'";

$result = dbQuery($sql); 
while ($row = dbFetchAssoc($result)) { 
$pd_name=$row['pd_name'];
$pd_qty=$row['pd_qty'];
        $pd_image=$row['pd_image']; 
        $pd_price=$row['pd_price'];         
        $pd_description=$row['pd_description'];

    if ($pd_image) {
            $pd_image = WEB_ROOT . 'images/product/' . $pd_image;;  
            } else {
            $pd_image = WEB_ROOT . 'images/no-image-small.png';

$result = getProductDetail($pdId, $catId);

// we have $pd_name, $pd_price, $pd_description, $pd_image, $cart_url
extract($result);}
?> 
<?php require_once 'include/miniCart.php'; ?>

<table class="sofT"  align="center" valign="center">

<tr>
  <td><img src="<?php echo $pd_image; ?>" alt="<?php echo $pd_name; ?>">

<br>
Name:<?php echo $pd_name; ?><br>
Price : <?php echo displayAmount($pd_price); ?></p>
 <?php echo $pd_description; ?><br>
<?php
// if we still have this product in stock
// show the 'Add to cart' button
if ($pd_qty > 0) {
?>
<input type="button" name="btnAddToCart" value="Add To Cart &gt;" onClick="window.location.href='<?php echo $cart_url; ?>';" class="addToCartButton">

<?php
} else {
    echo 'Out Of Stock';
}
}
}
?>

and this is my cart-functions code:

function addToCart()
{
    // make sure the product id exist
    if (isset($_GET['p']) && (int)$_GET['p'] > 0) {
        $productId = (int)$_GET['p'];
    } else {
        header('Location: index.php');
    }

    // does the product exist ?
    $sql = "SELECT pd_id, pd_qty
            FROM tbl_product
            WHERE pd_id = $productId";
    $result = dbQuery($sql);

    if (dbNumRows($result) != 1) {
        // the product doesn't exist
        header('Location: cart.php');
    } else {
        // how many of this product we
        // have in stock
        $row = dbFetchAssoc($result);
        $currentStock = $row['pd_qty'];

        if ($currentStock == 0) {
            // we no longer have this product in stock
            // show the error message
            setError('The product you requested is no longer in stock');
            header('Location: cart.php');
            exit;
        }

    }       

    // current session id
    $sid = session_id();

    // check if the product is already
    // in cart table for this session
    $sql = "SELECT pd_id
            FROM tbl_cart
            WHERE pd_id = $productId AND ct_session_id = '$sid'";
    $result = dbQuery($sql);

    if (dbNumRows($result) == 0) {
        // put the product in cart table
        $sql = "INSERT INTO tbl_cart (pd_id, ct_qty, ct_session_id, ct_date)
                VALUES ($productId, 1, '$sid', NOW())";
        $result = dbQuery($sql);
    } else {
        // update product quantity in cart table
        $sql = "UPDATE tbl_cart 
                SET ct_qty = ct_qty + 1
                WHERE ct_session_id = '$sid' AND pd_id = $productId";       

        $result = dbQuery($sql);        
    }   

    // an extra job for us here is to remove abandoned carts.
    // right now the best option is to call this function here
    deleteAbandonedCart();

    header('Location: ' . $_SESSION['shop_return_url']);                
}

Check if $cart_url is set correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.