Talha Habib Talha Habib - 6 months ago 18
MySQL Question

how to insert multiple values with foreach

adding checkbox values using

foreach
. the checkbox name are same.. enter image description here

i tried this

<?php
// checkbox name is check[]
$check = $_POST['check'];
foreach($check as $checks => $checked)
{
$value = $checked;
$sql = mysql_query("INSERT INTO `db` (`db_column`) VALUES ('".$value."')");
}
?>

Answer
        <form name="form1" action="count.php" method="POST">
         <input type="checkbox" name="check[]" value="your value">
         <input type="checkbox" name="check[]" value="your value1">
         <input type="checkbox" name="check[]" value="your value2">
         <button type="submit" name="btn">Ok</button>
         </form>
         <?php
         if(isset($_POST['btn'])){
            $check = $_POST['check'];
            print_r($check);foreach($check as $checks => $checked){
                $value = $checked;

             $sql = mysql_query("INSERT INTO `db` (`db_column`) VALUES ('".$value."')"); 
            }
         }
         ?>