PHP如何将多个复选框值插入数据库

时间:2018-05-30 06:41:58

标签: php html database checkbox

更新 - 由于 $ param_keyword

上的拼写错误,问题已得到解决

我创建了一个表单,它接受多个复选框值以插入数据库。我的数据库连接工作正常我可以插入名称,但不能复选框。我不知道问题在哪里,因为没有错误,我可以看到复选框的字符串。

单击提交按钮后,我打印keywordArr以查看值:

家政,化学和危险材料出了点问题。请稍后再试。

PHP查询

<?php
require_once("includes/dbConfig.php");

// get input when form is submitted, using the clean functon to clean inputs
if ($_SERVER["REQUEST_METHOD"] == "POST") {

//name validation
if (empty(clean($_POST["name"]))) {
    $nameErr = "Name is required";
} else {
    $name = clean($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
        $nameErr = "Only letters and white space allowed";
    }
}

$keyword = $_POST["keyword"];
echo $keywordArr = implode(",", $keyword);

// check input erros before inserting in database
if (empty($nameErr)) {

    // Prepare a INSERT statement
    $sql = "INSERT INTO site (name,keyword) VALUES (?,?)";

    if ($stmt = mysqli_prepare($conn, $sql)) {
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "ss", $param_name, $param_keyword);

        // Set parameters
        //$param_id = $id;
        $param_name = $name;
        $parm_keyword = $keywordArr;

        if (mysqli_stmt_execute($stmt)) {

            // Alert to redirect
            echo ("<SCRIPT LANGUAGE='JavaScript'>
                    window.alert('Successfully Sent!')
                    window.location.href='index.php';
                    </SCRIPT>");
        } else {
            echo "Something went wrong. Please try again later.";
        }
    }
    // Close statement
    mysqli_stmt_close($stmt);
    }
    // Close connection
    mysqli_close($conn);
}
?>

HTML表单

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1" 
value="housekeeping" name="keyword[]">
<label class="custom-control-label" for="customCheck1">Housekeeping</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck2" 
value="procedure" name="keyword[]">
<label class="custom-control-label" for="customCheck2">Procedure (PTW,JSA, 
Inspection)</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck3" 
value="chemical and dangerous material" name="keyword[]">
<label class="custom-control-label" for="customCheck3">Chemical & Dangerous 
Material</label>
</div>      
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck4" 
value="confined space" name="keyword[]">
<label class="custom-control-label" for="customCheck4">Confined 
Space</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck5" 
value="scaffolding" name="keyword[]">
<label class="custom-control-label" for="customCheck5">Scaffolding</label>
</div>
<input type="submit" name="submit" value="Submit">
</form>

2 个答案:

答案 0 :(得分:0)

复选框的名称可能不同,因为它们不是每个输入标记的单选按钮。然后在PHP文件中使用$ _POST [&#34; keyword1&#34;]获取每个值,$ _ POST [& #34; keyword2&#34;]依此类推,然后检查它是否被以下代码选中

If($_POST["keyword1"]=='on'){
// Your Code
}

同样检查每个复选框是否创建所需的任何字符串并将其插入到DB中。

答案 1 :(得分:0)

我将删除此帖子,因为这是一个错字错误 $ param_keyword

由于我无法删除帖子,此问题已经解决,并且将值插入数据库效果很好

相关问题