Link to home
Start Free TrialLog in
Avatar of asaidi
asaidi

asked on

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
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

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
Avatar of asaidi
asaidi

ASKER

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
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' :-)
Avatar of asaidi

ASKER

sorry
i want that the user can choose either one or more means he can choose wk1 and wk2 and so
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
Avatar of asaidi

ASKER

yes wk4
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...
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.
https://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.
Avatar of asaidi

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asaidi

ASKER

hi
thanks but instead of update i did insert and it works perfect