0

i have searched a lot, but didn't find any solution. i want to save checkbox selected options in SQL. if user selects two check boxes thay must save in MySQL with comma separated.

Kinly please help me in solving this

HTML CODE

<input name="patno" id="patno">
<input type="checkbox" name="newt[]" value="Diesel" id="type" />Diesel
<input type="checkbox" name="newt[]" value="Petrol" id="type" />Petrol
<input type="checkbox" name="newt[]" value="Electricity" id="type" />Electricity

PHP CODE

$order = "INSERT INTO tblsampo
            (patno, stno, newt)
            VALUES
            ('$smonth',
            '$patno','$stno','$newt')";

$result = mysql_query($order);  //order executes
if($result){
    echo("<br>Input data is succeed");
} else{
    echo("<br>Input data is fail");
}
?>
4

3 回答 3

1

Simply implode the $_POST['type'] array like so....

$types = implode(",", $_POST['type']);

Then insert that $types variable into your table...

So...

$types = implode(",", $_POST['type']);

if($types){
$order = "INSERT INTO tblsampo
        (patno, stno, type)
        VALUES
        ('$smonth',
        '$patno','$stno','$types')";}
于 2013-06-05T19:47:25.643 回答
1

Try this

$types = implode(',',$_POST['type']);
$sql = "INSERT INTO tblsampo
        (patno, stno, type)
        VALUES
        ('$smonth',
        '$patno','$stno','$types')";
mysql_query($sql)
于 2013-06-05T19:52:57.063 回答
0

try the following:

$newt = false;
if (isset($_POST['newt']) && !empty($_POST['newt']) && is_array($_POST['newt'])){
   $newt = implode(",", $_POST['newt']);
}

then just run your query same way but check if $myTypes is not false first

if ($newt) {
  //YOUR QUERY. TIP: use PDO or MySQLi instead of MySQL_* functions
}
于 2013-06-05T19:48:44.050 回答