Brendan Wilson
Brendan Wilson

Reputation: 27

Delete Query not working (No bugs/syntax errors)

<input type="hidden" name="code"
        value="<?php echo $product['productCode']; ?>">
<input type="submit" value="Delete">

// Get ID
$code = filter_input(INPUT_POST, 'productCode', FILTER_VALIDATE_INT);

// Delete the product from the database
if ($code != false) {
    $query = 'DELETE FROM products
              WHERE productCode = :productCode';
    $statement = $db->prepare($query);
    $statement->bindValue(':productCode', $code);
    $success = $statement->execute();
    $statement->closeCursor();
}

The code at the top is trying to get the code into where the php/MySQL code at the bottom of the screen here. There's no bugs that fly or trigger when the code runs in the webpage, and the code doesn't flag anything in the editor. I'm really at a loss for what's going wrong here, and no one that I ask has any idea what's wrong. Any help would be appreciated!

Upvotes: 0

Views: 66

Answers (5)

Mohammad Malek
Mohammad Malek

Reputation: 634

table field is prdocutcode why are you only name is code replace below my code

 <input type="hidden" name="productCode" value="<?php echo $product['productCode']; ?>">

Upvotes: 2

Jupe
Jupe

Reputation: 48

Can you log out the :productCode? It's possible that the value is not what you think, and a delete for a non-existent item will happily report nothing if no match is found.

Upvotes: 1

PHP Geek
PHP Geek

Reputation: 4033

Please use AJAX post request for delete by code.

Upvotes: 0

Ayaz Ali Shah
Ayaz Ali Shah

Reputation: 3531

You are posting value with different key and getting it with different, and I believe this can be the reason, replace your hidden input with following

<input type="hidden" name="productCode" value="<?php echo $product['productCode']; ?>">

Also you need to take care about php tags <?php ?>, which should start and end for php script.

Upvotes: 0

Pupil
Pupil

Reputation: 23948

I think you have HTML and PHP codes in the same file.

What might be the issue:

Add condition if the form is posted, go for deleting product.

if (isset($_POST['productCode'])) {
 // Get ID
 $code = filter_input(INPUT_POST, 'productCode', FILTER_VALIDATE_INT);
 // Delete the product from the database
 if ($code != false) {
    $query = 'DELETE FROM products
              WHERE productCode = :productCode';
    $statement = $db->prepare($query);
    $statement->bindValue(':productCode', $code);
    $success = $statement->execute();
    $statement->closeCursor();
 }
}

Upvotes: 0

Related Questions