I’m struggling to diagnose why my form isn’t updating my database records - I’ve taken to echoing the posted variables out in the sucess statement and the correct (updated) values are being shown but when I return to the form the update hasn’t taken hold and the old values are still present.
My update code:
<?php
include("includes/db.config.php");
try {
$product_id = $_POST['product_id'];
$product_stock_quantity = $_POST['product_stock_quantity'];
$product_location_shutters = $_POST['product_location_shutters'];
$sql = "UPDATE products SET product_id = :product_id,
product_stock_quantity = :product_stock_quantity,
product_location_shutters = :product_location_shutters,
WHERE product_id = :product_id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':product_id', $_POST['product_id'], PDO::PARAM_STR);
$stmt->bindParam(':product_stock_quantity', $_POST['product_stock_quantity'], PDO::PARAM_STR);
$stmt->bindParam(':product_location_shutters', $_POST['product_location_shutters'], PDO::PARAM_STR);
$stmt->execute();
// REDIRECT AND TRIGGER SUCCESS MESSAGE
echo "New records created successfully" . $product_id . " " . $product_stock_quantity . " " . $product_location_shutters;
//header("Location: products-single.php?pid=" . $product_id . "&edited=1");
die();
}
catch(PDOException $e)
{
// REDIRECT AND TRIGGER FAIL MESSAGE
echo "Error: " . $e->getMessage();
// /header("Location: products-single.php?fail=1");
die();
}
$db = null;
?>
And an example of my form script:
<form method="post" action="product-update.php?pid=<?php echo $product_id; ?>">
<input class="form-control" type="text" placeholder="Default input" id="product_stock_quantity" name="product_stock_quantity" value="<?php echo htmlentities($products_fetchproduct_single['product_stock_quantity'], ENT_QUOTES, 'UTF-8'); ?>
<button type="submit" class="btn btn-primary">Save</button>
</form>
Can anyone help shed any light on what might be happening?