How Insert The Multiple Checkbox Values Into Database Using Php
Solution 1:
You can add a value='YES' attribute to each of your checkboxes.
Then make an array where you assume no checkbox has been checked. Because when we iterate later, only the checked checkboxes will be sent via $_REQUEST.
$checkBoxes['customer_name']="NO";
$checkBoxes['organisation']="NO";
$checkBoxes['phone_num']="NO";
$checkBoxes['email']="NO";
$checkBoxes['country']="NO";
$checkBoxes['state']="NO";
$checkBoxes['city']="NO";
$checkBoxes['zipcode']="NO";
$checkBoxes['project_type']="NO";
$checkBoxes['website_url']="NO";
$checkBoxes['website_purpose']="NO";
$checkBoxes['website_keyword']="NO";
$checkBoxes['Competitors']="NO";
$checkBoxes['sample_websites']="NO";
$checkBoxes['no_of_updation']="NO";
$checkBoxes['required_pages']="NO";
$checkBoxes['additional_page']="NO";
$checkBoxes['other_details']="NO";
// Only the checked checkboxes wil be itterated below.// This will mean the $value would be YES no matter what.// So I write it literal for SQL-Injection-securityforeach ($_REQUEST['checkbox'] as$checkboxName=>$value){
$checkBoxes[$checkBoxName]="YES";
}
And in your SQL-Query, replace the variables with items from the array. I.E.
$sql="insert into request_quote(customer_name)
values('".$checkBoxes['customer_name']."')";
Security tip:
Also, directly inserting user input can be a huge security risk. Use mysql_real_escape_string($value) on all user-input-values you plan to use in a SQL-query.
You should look into $_POST. W3Schools will help you out: http://www.w3schools.com/php/php_forms.asp.
Solution 2:
You have to use 3 tables, and relationate each other
table 1 -> customers (id, name, etc)table 2 -> required_pages (id, name)- here you will add your options from checkbox (Home, About_us, etc)table 3 -> customers_required_pages (id, customer_id, required_page_id)
When saving data, you will have to save sequentialy:
Your customer:
$sql = "INSERT INTO customers (field1, field2, fieldn...)"$qry = mysql_query($sql);Retrieve your customer Id:
$sql = "SELECT LAST_INSERT_ID() AS customerId"; $qry = mysql_query($sql); $customerId = mysql_fetch_assoc($qry);Save the relationship between customers and required_pages
<?phpforeach ($_REQUEST['checkBox'] as$oneCheckbox => $checkboxValue) { $sql = "INSERT INTO customers_required_pages (customer_id, required_page_id) VALUES ({$customerId['customerId']}, '{$checkboxValue}')"; } ?>When retrieving your data, you will select values from table2 using left join with table3, this way you will be able to know wich values where or not selected from the checkbox list
$sql = "SELECT rp.id, rp.name, IF(crp.customer_id IS NULL, 'NO', 'YES') AS checked FROM required_pages rp LEFT JOIN customers_required_pages crp ON rp.id = crp.required_page_id";
As an extra, consider use MySQLi or PDO instead of mysql as your database library, since the use of mysql lib has been discouraged
Solution 3:
I was having the problem to store the multiple checkbox into the mysql table, but now it's working fine with this code:
<html><head><title>Registration Page</title></head><body><?phprequire("config.php");
?><?phpif(isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$hobbies = implode(",",$_POST['hobbies']);
echo$hobbies;
$country = $_POST['country'];
// echo $hobbies;//foreach($hobbies as $hobbies1){//echo $hobbies1;//$implode = implode(',',$hobbies1);$sql = "INSERT INTO `userinfo`(`fname`,`lname`,`gender`,`hobbies`,`country`)
values('$fname','$lname','$gender','$hobbies','$country')";
//}//$sql = "INSERT INTO `userinfo`(`fname`,`lname`,`gender`,`hobbies`,`country`) // values('$fname','$lname','$gender','$hobbies1','$country')";$query = mysql_query($sql) ordie(mysql_error());
if
($query){
echo"<h1>Data inserted .. Happy coding</h1>";
}else{
echo"<h1>Please check the problem</h1>";
}
}
//}?><formname="registration"method="post"action=""><tableborder="1"cellpadding="2"cellspacing="2"align="center"><tr><td>First Name:</td><td><inputtype="text"name="fname"required=""></td></tr><tr><td>Last Name:</td><td><inputtype="text"name="lname"required=""></td></tr><tr><td>Gender:</td><td><inputtype="radio"name="gender"value="male"required="">Male <inputtype="radio"name="gender"value="female" >Female</td></tr><tr><td>Hobbies:</td><td><inputtype="checkbox"name="hobbies[]"value="tennis">Tennis <br><inputtype="checkbox"name="hobbies[]"value="cricket">Cricket <br><inputtype="checkbox"name="hobbies[]"value="dance">Dance7 <br><inputtype="checkbox"name="hobbies[]"value="sketching">Sketching </td></tr><tr><td>Country:</td><td><selectname="country"><option >----select----</option><option>India</option><option>Pakistan</option><option>UAE</option></select></td></tr><tr><td><inputtype="submit"name="submit"value="Register me"></td></tr></table></form></body></html>
Post a Comment for "How Insert The Multiple Checkbox Values Into Database Using Php"