doumu8911 2019-03-27 06:55
浏览 49

使用select和from post更新mysql列

I want to update a field with a default value 0 to 1 whenever the column match with the post values. enter image description here

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计