简体   繁体   English

如何在 MySql 数据库中将复选框值存储为是 - PHP

[英]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 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您可以在“else”查询中进行 if 查询

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.而且您可以将 $checkbox 添加到执行数组中,但您应该修改准备好的语句。

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.如果未选中该复选框,则$_POST变量中不会有任何agree索引,因此请事先检查其存在以防止在日志中引发警告。

After that you can use an UPDATE query to modify the column termsAccepted in your members table.之后,您可以使用 UPDATE 查询来修改成员表中的termsAccepted列。

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.提示:如果您只存储truefalse ,则可以对列termsAccepted使用数据类型 TINYINT(1) 或 BOOLEAN,因为它只存储两个状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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