我已经搜索了很多方法来将复选框值插入到mysql数据库中,其值以逗号分隔。但到目前为止我的尝试都没有,下面你可以看到我的代码:
HTML代码:
<form role="form" action="zone_detail1.php" method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="sc">Zone Name for Identification</label> <input required type="text" class="form-control" name="txtzonename" placeholder="Enter Name"> </div> <div class="form-group"> <label for="sc">Day(s) for Zone</label><br> <input type="checkbox" name="txtday" value="Monday" />Monday <input type="checkbox" name="txtday" value="Tuesday" />Tuesday <input type="checkbox" name="txtday" value="Wednesday" />Wednesday <input type="checkbox" name="txtday" value="Thrusday" />Thrusday <input type="checkbox" name="txtday" value="Friday" />Friday <input type="checkbox" name="txtday" value="Saturday" />Saturday <input type="checkbox" name="txtday" value="Sunday" />Sunday </div> <input type="submit" class="btn btn-default" name="btn_submit" value="Submit"/> </form>
<?php
include("db.php");
session_start();
if(isset($_POST['btn_submit']))
{
//retrieve our data from POST
$zone_name = $_POST['txtzonename'];
$zone_days = @mysql_real_escape_string(implode(',',$_POST['txtday']));
$sql="Insert into `zone` (zone_name , zone_turn) values('$zone_name', '$zone_days')";
$result=mysql_query($sql);
if($result){
$msg="Data Inserted Successfully..!!";
$_SESSION['msg'] = $msg;
header("location:zone_detail.php");
}
else {
echo "ERROR";
}
}
?>
答案 0 :(得分:0)
您应该指定
<input type="checkbox" name="txtday[]" value="Monday">Monday<br>
as array。
将[]
添加到txtday的所有名称,然后在php上使用array
进行操作。
之后,您可以在db的不同列处INSERT
值,或使用implode
并将值保存到一列中。
不要忘记将mysql
替换为mysqli
。另请注意,mysql_ *函数为officially deprecated,因此不应在新版本中使用码。您可以使用PDO或MySQLi。有关详细信息,请参阅this answer on SO。
HTML代码
<form role="form" action="zone_detail1.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="sc">Zone Name for Identification</label>
<input required type="text" class="form-control" name="txtzonename" placeholder="Enter Name">
</div>
<div class="form-group">
<label for="sc">Day(s) for Zone</label><br>
<input type="checkbox" name="txtday[]" value="Monday" />Monday
<input type="checkbox" name="txtday[]" value="Tuesday" />Tuesday
<input type="checkbox" name="txtday[]" value="Wednesday" />Wednesday
<input type="checkbox" name="txtday[]" value="Thrusday" />Thrusday
<input type="checkbox" name="txtday[]" value="Friday" />Friday
<input type="checkbox" name="txtday[]" value="Saturday" />Saturday
<input type="checkbox" name="txtday[]" value="Sunday" />Sunday
</div>
<input type="submit" class="btn btn-default" name="btn_submit" value="Submit"/>
</form>
PHP代码
<?php
include("db.php");
session_start();
if(isset($_POST['btn_submit']))
{
//retrieve our data from POST
$zone_name = $_POST['txtzonename'];
$zone_days = implode(',', $_POST['txtday']);
$sql="Insert into `zone` (zone_name , zone_turn) values('".$zone_name."', '".$zone_days."')";
$result=mysql_query($sql);
if($result){
$msg="Data Inserted Successfully..!!";
$_SESSION['msg'] = $msg;
header("location:zone_detail.php");
}
else {
echo "ERROR";
}
}
?>
答案 1 :(得分:0)
您在标记中缺少 name =“txtday []”。
在输入元素中,您应指定:
<input type="checkbox" name="txtday[]" value="Monday" />
像这样更新你的标记:
<form role="form" action="zone_detail1.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="sc">Zone Name for Identification</label>
<input required type="text" class="form-control" name="txtzonename" placeholder="Enter Name">
</div>
<div class="form-group">
<label for="sc">Day(s) for Zone</label><br>
<input type="checkbox" name="txtday[]" value="Monday" />Monday
<input type="checkbox" name="txtday[]" value="Tuesday" />Tuesday
<input type="checkbox" name="txtday[]" value="Wednesday" />Wednesday
<input type="checkbox" name="txtday[]" value="Thrusday" />Thrusday
<input type="checkbox" name="txtday[]" value="Friday" />Friday
<input type="checkbox" name="txtday[]" value="Saturday" />Saturday
<input type="checkbox" name="txtday[] value="Sunday" />Sunday
</div>
<input type="submit" class="btn btn-default" name="btn_submit" value="Submit"/>
</form>
您可以看到此tutorial供您参考。