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");
}
?>
user2457175
  • 31
  • 1
  • 7
  • Side note, IDs **must** be unique. – j08691 Jun 05 '13 at 19:44
  • @sircapsalot this - http://stackoverflow.com/questions/15341639/how-to-save-checkbox-value-into-mysql-database-using-php and few many. but no use. they are confusing me a lot :( – user2457175 Jun 05 '13 at 19:46
  • Once you get the type array, `implode` it before storing in the database. Also, the `mysql_*` functions are deprecated and should not be used and your checkbox elements need to have unique ids if that are going to have ids – keithhatfield Jun 05 '13 at 19:46
  • 1
    avoid using php closing tags if its the end of file since rendering it might cause some problems if there are some spaces afterwards. – GGio Jun 05 '13 at 19:59

3 Answers3

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')";}
Kylie
  • 11,421
  • 11
  • 47
  • 78
  • ID's of what must be unique?? Each entry? Simply add a PRIMARY INDEX to your sql table, and auto increment – Kylie Jun 05 '13 at 19:48
  • Oh yeah, and you should be using PDO instead of the deprecated mysql_* functions – Kylie Jun 05 '13 at 19:50
  • If you look at the given HTML code, all three checkboxes have `id="type"` ... while this does not affect functionality of the form submission, it is incorrect. I believe that is what j08691 was referring to – keithhatfield Jun 05 '13 at 19:54
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
}
GGio
  • 7,563
  • 11
  • 44
  • 81
  • thanks for taking time for this question. i had just changed my question and updated name="newt" can you please update your answer with 'newt' please! – user2457175 Jun 05 '13 at 19:52
0

Try this

$types = implode(',',$_POST['type']);
$sql = "INSERT INTO tblsampo
        (patno, stno, type)
        VALUES
        ('$smonth',
        '$patno','$stno','$types')";
mysql_query($sql)
baig772
  • 3,404
  • 11
  • 48
  • 93