-1

I already create a delete button, but when I click it no showing any error message and the cart item and the item in database no get deleted.

<td class="col-sm-1 col-md-1">
    <button type="button" class="btn btn-sm btn-danger delete_cart_btn" value="<?= $citem['prod_id']; ?>">Delete</button>
</td>

This is the delete button function

if(isset($_POST['delete_cart_btn']))
{
    $prod_id = mysqli_real_escape_string($con, $_POST['prod_id']);

    $prod_query = "SELECT * FROM carts WHERE id='$prod_id' ";
    $prod_query_run = mysqli_query($con, $prod_query);
    $prod_data = mysqli_fetch_array($prod_query_run);
    $image = $prod_data['image'];

    $delete_query = "DELETE FROM carts WHERE id='$prod_id' ";
    $delete_query_run = mysqli_query($con, $delete_query);

    if($delete_query_run)
    {
         if(file_exists("../assets/images/products/".$image))
         {
             unlink("../assets/images/products/".$image);
         }

         echo 200;
    }
    else
    {
         echo 404;
    }
 }

Here my database enter image description here

Here the button cart script and the button function

enter image description here enter image description here

Here the full code the cart

<div id="fullCart" class="row">
    <div class="col-sm-12 col-md-12 col-md-offset-1">
        <table class="table table-hover">
            <thead colspan="4">
            <tr>
                <p class="text-center"><strong><span style="font-size: 25px;"><i class="fa fa-shopping-cart" style="font-size:25px;color:black"></i> My Cart</span></strong></p>
            </tr>
            </thead>
            <hr>
            <thead>
            <tr>
                <th class="text-center">Product</th>
                <th class="text-center">Quantity</th>
                <th class="text-center">Price</th>
                <th class="text-center">Total</th>
                <th>Action</th>
            </tr>
            </thead>
            <tbody>
                <?php 
                    $subTotal   = 0;
                    $quantity   = 0;
                    $tax        = 10;
                    $items = getCartItems();
                    foreach ($items as $citem) {
                      $subTotal += $citem['prod_qty'] * $citem['selling_price'];
                      $quantity += $citem['prod_qty'];
                ?>
                 
                <tr id="item_<?= $citem['prod_id']; ?>">
                <td class="col-sm-8 col-md-6">
                    <div class="media">
                        <img class="media-object" src="assets/images/products/<?= $citem['image']; ?>" style="width: 100px; height: 100px;">
                        <h4 class="media-heading" style="position:relative; left:110px; top:-100px;"><?= $citem['name']; ?></h4>
                    </div>
                </td>
                <td class="col-sm-1 col-md-1 text-center">
                    <strong><?= $citem['prod_qty']; ?></strong>
                </td>
                <td class="col-sm-1 col-md-1 text-center">
                    <strong><span style="font-size: 18px;">$</span><span id="price"><?= number_format( $citem['selling_price'], 2 ); ?></span>
                    </strong>
                </td>
                <td class="col-sm-1 col-md-1 text-center">
                    <strong><span style="font-size: 18px;">$</span><span id="totalPrice_<?= $citem['prod_id']; ?>"><?= number_format( $citem['prod_qty'] * $citem['selling_price'], 2 ); ?></span>
                    </strong>
                </td>
                <td class="col-sm-1 col-md-1">
                    <button type="button" class="btn btn-sm btn-danger delete_cart_btn" name="delete_cart_btn" value="<?= $citem['prod_id']; ?>">Delete</button>
                </td>
            </tr>
            <?php } ?>
            <tr>
                <td colspan="4" align="right">Subtotal</td>
                <td class="text-right">
                    <strong><span style="font-size: 18px;">$</span>
                        <span id="subTotal"><?= number_format( $subTotal, 2 ); ?></span>
                    </strong>
                </td>
            </tr>
            <tr>
                <td colspan="4" align="right">Taxes</td>
                <td class="text-right">
                    <strong><span style="font-size: 18px;">$</span>
                        <span id="taxes"><?= number_format( $tax * $quantity, 2 ); ?></span>
                    </strong>
                </td>
            </tr>
            <tr>
                <td colspan="4" align="right">Total</td>
                <td class="text-right">
                    <strong><span style="font-size: 18px;">$</span>
                        <span id="finalPrice"><?= number_format( $subTotal+($tax * $quantity), 2 ); ?></span>
                    </strong>
                </td>
            </tr>
            <tr>
                <td colspan="4" align="right">
                    <a href="index.php" class="btn btn-default">
                        <span class="glyphicon glyphicon-shopping-cart"></span> Place Order
                    </a>
                </td>
                <td >
                    <a href="checkout.php" class="btn btn-success">
                        Order <span class="glyphicon glyphicon-play"></span>
                    </a>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
Di Dian
  • 1
  • 2
  • 1
    Can you describe what "it doesn't working" means? You get an database error? or simply you cant see anything? after you've clicked the button you get 404 or something? can you please be more specific. – Debuqer Jul 05 '22 at 04:13
  • Please add some description as well. – Aqib Javed Jul 05 '22 at 04:30
  • @Debuqer The console not output anything when I click the delete cart button, the cart and the database no get deleted. That mean the button function link with the database error? – Di Dian Jul 05 '22 at 05:18
  • Do you use form for sending your data? can you show us the code? – Debuqer Jul 05 '22 at 05:32
  • @Debuqer I already uploaded full code script and image – Di Dian Jul 05 '22 at 05:44
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jul 05 '22 at 10:48

2 Answers2

0

It seems you don't have a form to submit There is no action when there is no form

Add a form tag and change your button type to submit you need to data pass by POST so i 've added a method="POST" attribute to my form

<form>
       <td class="col-sm-1 col-md-1" action="file_name.php" method="POST">
                    <button type="submit" class="btn btn-sm btn-danger delete_cart_btn" name="delete_cart_btn" value="<?= $citem['prod_id']; ?>">Delete</button>
       </td>
</form>

instead of file_name.php put your php script file name

Debuqer
  • 328
  • 2
  • 7
-1

You did not name the button, so it doesn't send the data in the form.

<td class="col-sm-1 col-md-1">
    <button type="button" class="btn btn-sm btn-danger delete_cart_btn" name="delete_cart_btn" value="<?= $citem['prod_id']; ?>">Delete</button>
</td>

if(isset($_POST['delete_cart_btn']))
{
    $prod_id = mysqli_real_escape_string($con, $_POST['delete_cart_btn']);

    $prod_query = "SELECT * FROM carts WHERE id='$prod_id' ";
    $prod_query_run = mysqli_query($con, $prod_query);
    $prod_data = mysqli_fetch_array($prod_query_run);
    $image = $prod_data['image'];

    $delete_query = "DELETE FROM carts WHERE id='$prod_id' ";
    $delete_query_run = mysqli_query($con, $delete_query);

    if($delete_query_run)
    {
         if(file_exists("../assets/images/products/".$image))
         {
             unlink("../assets/images/products/".$image);
         }

         echo 200;
    }
    else
    {
         echo 404;
    }
 }
Salketer
  • 14,263
  • 2
  • 30
  • 58