Here is my existing code i have my db looks like this
req table
test_id(auto increment) test_work varchar(50)
<?php
include_once '../connect.php'; //database connection
if($_COOKIE["Utype"]!="Logistic")
{
header("Location: login.php");
}
if(!isset($_POST['mat']))
{
$checkboxValue = false;
} else {
$checkboxValue = $_POST['mat'];
}
$values = implode(",", $_POST["mat"]);
$sql="Insert into req(test_work)
values ('$values')";
$result=mysql_query($sql);
?>
<script>
function toggle(source) {
checkboxes = document.getElementsByName('mat[]');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
$('input[type="checkbox"]').on('change', function(e){
if($(this).prop('checked'))
{
$(this).next().val(1);
} else {
$(this).next().val(0);
}
});
</script>
<form name="form" action="test.php" method="post">
<table border="1" width="200px" style="position:relative;top:0px;font-family:tahoma;font-size:12px;border-collapse:collapse">
<tr style="background-color:#f3f3f3">
<th width="2%"></th>
<th height="20px" width="15%">Work Order ID</th>
</tr>
<?php
$resource=mysql_query("Select * from test",$con);
while($result2=mysql_fetch_array($resource))
{
?>
<tr style="background-color:#ffffff">
<td align="center"><input type="checkbox" name="mat[]" id="mat" value="<?php echo $result2["id"]; ?>"/><?php echo $result2["id"]; ?></td>
<td><?php echo $result2["name"]; ?></td>
</tr>
<?php };?>
</table>
<input style="color:#FFFFFF;position:relative;width:70px"type="submit" name="submit" id="submit" value="Save" class="imgClass" height="50px">
</form>
This can insert only one checked checkbox. but if i check multiple checkboxes it only inserts one value.
How can I do single and also multiple(with comma separated) inserts? Like
as if i selected four checkbox from the array
req table
test_id(auto increment) test_work varchar(50)
23 1, 2, 3, 4
and it omits the comma when i only select one checkbox
req table
test_id(auto increment) test_work varchar(50)
23 1