Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

insert multiple choices of checkboxes in php

Hi
i have this html line and i want to insert it into mysql database and if the user choose 2 or 3 checkboxes how i can insert them
<td>Extra Call</td>
<td><input type="checkbox" name="wk1" value="wk1">Week1
<input type="checkbox" name="wk2" value="wk2">Week2
<input type="checkbox" name="wk3" value="wk3">Week3
<input type="checkbox" name="wk4" value="wk4">week4
 </td></tr>
any help please
0
asaidi
Asked:
asaidi
  • 5
  • 5
1 Solution
 
Marco GasiFreelancerCommented:
Change checkboxes' names with array notationthis way:

<td>Extra Call</td>
<td><input type="checkbox" name="wk1" value="wk1">Week1
<input type="checkbox" name="wk2[]" value="wk2">Week2
<input type="checkbox" name="wk3[]" value="wk3">Week3
<input type="checkbox" name="wk4[]" value="wk4">week4
 </td></tr>

then place in the script which has to update database this code

if (isset($_POST['wk2'])){
  foreach ($_POST['wk2'] as $v){
    $sql = "UPDATE myTable SET field=$v";
    mysql_query($Sql);
  }
}

Please, describe how your table is structured if you want more detailed code: this one is dirty but take you in the right direction :-)

Cheers
0
 
asaidiAuthor Commented:
Hi
i will have simple table that i call extra
has id 9primary key) reference for the customer linked to the customer table ,wk1,wk2,wk3,wk4 as wk is a week
0
 
Marco GasiFreelancerCommented:
Sorry, I have not been clear about what I wish: show, if you can, how you would update your table if the value would be 'wk4' :-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
asaidiAuthor Commented:
sorry
i want that the user can choose either one or more means he can choose wk1 and wk2 and so
0
 
Marco GasiFreelancerCommented:
Yes, I understood: I only see how is you wiuld update your table if the value would be 'wk4', so I'll insert your own code wiothin my example and it will be more clear - hope
0
 
asaidiAuthor Commented:
yes wk4
0
 
Marco GasiFreelancerCommented:
Well, it seems there is some communication issue ;-)
I re-post my first comment using data you gave me:

<td>Extra Call</td>
<td><input type="checkbox" name="wk1" value="wk1">Week1
<input type="checkbox" name="wk2[]" value="wk2">Week2
<input type="checkbox" name="wk3[]" value="wk3">Week3
<input type="checkbox" name="wk4[]" value="wk4">week4
 </td></tr>

then place in the script which has to update database this code

if (isset($_POST['wk2'])){
  foreach ($_POST['wk2'] as $v){
    $sql = "UPDATE extra SET $v='" . $v . "'";
    mysql_query($Sql);
  }
}

This should work if in your table you have a series of fields called wk1, wk2, wk3 and so on. If you want, to can use export function of phpMyAdmin and show only the CREATE statement of the resulting file, so your table structure will be more clear...
0
 
Ray PaseurCommented:
You can probably find some of the answers here.  Please read it over and post back if you still have specific questions about how checkboxes work.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html

To help with the data base part we would need to see your CREATE TABLE statement.
0
 
asaidiAuthor Commented:
hi
and why you put wk2 in the test and the same in the loop
i think can i put the my first html
<td><input type="checkbox" name="wk1" value="wk1">Week1
<input type="checkbox" name="wk2" value="wk2">Week2
<input type="checkbox" name="wk3" value="wk3">Week3
<input type="checkbox" name="wk4" value="wk4">week4
 </td></tr>
and test if checked then i insert it into my table
the structure will be wk1 as char(4) the same for others and in insert i will insert the value
i hope i am understable
0
 
Marco GasiFreelancerCommented:
You're right!!! It was my mistake: do this

<td>Extra Call</td>
<td><input type="checkbox" name="wk1" value="wk1">Week1
<input type="checkbox" name="wk[]" value="wk2">Week2
<input type="checkbox" name="wk[]" value="wk3">Week3
<input type="checkbox" name="wk[]" value="wk4">week4
 </td></tr>

then place in the script which has to update database this code

if (isset($_POST['wk'])){
  foreach ($_POST['wk'] as $v){
    $sql = "UPDATE extra SET $v='" . $v . "'";
    mysql_query($Sql);
  }
}

PHP considers only checked checkboxes, so if user checked one or more checkboxes only these values wiull be inserted in the array wk. The code loops through this array and insert that value in the correspondent field. About this, I would made those fields as boolean, that is TinyInt with a default value set to 0 and doing this:

if (isset($_POST['wk'])){
  foreach ($_POST['wk'] as $v){
    $sql = "UPDATE extra SET $v=1";
    mysql_query($Sql);
  }
}

Then you can check if the vbalues for each wk (1, 2, 3...) is 1 or 0

Cheers
0
 
asaidiAuthor Commented:
hi
thanks but instead of update i did insert and it works perfect
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now