php 在单个数据库列中插入多个HTML表单复选框值

r6l8ljro  于 2022-12-28  发布在  PHP
关注(0)|答案(2)|浏览(106)

我正在使用Bootstrap 4制作一个HTML表单,其中包含三个多项选择题,允许读者使用复选框选择多个答案。

    • 下面是表格中一个问题的示例。**
<div class="container">
    <form action="/survey/insert.php" class="was-validated" method="post">
        <div class="form-group">
            <legend class="form-check-label" for="tmp-area"><b>Please select the nearest city, town, or area where you have detailed knowledge of roads, trails, and places on public land.</b></legend>

            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Black Canyon City, Bumblebee, Cortes Junction" />Black Canyon City, Bumblebee, Cortes Junction </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Bouse, Brenda, Parker" />Bouse, Brenda, Parker </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Kingman, Lake Havasu City" />Kingman, Lake Havasu City </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Littlefield, Masquite" />Littlefield, Masquite </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Arizona Strip, Parashant National Monument" />Arizona Strip, Parashant National Monument</label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Tuscon, Winkelman" />Tuscon, Winkelman </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Buckeye, Rainbow Valley" />Buckeye, Rainbow Valley </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Tonto National Forest " />Tonto National Forest </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Coconino National Forest" />Coconino National Forest </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Apache Sitgreaves National Forest" />Apache Sitgreaves National Forest </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"> <input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Prescott National Forest" />Prescott National Forest </label>
            </div>
        </div>
    </form>
</div>
    • 以下是**/survey/insert.php的示例
<?php
        //Database connection
        $servername = "localhost";
        $username = "user";
        $password = "password";
        $dbname = "name";
        $conn = new mysqli($servername, $username, $password, $dbname);

        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        // Taking all values from the form data(input)
        $tmp_area = $_REQUEST['tmp_area'];
        

        //Insert form data into table. Set table and columns.
        $sql = "INSERT INTO Survey1 (tmp_area) VALUES ('$tmp_area')";

        if ($conn->query($sql) === true) {
            echo "Your survey has been submitted";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
        ?>
    • 问题是**

当用户选择多个复选框时,只有一个复选框值被添加到数据库列。
我希望用户选择的ALL复选框值插入到相同的数据库列中。
我在网上找到的所有例子和教程都要求每个复选框都有自己的列。或者不详细说明如何实现这一点。
我的PHP知识有限,任何帮助都将非常感谢。:)

x4shl7ld

x4shl7ld1#

将临时区域更改为临时区域[]

<div class="form-check">
   <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area[]" value="Bouse, Brenda, Parker" />Bouse, Brenda, Parker </label>
 </div>

插入期间

// Taking all values from the form data(input)
$tmp_area= implode(",",$_REQUEST['tmp_area']); // Store this to your database

如果您需要tmp_area,则使用它再次转换为数组

$tmp_area[] = explode(",",$your_tmp_area);
luaexgnf

luaexgnf2#

我会建议一个替代的方式,如果你可以存储实际值在db.
就像这样:
将输入类型名称更改为数组类型。

<input type="checkbox" class=".." id=".." name="tmp_area[]" value=".." />

在插入数据库之前创建数组值。

$tmp_area = ""; // create a variable to store all data

$tmp_area = implode(',',$_REQUEST['tmp_area']); // convert array type to string

当你检索数据时,

$tmp_area[] = explode(",",$data_tmp_area_replace_here); // convert string data from database to array

相关问题