Entering Checkbox data into MySQL using Implode()

Hello, I have one checkbox.

<input type="checkbox" name="vehicle[]" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle[]" value="Cycle"> I have a cycle

And here is the PHP code.

<?php
require("conn.php");
 
// Escape user inputs for security
$name = mysqli_real_escape_string($conn, $_POST['name']);
$desc = mysqli_real_escape_string($conn, $_POST['desc']);
$cars = mysqli_real_escape_string($conn, $_POST['cars']);
$gender = mysqli_real_escape_string($conn, $_POST['gender']);
//$vehicle = mysqli_real_escape_string($conn, $_POST['vehicle']);
$vehicle = implode(',', $_POST['vehicle']);
 
// attempt insert query execution
$sql = "INSERT INTO tbl_project2 (Name, Description, Cars, Gender, Own) VALUES ('$name', '$desc', '$cars', '$gender', '$vehicle' )";
if(mysqli_query($conn, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
 
// close connection
mysqli_close($conn);
?>

But its not working.

Can anybody please guide me on how to add checkbox data to MySQL.

Thank you.

Can you expand on “it’s not working”? Do you get an error, does it not insert the record, does it insert the record but not the checkbox values?

I would advise strongly against structuring the database to contain more than one value in a single column. It will be a pain to recover the values from it later on - how will you recover all rows for people who own a bike, easily? You should either have a separate table to show what vehicles each person owns linked to the other table by owner_id, or at the very least a separate column for owns_bike and owns_cycle. Whatever the difference between a bike and a cycle is, aren’t they the same thing?

Check out “Database Normalisation” for lots to read on the subject.

2 Likes

You should also try and add some validation of the user submitted data, also have a read up about prepared statements - they’re far better the any real_escape_string functions

2 Likes

I think that you must use

       
        foreach ($_POST['vehicle'] as $checkBoxValue) {
        
   
    $vehicle=$_POST['vehicle'];
1 Like

Hello DS, Thank you very much for your help. I get no error. The data is just not inserted in the Database Table Column… That column (named Own) stays as blank.

DS, I am just a total newbie to this PHP MySQL thing. This is my first project which I am just practicing on my own. So what you said in the 2nd para is extremely correct. That thing never came to my mind :slight_smile:

Yes I would now create a new table and link that table to the first table by owner_id. Will check out Database Normalisation too.

Thank you :slight_smile:

try echo-ing $sql and run it through phpmyadmin or something.

Ohh I am so sorry… I don’t know what’s wrong with me… The implode() was not working but now when I restarted XAMPP and everything… its working indeed.

Yes its taking data as bike,cycle but as Droopsnoot pointed out its not a good way to go and use RDBMS instead.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.