简体   繁体   中英

How do I store a checkbox value as yes in MySql Database - PHP

So when a user creates an account and they have to tick the checkbox to proceed which works, but how do I store that they accepted it (even though they cannot make an account without accpecting it). But we want to store it nonetheless.

Checkbox

<input type="checkbox" name="agree" value="accepted">

PHP

if ($_POST['agree'] != 'accepted') {
             $error[] = 'Please indicate that you have read and agree to the Terms and Conditions and Privacy Policy';
    } else {
            $stmt = $db->prepare('SELECT termsAccepted FROM members WHERE termsAccepted = :??????');
    $stmt->execute(array(':??????' => $?????));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    }

You can make a if query in your "else" query

if ($_POST['agree'] == 'accepted') {
 $checkbox = "true";
} else {
 $checkbox = "false";
}

And than you can add $checkbox to your executing array, but you should revise the prepared statement.

If the checkbox isn't checked, there wont be any agree index in the $_POST variable so check its existence beforehand to prevent throwing warnings in the log.

After that you can use an UPDATE query to modify the column termsAccepted in your members table.

pseudo code:

if (isset($_POST['agree']) && $_POST['agree'] == 'accepted') {
    $stmt = $db->prepare('UPDATE members SET termsAccepted=1 WHERE id = :user_id');
    $stmt->execute(array(':user_id' => $user_id));
}

Tip: If you're only storing true or false , you can use the datatype TINYINT(1) or BOOLEAN for the column termsAccepted as it stores only two states.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM