In the folowing code I am doing an insert from a form into a mysql table
The form fields are populated from an MySQL database, which work correctly.
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.
The userid field is inserted correctly, but the videoid is not posting any data.
<?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:
<?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 } ?>
your chosen video is $checkbox1
"); - which returns a value, but the value is not being inserted correctly. The problem must be in the **foreach** statement. – dkjoe May 15 '16 at 06:08