两个或多个不同的复选框如何在数据库中存储值


Two or more different checkboxes how to store value in database

我有一个包含文本和数字输入字段的表单,还有一些复选框。我可以从输入字段将文本输入数据库,但当我有超过1个复选框时,我无法使用它。这是我表格中4个代码中的2个。

    <dl class="inputCheckbox">
        <dt><label for="interests">Nyt/brugt:</label></dt>
        <dd>
            <input type="checkbox" name="newUsed[]" value="Nyt" />
            <label for="new" class="opt">Nyt</label>
            <input type="checkbox" name="newUsed[]" value="Brugt" />
            <label for="used" class="opt">Brugt</label>
            <input type="checkbox" name="newUsed[]" value="Nyt/Brugt" />
            <label for="both" class="opt">Nyt/Brugt</label>
        </dd>
    </dl>
    <dl class="inputCheckbox">
        <dt><label for="interests">Diverse:</label></dt>
        <dd>
            <input type="checkbox" name="various[]" value="El" />
            <label for="electricity" class="opt">El</label>
            <input type="checkbox" name="various[]" value="Vand" />
            <label for="water" class="opt">Vand</label>
            <input type="checkbox" name="various[]" value="El/Vand" />
            <label for="both" class="opt">El/Vand</label>
        </dd>
    </dl>

我希望有一些人可以帮助我使用一些PHP代码:)

以下是我如何将标签中的文本和数字等其他内容输入数据库的代码。

<?php
session_start();
include('connection.php');
$cvr=$_POST['cvr'];
$cvr=$_POST['companyName'];
$fName=$_POST['fName'];
$lName=$_POST['lName'];
$streetName=$_POST['streetName'];
$streetNumber=$_POST['streetNumber'];
$zip=$_POST['zip'];
$city=$_POST['city'];
$phone=$_POST['phone'];
$eMail=$_POST['eMail'];
$repeatEmail=$_POST['repeatEmail'];
$locationNumber=$_POST['locationNumber'];
$size=$_POST['size'];
$products=$_POST['products'];
$comments=$_POST['comments'];
$newUsed=$_POST['newUsed'];
$various=$_POST['various'];
$sleep=$_POST['sleep'];
$reOrder=$_POST['reOrder'];
mysql_query("INSERT INTO creatUser(cvr, fName, lName, streetName, streetNumber, zip, city, phone, eMail, repeatEmail, locationNumber, size, products, comments)
    VALUES('$cvr', '$fName', '$lName', '$streetName', '$streetNumber', '$zip', '$city', '$phone', '$eMail', '$repeatEmail', '$locationNumber', '$size', '$products', '$comments')");
header("location: creatUser.php?remarks=success");
mysql_close($con);
?>

您可以用逗号分隔的格式保存复选框。

在你的PHP脚本中,你可以做

$checkboxData = implode(",", $_POST['various']);

当重新填充您的数据时,您可以在视图中进行

<?php $checkboxValues = explode(",", $checkboxData); ?>
<input type="checkbox" name="various[]" value="El" <?php if(in_array("EL", $checkboxValues)) echo 'selected="selected";?>/>

你能试试吗,

 $various=$_POST['various'];
 $VariousData ='';
 foreach($various as $k=>$val){
   $VariousData .=$val." ";
 }

您可以使用将$VariousData存储到您的表

您将不得不以某种方式序列化复选框输入。通常使用CCD_ 2和CCD_。使用您的代码:

$newUsedSerialized = implode(",", $_POST["newUsed"]); # Sanitize $_POST["newUsed"] first!

然后,使用将您的输入返回到阵列中

$newUsedArray = explode(",", $queryResult["newUsed"]);

因此,您可以在PHP代码中使用$newUsedArray来生成表单:

$inputField = "";
$inputField .= '<input type="checkbox" name="newUsed[]" checked="' . (in_array("Nyt", $nytUsedArray) ? "checked" : "") . '/>';

(在这种情况下,请考虑使用数字values,这样可以节省空间。)