I want to update a field with a default value 0 to 1 whenever the column match with the post values.
The issue is I have two columns in my db, I want to use post values to compare whether it exist in the database and if it exist it should update the column which has a default value of 0 to 1. I have tried some code but is not working.
public function ticket()
{
// user coupon credentials
$validate_coupon = "";
$validate_coupon_err = "";
// Processing form data when form is submitted
if (isset($_POST['submit_coupon'])) {
// Check if coupon field is empty
if (empty(trim($_POST["usercoupon"]))) {
$validate_coupon_err = 'Please enter your coupon number.';
} else {
$usercoupon = htmlspecialchars($_POST["usercoupon"]);
}
$is_used = 1;
// Validate credentials
if (empty($validate_coupon_err)) {
// Prepare a select statement
$sql = "SELECT * FROM coupon WHERE coupon_value = :vcoupon LIMIT 1";
if ($stmt = $this->conn->prepare($sql)) {
// Bind variables to the prepared statement as parameters
$stmt->bindParam(':vcoupon', $param_vcoupon, PDO::PARAM_STR);
// Set parameters
$param_vcoupon = trim($_POST["usercoupon"]);
// Attempt to execute the prepared statement
if ($stmt->execute()) {
// Check if coupon exists, if yes then proceed
if ($stmt->rowCount() == 1) {
$stmt = $this->conn->prepare('UPDATE coupon
SET is_used = :used
WHERE coupon_value=:coupon');
$stmt->bindParam(':used',$is_used);
$stmt->bindParam(':coupon',$usercoupon);
if ($row = $stmt->fetch()) {
/* coupon value is correct, so start a new session and
save the value to the session */
session_start();
$_SESSION['usercoupon'] = $usercoupon;
header("location: selectexams.php");
}
else {
// Display an error message if coupon doesn't exist
$validate_coupon_err = 'No account found with that coupon.';
}
}
} else {
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
unset($stmt);
}
// Close connection
unset($pdo);
}
}
I want the is_used
column value to change from 0 to 1 whenever the post value matches with the values in the coupon_value
field.