问题是我的数据库中有两列,我想使用发布值比较数据库中是否存在该列,如果存在,则应将默认值为0的列更新为1。一些代码,但是不起作用。
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);
}
}
每当发布值与is_used
字段中的值匹配时,我希望coupon_value
列值从0更改为1。