[英]INSERT from checkboxes to MySQL - foreach statement
In the folowing code I am doing an insert from a form into a mysql table 在下面的代码中,我正在从窗体插入到mysql表中
The form fields are populated from an MySQL database, which work correctly. 表单字段是从MySQL数据库填充的,可以正常工作。
The problem is extracting data from the multiple checkboxes and inserting each value as a new row in the same table 'selection' columns userid and videoid. 问题是从多个复选框中提取数据,并将每个值作为新行插入同一表“选择”列中的userid和videoid中。
The userid field is inserted correctly, but the videoid is not posting any data. 用户ID字段已正确插入,但视频ID未发布任何数据。
<?php
$con=mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect");//connection string
$user=$_POST['userid'];
$checkbox1=$_POST['videoid'];
$chk="";
foreach($checkbox1 as $chk1)
{
$chk .= $chk1.",";
}
$in_ch=mysqli_query($con,"INSERT INTO tbl_selection (userid, videoid) VALUES ('$user', '$chk');");
if($in_ch==1)
{
echo'<script>alert("Inserted Successfully")</script>';
}
else
{
echo'<script>alert("Failed To Insert")</script>';
}
}
?>
</body>
</html>
This is the html form which is populated from a mysql table: 这是从mysql表填充的html表单:
<?php
connect to database
?>
<div class="control-group">
<?php
$query = "SELECT * FROM video";
$result2 = mysql_query($query);
while ($line = mysql_fetch_array($result2, MYSQL_ASSOC)) {
?>
<div class="controls">
<label class="checkbox">
<input type="checkbox" name="videoid" value="<?php echo $line[id]?>"><?php echo $line[title]?>
</label>
</div>
<?php } ?>
1) Change 1)变更
<input type="checkbox" name="videoid" value="<?php echo $line[id]?>"><?php echo $line['title']?>
To 至
<input type="checkbox" name="videoid[]" value="<?php echo $line[id]?>"><?php echo $line['title']?>
Means, for multiple checkbox. 表示多个复选框。 Use name
as array
type. 使用name
作为array
类型。 Like videoid[]
. 就像videoid[]
。
2) Use for
or foreach
loop for inserting multiple checkbox value into a table. 2)使用for
或foreach
循环将多个复选框值插入表中。 First, find out the checked checkbox through sizeof
. 首先,通过sizeof
找到选中的复选框。 Then, use accordingly. 然后,相应地使用。
Updated Code 更新的代码
<?php
$con=mysqli_connect("$host", "$username","$password","$db_name")or die("cannot connect");
$user=$_POST['userid'];
$videoCheckBox=$_POST['videoid'];
$checkedVideo = sizeof($videoCheckBox);
for($i=0;$i<$checkedVideo;$i++) {
$videoId = $videoCheckBox[$i];
$queryVideo = mysqli_query($con,"INSERT INTO `tbl_selection` (`userid`, `videoid`) VALUES ('$user', '$videoId');");
}
if($checkedVideo == 0) {
echo'<script>alert("Failed To Insert")</script>';
} elseif($queryVideo) {
echo'<script>alert("Inserted Successfully")</script>';
}
?>
</body>
</html>
<div class="control-group">
<?php
$query = "SELECT * FROM video";
$result2 = mysql_query($query);
while ($line = mysql_fetch_array($result2, MYSQL_ASSOC)) {
?>
<div class="controls">
<label class="checkbox">
<input type="checkbox" name="videoid[]" value="<?php echo $line['id']?>"><?php echo $line['title']?>
</label>
</div>
<?php } ?>
For more info, please click Inserting data into mySQL table from mutlidimensional input form 有关更多信息,请单击从多维输入表单向MySQL表插入数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.