Reputation: 1
in this code I retrieve each product added to the cart by the user, and under each product is a 'remove' button. The problem is I want to set a hidden value for each one, and be able to get the value when the button is clicked. (I know that one way of doing this is setting the same name to the type= submit buttons but set their values different from each other, but the values would appear on the button itself, which i don't want.) Thank you in advance.
while ($isready= mysqli_fetch_array($getinfo))
{
echo "<br>$isready[1] $isready[2] BD $isready[4]";
echo '<br><input name="removefromcart" type="submit" value="remove">';
echo '<input type=hidden name="removed" value="'.$isready[0].'"">';
$i++;
}`enter code here`
$mult= $isready[2]* $isready[4];
$total= $total +$mult;
echo "<br><br>$total BD";
if (isset($_POST['removefromcart']))
{
$removebutton=$_POST['removefromcart'];
$conn->query("DELETE FROM `addedtocart` WHERE ID=$removebutton");
$conn->query("ALTER TABLE addedtocart AUTO_INCREMENT=$removebutton");
}
Upvotes: 0
Views: 853
Reputation: 1692
Avoid SQL Injection, Use prepared statements and parameterized queries for more info HERE For expediency sake, below code is still vulnerable:
This Code I altered little bit i added the post removed value into your query you can adjust like how you want:
while ($isready= mysqli_fetch_array($getinfo))
{
echo "<br>$isready[1] $isready[2] BD $isready[4]";
echo '<br><input name="removefromcart" type="submit" value="remove">';
echo '<input type=hidden name="removed" value="'.$isready[0].'"">';
$i++;
}`enter code here`
$mult= $isready[2]* $isready[4];
$total= $total +$mult;
echo "<br><br>$total BD";
if (isset($_POST['removefromcart']))
{
$removebutton=$_POST['removefromcart'];
$removeValue=$_POST['removed'];
$conn->query("DELETE FROM `addedtocart` WHERE ID=".$removeValue);
$conn->query("ALTER TABLE addedtocart AUTO_INCREMENT=".$removeValue);
}
Upvotes: 1