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
  • Side note, IDs must be unique. Commented Jun 5, 2013 at 19:44
  • @sircapsalot this - stackoverflow.com/questions/15341639/… and few many. but no use. they are confusing me a lot :( Commented Jun 5, 2013 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 Commented Jun 5, 2013 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. Commented Jun 5, 2013 at 19:59

3 Answers 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')";}
Sign up to request clarification or add additional context in comments.

3 Comments

ID's of what must be unique?? Each entry? Simply add a PRIMARY INDEX to your sql table, and auto increment
Oh yeah, and you should be using PDO instead of the deprecated mysql_* functions
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
0

Try this

$types = implode(',',$_POST['type']);
$sql = "INSERT INTO tblsampo
        (patno, stno, type)
        VALUES
        ('$smonth',
        '$patno','$stno','$types')";
mysql_query($sql)

Comments

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
}

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.