I am new to php/mysql and I am learning quite a bit. I have hit a snag with my checkboxes. I have looked and everything I have come across is not making sense to me.
Pretty much I am doing a site where the admin can add a user and there are checkboxes that will say what the user is interested in. (more than one can be selected.
Example Interested in what sports. (these are checkboxes users can select)
- Baseball
- Football
- Hockey
and so on
How can I have it where the choices are stored in the database?
Here is what I have so far.
HTML
<div class="col-md-8">
<label style="margin-right:10px; width:130px"><input name="interested[]" type="checkbox" value="a6"><span class="cats">Baseball</span></label>
<label style="margin-right:10px; width:130px"><input name="interested[]" type="checkbox" value="a6"><span class="cats">Football</span></label>
<label style="margin-right:10px; width:130px"><input name="interested[]" type="checkbox" value="a6"><span class="cats">Hockey</span></label>
</div>
PHP
<?php
//error_reporting(E_ALL);
//ini_set("display_errors", 1);
// Pick up the form data and assign it to variables
$id = @$_POST['id'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$altemail = $_POST['altemail'];
$notes = $_POST['notes'];
$company = $_POST['company'];
$address = $_POST['address'];
$home = $_POST['home'];
$cell = $_POST['cell'];
$telephone = $_POST['telephone'];
$category = $_POST['category'];
$usertype = $_POST['usertype'];
$assigned = $_POST['assigned'];
$othercat = $_POST['othercat'];
$interested=$_POST['interested'];
//Get data in local variable
$id = @$_POST['id'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$altemail = $_POST['altemail'];
$notes = $_POST['notes'];
$company = $_POST['company'];
$address = $_POST['address'];
$home = $_POST['home'];
$cell = $_POST['cell'];
$telephone = $_POST['telephone'];
$category = $_POST['category'];
$usertype = $_POST['usertype'];
$assigned = $_POST['assigned'];
$othercat = $_POST['othercat'];
$interested=$_POST['interested'];
// You have to loop through the array of checked box values ...
$interested="";
foreach($interested as $entry){
$interested .= $entry.",";
}
if ($fname=="" || $email=="")
{
echo "All fields must be entered, hit back button and re-enter information";
}else{
$query="INSERT INTO users(`id`, `fname`, `lname`, `email`, `notes`,`company`,`address`,`cell`,`home`,`telephone`,`category`,`usertype`,`assigned`,`altemail`,`othercat`,`interested`) VALUES('$id','$fname','$lname','$email','$notes','$company','$address','$telephone','$category','$usertype','$assigned','$altemail','$othercat','$cell','$home','$interested')";
Like I mentioned, I am brand new to PHP/Mysql. I have only been doing it for about 8 days now. I have come quite a ways with it but this has me stumped.
Any help would be appreciated. Not trying to put myself down, but with all the others that I have looked at and no being able to grasp, please kind of dumb it down for me.
Thanks in advance.