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
asaidiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
asaidiAuthor Commented:
hi
thanks but instead of update i did insert and it works perfect
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.