I am building a shopping cart and have outputted data from my cart table. I would like to update the quantity via an update button. When the button is clicked all the rows are affected and reflect the new quantity. I would like to update any one row at a time. Could anyone assist me with this problem? See snippet of code below:
$sql="SELECT * FROM carttemp WHERE session='$session'";
$result=mysql_query($sql)or die(mysql_error());
echo "<table align=\"center\" width=\"60%\" border=\"1\">";
echo "<tr>";
echo "<th>Remove</th>";
echo "<th>Quantity</th>";
echo "<th>Product ID</th>";
echo "<th>Unit Price</th>";
echo "<th>Total</th>";
echo "</tr>";
while ($row = mysql_fetch_array($result)){
$prod=$row['prodid'];
$unitprice=$row['price'];
$quantity=$row['quantity'];
$total=$quantity*$unitprice;
if(isset($_POST['update'])){
$a=$_POST['qty'];
$sql2=("UPDATE carttemp SET quantity='$a' WHERE session='$session' and prodid='$prod'");
$result2=mysql_query($sql2);
}
$sql3=("SELECT * FROM pricelist WHERE prodid='$prod'");
$result3=mysql_query($sql3)or die(mysql_error());
while ($row3 = mysql_fetch_array($result3)){
$newprice=$row3['price'];
}
echo "<tr>";
echo "<td align=\"center\"><input type=\"checkbox\" name=\"remove\" />Remove</td>";
echo "<td align=\"center\"><input type=\"text\" name=\"qty\" value=\"".$quantity."\" size=\"3\"/></td>";
echo "<td align=\"center\"><input class=\"noborder\" type=\"text\" name=\"product\" value=\"".$row['prodid']."\" size=\"10\" READONLY/></td>";
echo "<td align=\"center\"><input class=\"noborder\" type=\"text\" name=\"unitprice\" value=\"".$newprice."\" READONLY/></td>";
echo "<td align=\"center\"><input class=\"noborder\" type=\"text\" name=\"total\" value=\"".$total."\" READONLY/></td>";
echo "</tr>";
}
echo "</table>";
echo "<table align=\"center\" width=\"60%\" border=\"0\">";
echo "<tr>";
echo "<td><input type=\"submit\" name=\"update\" value=\"Update\" /></td>";
echo "<td align=\"right\"><input type=\"submit\" name=\"continue\" value=\"Continue\" /></td>";
echo "</tr>";
echo "</table>";
mysql_close($db_name);
?>