我正在使用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知识有限,任何帮助都将非常感谢。:)
2条答案
按热度按时间x4shl7ld1#
将临时区域更改为临时区域[]
插入期间
如果您需要tmp_area,则使用它再次转换为数组
luaexgnf2#
我会建议一个替代的方式,如果你可以存储实际值在db.
就像这样:
将输入类型名称更改为数组类型。
在插入数据库之前创建数组值。
当你检索数据时,