Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

By this code i can able insert the checkbox values to database. But i need add one column in phpmyadmin and in that column i need store the values like, if i select 2 checkbox, i need store that ckeckbox values in one column in another column i need store for selected checkbox as YES and unselected values as NO. But see i want both column

here is my code

<input type="checkbox" name="checkbox[]" value="Home" id="pageHome" onChange="toggleVisibility('home');" /><label for="pageHome"> Home</label><img id="home" src="images/icon-tick.png"  style="visibility:hidden"/><br/>

<input name="checkbox[]" value="About_us" id="page_Aboutus" type="checkbox" onChange="toggleVisibility('aboutus');"><label for="page_Aboutus"> About Us</label><img id="aboutus" src="images/icon-tick.png"  style="visibility:hidden" /><br/>

<input name="checkbox[]" value="Services" id="pageServices" type="checkbox" onChange="toggleVisibility('services');"><label for="pageServices"> Services</label><img id="services" src="images/icon-tick.png"  style="visibility:hidden" /><br/>

<input name="checkbox[]" value="Products" id="pageProducts" type="checkbox" onChange="toggleVisibility('products');"><label for="pageProducts"> Products</label><img id="products" src="images/icon-tick.png"  style="visibility:hidden"/><br/><br>

<input name="checkbox[]" value="Enquiry" id="pageEnquiry" type="checkbox" onChange="toggleVisibility('enquiry');"><label for="pageEnquiry"> Enquiry</label><img id="enquiry" src="images/icon-tick.png"  style="visibility:hidden"/><br/><br>

<input name="checkbox[]" value="Contact_us" id="pageContact" type="checkbox" onChange="toggleVisibility('Contact');"><label for="pageContact">Contact Us</label><img id="Contact" src="images/icon-tick.png"  style="visibility:hidden" /><br/>

php code

$required_pages = implode(',', $_REQUEST['checkBox']);

$sql="insert into request_quote(customer_name,organisation,phone_num,email,country,state,city,zipcode,project_type,website_url,website_purpose,website_keyword,Competitors,sample_websites,no_of_updation,required_pages,additional_page,other_details) 
        values('$customer_name','$organisation','$phone_num','$email','$country','$state','$city','$zipcode','$project_type','$website_url','$website_purpose','$website_keyword','$Competitors','$sample_websites','$no_of_updation','$required_pages','$additional_page','$other_details')";
mysql_query($sql) or die(mysql_error());
share|improve this question
2  
Please make your question into more than two sentences and one looong sentence. It is hard to read :) –  Andreas Storvik Strauman Apr 25 at 12:25
add comment

2 Answers

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-security
foreach ($_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.

share|improve this answer
    
how can i insert the loop values to database –  user3422452 Apr 25 at 12:40
    
Via the $checkBoxes-array. As I just added (right above the security tip). :) –  Andreas Storvik Strauman Apr 25 at 12:41
    
You the $checkBoxes-array is the equivalent of $required_pages in your code. –  Andreas Storvik Strauman Apr 25 at 12:43
    
but i need two columns in database. one column has the checkbox values other column has the "YES" or "no" –  user3422452 Apr 25 at 12:49
    
The checkbox value is YES or NO. The checkbox, names, however is set in the SQL query right after INSERT INTO request_quote(. I'm just to lazy to add all the fields :) –  Andreas Storvik Strauman Apr 25 at 12:50
add comment

You have to use 3 tables, and relationate each other

  1. table 1 -> customers (id, name, etc)
  2. table 2 -> required_pages (id, name) - here you will add your options from checkbox (Home, About_us, etc)
  3. table 3 -> customers_required_pages (id, customer_id, required_page_id)

When saving data, you will have to save sequentialy:

  1. Your customer

    $sql = "INSERT INTO customers (field1, field2, fieldn...)" $qry = mysql_query($sql);

  2. Retrieve your customer Id

    $sql = "SELECT LAST_INSERT_ID() AS customerId"; $qry = mysql_query($sql); $customerId = mysql_fetch_assoc($qry);

  3. Save the relationship between customers and required_pages


<?php
foreach ($_REQUEST['checkBox'] as $oneCheckbox => $checkboxValue) {
    $sql = "INSERT INTO customers_required_pages (customer_id, required_page_id) VALUES ({$customerId['customerId']}, '{$checkboxValue}')"; 
}
?>

  1. 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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.