I keep getting the word "Array" in my database. I have checkboxes in my form and am having a hard time with it. I've tried different things, and this is the latest coding I have. What am I doing wrong?
In my form, the checkboxes have the name "cats[]".
I don't know if the first part matters (I saw that online and tried using it with the rest just in case)
if(count($cats) > 0)
{
$str = implode(",", $cats);
}
And then the actual code
$cats = array();
// Not good?
//if(isset($_POST['submit']))
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['cats']))
{
$cats = $_POST['cats'];
}
$categories= $_POST['categories'];
// This saves it aa a string, as you can only save those values as string.
$str = $categories . ": " . implode(", ", $cats);
var_dump ($str);
}
And this for the database
$sql="INSERT INTO form_corpo_test (compagnie)
VALUES
('$_POST[cats]')";
EDIT:
This is the correct code (thank you guys for the help! I wish I could have marked two of your answers as the "best answer"). Hopefully this helped others (err, although they'll need to look into sql injections too of course).
$cats = array();
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['cats'])) {
$cats = implode(",", $_POST['cats'] );
}
$categories= $_POST['categories'];
$str = $categories . ": " . $cats;
}
And
$sql="INSERT INTO form_corpo_test (categories)
VALUES
('$str')";