...

View Full Version : Adding to shopping cart



barb-1989
05-26-2011, 04:50 AM
Hello!

I'm trying to add an item from products.php (which connects to a database and then displays the products) to simply display in shopping.php with <form action="shopping.php" method="post". For some reason, when I send the Product ID as a value, it only shows the last item in the list. What can I do to send the correct value for each item?




<?php
//Check if there is a login session
session_start();
if (isset($_SESSION['logged']))
$id=$_SESSION['logged'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Shopping Books.com</title>

<script type = "text/javascript" src = "ShoppingBooks.js" >
</script>
<link href="style.css" rel="Stylesheet" type="text/css" />

</head>
<body>
<center>
<form action="shopping.php" method="post">

<?php #line 16

echo "<b>Books</b><br/><br/>";

//Connection to database

$conn = mysql_connect("localhost","root");

if (!$conn)

die('Could not connect' . mysql_error())


@mysql_select_db("ShoppingBooks") or die("Unable to connect");

//Selecting all rows from Product table

$sql = mysql_query("SELECT * FROM Product");

echo "<table border='0' width = '750'>";

echo "<tr><td>Product ID</td><td>Product Name</td><td>Author</td><td>Qty</td><td>Price</td><td>Picture</td><td></td></tr>";

//while the end of number of rows in the table.

while($row = mysql_fetch_array($sql)) {

//Value of hidden field is the Product_ID of each row.

echo "<tr><td><input type='hidden' name='prid' value='".$row['Product_ID']."'/>$row[Product_ID]</td>";
echo "<td>$row[Product_Name]</td>";

echo "<td>$row[Product_Detail]</td>";

echo "<td>$row[Qty]</td>";

echo "<td>$row[Price]</td>";

echo "<td>$row[Picture]</td>";

echo "<td><input type='submit' value='Add to Cart'/></td></tr>";

}

echo "</table>";

if (!$sql)

die('Error: ' . mysql_error());

mysql_close($conn);

?>
</form>
</center>
</body>
</html>

bullant
05-26-2011, 05:56 AM
To send an array of values to a php script the name of the input element must end with [].

try


name='prid[]'

You also have a syntax error in your php code which will probably need to be fixed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum