0
votes

I'm currently working on a PHP shopping cart project in web development. A problem I have yet to resolve is passing the values of the product that I select from my product_list.php to cart.php - no matter what product I click, the name of the last product found in my products table is the one that keeps on appearing on the cart.php page.

For example, I would select Bamboo Mat, which is id no. 2, but the product name that would appear in cart.php is Bamboo Fence instead, which is id no. 8. Also, the value that appears in the quantity column is the number of remaining items of the selected product.

Only the values in the Product and Price columns are appearing as they should.

Here's the product_list.php file:

<?php 

$err_level = error_reporting(0);  
$conn = mysql_connect('params');  
error_reporting($err_level); 
?>
<?php 

include "storescripts/connect_to_mysql.php"; 


?>

<html>
<head>
    <title>Product List | Bamboo Art</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>

    <div class="container">
        <div class="header">
            Bamboo Art
        </div>
        <div id="nav">
            <ul id="nav">

                <li><a href="index.php">Home</a>
          </li><!--
          --><li><a href="#" class="current">Products</a>
          </li><!--
          --><li><a href="#">Contact Us</a>
          </li>

            </ul>
        </div>



        <div class="content_product-list">


            <form method="POST" action="addtocart.php">
                <?php

                        $sql = "SELECT * FROM products";
                        $query = mysql_query($sql);

                        while ($row = mysql_fetch_assoc($query)) {


                    ?>
                            <div class="products">

                                <input type='hidden'  name='id' value=' <?php echo $row['id'] ?> '>
                                <?php echo "<img src='". $row['image'] ."' width=290 height=220>" ?><br><br>

                                <h3><?php echo $row['name'] . '<input type="hidden"  name="name" value="' . $row['name'] . '">' ?></h3>

                                <h4><?php echo $row['description'] . '<input type="hidden"  name="description" value="' . $row['description'] . '">' ?></h4>

                                Price: Php <?php echo $row['price'] . '<input type="hidden"  name="price" value="' . $row['price'] . '">' ?><br>
                                <?php echo " Availability: "  . $row['qty'] . " <br/>" ?>
                                <?php echo " Quantity: <select name='qty' style='width:40px;'> 
                                                    <option>1</option>
                                                    <option>2</option>
                                                    <option>3</option>
                                                    <option>4</option>
                                                    <option>5</option>
                                                    <option>6</option>
                                                    <option>7</option>
                                                    <option>8</option>
                                                    <option>9</option>
                                                    <option>10</option>
                                                </select> "?>
                                                <br><br>
                                                <input type='submit' value='Add to Cart' name='submit'>   <br/>

                            </div>  



                    <?php

                        }

                    ?>
                </form>
            <br/>



            </div>                        
            </div>                   
            </div>                   
    </div>
</body>
</html>

The addtocart.php:

<?php 

include "storescripts/connect_to_mysql.php";

    if (isset($_POST['submit'])) {

    $qty=$_REQUEST['qty'];
    $name=$_REQUEST['name'];
    $price=$_REQUEST['price'];
    $id=$_REQUEST['id'];  



    $rs=mysql_query("SELECT * FROM cart WHERE prod_id='$id'",$con) or die (mysql_error());
    if (mysql_num_rows($rs)>0 )
        {
            mysql_query("UPDATE cart SET qty=(qty+'$qty') WHERE prod_id='$id'",$con) or die (mysql_error());
            mysql_query("UPDATE sales_record SET qty=(qty+'$qty') WHERE product_name='$name'",$con);

        }

    else
        {
            mysql_query("INSERT INTO cart (`cart_id`, `prod_id`, `qty`, `name`, `price`) VALUES (NULL, '$id', '$qty', '$name', '$price');",$con) or die(mysql_error());

            mysql_query("INSERT INTO sales_record (`sales_id`, `customer_id`, `product_name`, `qty`,`price`) VALUES (NULL, '', '$name', '$qty', '$price');",$con);
        }

        mysql_query("UPDATE products SET qty=(qty-'$qty') WHERE id='$id'",$con) or die(mysql_error());

    header("location:cart.php"); 
    exit; 
    } 

?>

The cart.php file:

<?php
include "storescripts/connect_to_mysql.php";

if(isset($_POST['checkout'])){ 
header("location:orders.php"); 
} 
if(isset($_POST['shop'])){ 
header("location:product_list.php"); 
}

?>
<html>
<head>
    <title>Shopping Cart | Bamboo Art</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    </head> 
<body>
<div class="container">
    <div class="header">
            Bamboo Art
    </div>
    <div id="nav">
            <ul id="nav">

                <li><a href="index.php">Home</a>
          </li><!--
          --><li><a href="product_list.php">Products</a>
          </li><!--
          --><li><a href="contactus.html">Contact Us</a>
          </li>

            </ul>
        </div>
<div class="content">

    <div class="table_content">


                        <h2>Your Cart</h2>
                    <form method="post">
                    <table id="table" align="center" border="0" width="100%">
                        <th>Qty</th>
                        <th>Product</th>
                        <th>Price</th>
                        <th>Total</th>
                        <tr>
                            <?php
                            //extract all items from the temporary cart
                            $q="SELECT * FROM cart, products WHERE cart.prod_id=products.id";

                            $rs=mysql_query($q) or die (mysql_error());
                            $total=0;
                            while($row=mysql_fetch_array($rs)){
                                echo '<td>'. $row['qty'] .'<input type="hidden" name="qty" value="' . $row['qty'] . '"></td>';
                                echo '<td>'. $row['name'] .'<input type="hidden" name="name" value="' . $row['name'] . '"></td>';
                                echo '<td>'. number_format($row['price'],'2','.',',') .'<input type="hidden" name="price" value="' . number_format($row['price'],'2','.',',') . '"></td>';
                                echo '<td><b>' . number_format(($row['qty']*$row['price']),'2','.',',') . '</b><input type="hidden" name="total" value="' . number_format(($row['qty']*$row['price']),'2','.',',') . '"></td>';
                                $total+=($row['qty']*$row['price']);
                                echo '</tr>';
                                echo '<tr>';
                            }
                            ?>

                            <tr></tr>
                            <tr></tr>
                            <tr><td colspan="3"><strong>GRAND TOTAL</strong></td>
                            <td><?php echo number_format($total,'2','.',',');?><input type="hidden" name="gtotal" value="<?php echo number_format($total,'2','.',',');?>"></td>

                            </tr>
                            <tr height="40px"></tr>
                            <tr>
                                <td></td><td><input type="submit" name="checkout" value="Check Out" /></td> 
                                <td><input type="submit" name="shop" value="Back to Shopping" /></td> 
                            </tr>
                    </table>

                    </form>
    </div>                                                            

    </div>

</div>
</body>
</html>

I've had a look at a few shopping cart sample codes, trying to see if there is a solution to this, but it has just stumped me.

I did find this question which sounds similar to the problem that I have, but I can't use Javascript in building this shopping cart - just HTML, CSS and PHP.

I'm currently using PHP version 4.2.7.1.

2

2 Answers

0
votes

The problem is that you only have one form and multiple input fields with the same name. Every time you set a new hidden input you give it the same name, basically "overwriting" the $_POST data you will send on submit. You could go for something like:

input type="hidden" name="name[]"

and then access that one like this in your addtocart:

$_POST['name'][2]

You'll have to base the index on the submit button pressed.

Other options would be creating a form for every product or using $_GET to pass a product id allong with quantity.

0
votes

Ok, my product_list.php can now pass the values of the product ID and name that the customer selected to the cart.php:

<?php

                        $sql = "SELECT * FROM products";
                        $query = mysql_query($sql);

                        while ($row = mysql_fetch_assoc($query)) {





                                echo "<div class='products'>";
                                echo "<form method='POST' action='addtocart.php'>";
                                echo "<img src='". $row['image'] ."' width=290 height=220><br><br>";

                                echo "<h3>". $row['name'] . '<input type="hidden"  name="name" value="' . $row['name'] . '">' . "</h3>";

                                echo "<h4>" . $row['description'] . '<input type="hidden"  name="description" value="' . $row['description'] . '">' . "</h4>";

                                echo "Price: Php" . $row['price'] . '<input type="hidden"  name="price" value="' . $row['price'] . '">' . "<br>";
                                echo " Availability: "  . $row['qty'] . " <br/>";
                                echo " <select name='qty' style='width:40px;'> 
                                                    <option>1</option>
                                                    <option>2</option>
                                                    <option>3</option>
                                                    <option>4</option>
                                                    <option>5</option>
                                                    <option>6</option>
                                                    <option>7</option>
                                                    <option>8</option>
                                                    <option>9</option>
                                                    <option>10</option>
                                                </select>
                                                <input type='hidden'  name='id' value='" . $row['id'] ." '> 
                                                <br><br>
                                                <input type='submit' value='Add to Cart' name='submit'>   <br/>";
                                echo '</form>';
                                echo " </div>";


                    }


                    ?>

However, the quantity is not showing correctly - instead of displaying the quantity the customer has selected, it deducts that quantity from the total number of items on sale. For example, there are 9 Bamboo Lanterns on sale, the customer orders 3, but the quantity displayed in cart.php is 6.

I also changed the qty field in the cart and sales_record tables to cart_qty and sr_qty, respectively, thinking it was due to a conflict, but it didn't make any difference.