tags:

views:

128

answers:

3

hey i have registration form that has checkboxes along with other fields. i cant insert the selected checkbox values into the data base. i have made one field in the database for storing all checked values. this is the code for checkbox part in the form:

<pre><input type="checkbox" name="expertise[]" value="Websites,IT and Software"> Websites, IT and Software   <input type="checkbox" name="expertise[]" value="Writing and Content"> Writing and Content </pre>
<pre><input type="checkbox" name="expertise[]" value="Design and Media"> Design and Media            <input type="checkbox" name="expertise[]" value="Data entry and Admin"> Data entry and Admin </pre>
<pre><input type="checkbox" name="expertise[]" value="Engineering and Skills"> Engineering and Science     <input type="checkbox" name="expertise[]" value="Seles and Marketing"> Sales and Marketing </pre>
<pre><input type="checkbox" name="expertise[]" value="Business and Accounting"> Business and Accounting     <input type="checkbox" name="expertise[]" value="Others"> Others </pre>

and this is the corresponding php code for inserting data

$checkusername=mysql_query("SELECT * FROM freelancer WHERE fusername='{$_POST['username']}'");

if (mysql_num_rows($checkusername)==1)
{
  echo "username already exist";
}
else
{
  $query = "insert into freelancer(ffname,flname,fgender,femail,fusername,fpwd,fphone,fadd,facc,facc_name,fbank_details,fcity,fcountry,fexpertise,fprofile,fskills,fhourly_rate,fresume) values

  ('".$_POST['first_name']."','".$_POST['last_name']."','".$_POST['gender']."','".$_POST['email']."','".$_POST['username']."','".$_POST['password']."','".$_POST['phone']."','".$_POST['address']."','".$_POST['acc_num']."','".$_POST['acc_name']."','".$_POST['bank']."','".$_POST['city']."','".$_POST['country']."','".implode(',',$_POST['expertise'])."','".$_POST['profile']."','".$_POST['skills']."','".$_POST['rate']."','".$_POST['resume']."')";

  $result =

  ($query) or die (mysql_error());

this code inserts data for all fields but the checkbox value field remains empty???

A: 

You've defined in HTML an array via "[]" (expertise[]) and in PHP you use value $_POST['expertise']. I suggest using:

 <input type="checkbox" name="expertise" value="Design and Media" /> Design and Media   

EDIT:

Try use var_dump($_POST['expertise']) to view the content of variable $_POST['expertise']. The function often helps a lot.

MartyIX
i tried this but its still not inserting checkbox values
rabeea
Well, you're a step ahead. Test what value you get in $_POST['expertise'] and then add the code for inserting to the database.
MartyIX
You've got two choices: 1) Use expertise[] in html and then you can't simply put all the values in one record in DB because one column should hold one value (you can bypass it but it's often bad practice). 2) Use radio button instead of checkbox to let the user choose one item and then you can add the value to the database with your current SQL query.
MartyIX
A: 

In this expertise contains array, so use this

$exp = $_POST["expertise"];
$fulexp = implode(",", $exp);
// echo $fulexp;

It will give the correct results of all checkbox valu

Karthik
this is not working too
rabeea
i worked this after that only i provided the solution. Please check that.. my output are appear like this, if i select two means output will beData entry and Admin,Seles and MarketingPlease check once again.If you use striaghtly in your database means in insert query itself you can use likeimplode(",", $_POST["expertise"]);
Karthik
A: 

But $_POST["expertise"]; is only going to return the last checked box checked by the user if you change the checkbox name from expertise[].

Jamie