0

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");

    }
yanis
  • 1
  • you can use like `$removedValue=$_POST['removed'];` – Nawin Feb 08 '18 at 11:47
  • imho you never should alter the auto-increment value as this defeat the purpose of it – DarkBee Feb 08 '18 at 11:57
  • It can’t work this way, there is no connection between the specific submit button the user will press, and the hidden input field next to it - as long as this is all contained in one single form, it will send _all_ those hidden input values at the same time. _"but the values would appear on the button itself, which i don't want."_ - then use an actual `button` element, that allows you to specify submit value and content shown to the user separately. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button – CBroe Feb 08 '18 at 11:59

1 Answers1

1

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);

    }
przemo_li
  • 3,932
  • 4
  • 35
  • 60
Nawin
  • 1,653
  • 2
  • 14
  • 23
  • Thanks @przemo_li but questioner asking this way only... I just give Suggestion for him... – Nawin Feb 08 '18 at 11:57