473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Creating a checkbox array

I have a database of required skills for employees. There is a many to many relationship between the users table and the skills table, called user_skills. It contains the following fields: auto increment ID called user_skillsID, the foreign keys userID from the users table, and skillID from the skills table, a date field called tngdate and a "set" field called achieved. I have a form (called "updateskills.php") that the employees can open which brings up their un-achieved skills and presents a checkbox for them to check the skills that they can perform. The updateskills.php form action goes to "processskills.php", and when a checkbox is checked and the submit button is clicked, "processskills.php" opens, and an echo of the query displays: UPDATE user_skills SET achieved = 'Yes'WHERE user_skillID=6284. The problem is that I have clicked the skill with the user_skillID= 6191, not 6284. 6284 is the last skill in the list. For my checkbox, I have the code as:

<input type="checkbox" name="achieved[]" value="Yes">

which is supposed to create an array, but it appears that it is NOT creating an array. I do not know how to fix this. No matter what I do, it keeps updating only the last record, no matter how many checkboxes I select. Each checkbox, however, does appear when I echo the $_REQUEST. Does anyone have any suggestions? I will be glad to post additional code if need be, but all else seems to be working correctly. Thank you in advance.
Jul 21 '10 #1
4 2135
dlite922
1,584 Expert 1GB
You must have misspelled something somewhere.

The following code works just as you described:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4.  
  5. if(isset($_REQUEST['foo']))
  6. {
  7.     die(print_r($_REQUEST['foo']));
  8. }
  9.  
  10. ?>
  11. <html>
  12. <body>
  13. <form action="" method="post">
  14.  
  15. <input name="foo[]" type="checkbox" value="1" /> 1 <br />
  16. <input name="foo[]" type="checkbox" value="2" /> 2<br />
  17. <input name="foo[]" type="checkbox" value="3" /> 3<br />
  18. <input name="foo[]" type="checkbox" value="4" /> 4<br />
  19.  
  20. <input type="submit" />
  21. </form>
  22. </body>
  23. </html>
  24.  
Check to see if all your check box's values are not the same. (Right Click -> View Source in your browser)


Dan
Jul 21 '10 #2
Hi. Thanks for your reply. Here is some of my page source:

<tr><input type="hidden" name="check_submit" value="1"/>
<input type="hidden" name="user_skillID" value="6191"/>
<td align="center">&nbsp;&nbsp;6191</td>
<td align="left">&nbsp;&nbsp;Access</td>
<td align="left">&nbsp;&nbsp;Can you export data from a database?</td>
<td align="center"><input type="checkbox" name="achieved[]" value="Yes"></td></tr>
<tr><input type="hidden" name="check_submit" value="1"/>
<input type="hidden" name="user_skillID" value="6192"/>
<td align="center">&nbsp;&nbsp;6192</td><td align="left">&nbsp;&nbsp;Access</td>
<td align="left">&nbsp;&nbsp;Can you run an existing query on a database?</td>
<td align="center"><input type="checkbox" name="achieved[]" value="Yes"></td></tr>
<tr><input type="hidden" name="check_submit" value="1"/>
<input type="hidden" name="user_skillID" value="6193"/>
<td align="center">&nbsp;&nbsp;6193</td><td align="left">&nbsp;&nbsp;Access</td>
<td align="left">&nbsp;&nbsp;Can you run an existing report on a database?</td>
<td align="center"><input type="checkbox" name="achieved[]" value="Yes"></td></tr>
<tr><input type="hidden" name="check_submit" value="1"/>

It appears that the checkboxes actually do have the same name. Somehow they need to get associated with the user_skillID, which is what they are actually updating as achieved.

Playing around with some echo statements, here is what is displayed after I click three checkboxes:

Yes
Yes
Yes

Your skills: 6284

UPDATE user_skills SET achieved = 'Yes'WHERE user_skillID=6284

<pre&gtArray ( [check_submit] => 1 [user_skillID] => 6284 [achieved] => Array ( [0] => Yes [1] => Yes [2] => Yes ) [Submit] => Submit ) 1</pre>

<pre&gtArray ( [fname] => Patricia [lname] => Canning [username] => pcanning ) 1</pre>
1achieved skill(s) are updated in the database.

The first 3 "Yeses" are displaying each checkbox I checked.

The "Your skills: is echo "Your skills {$_POST['user_skillid']}.

The UPDATE user_skills is my query echo - here is where the rub appears: only one record, the LAST record, is selected to be updated, even though I selected three checkboxes, which shows me that some how the array is wrong, since my "pre&gtArray" display does show an array of something, but apparently it isn't right since it isn't doing what I need it to do. That's my problem, I don't know how to tell it to do what I need it to do!
Jul 22 '10 #3
dlite922
1,584 Expert 1GB
You need to put the skill_id where you put "Yes". You see how in my example that I gave you, I had different values? 1,2,3,4? if those were all the same, I'd have no idea what was selected!

skill_id must be the value of the checkbox.

Right now you have 3 check boxes all of them have the same name and the same value. So that's what you're going to get.

If you have checkboxes of the same name[] but different values, you will get all the values that were checked.

Here's some rough pseudocode of what you need to do:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. if($_POST['submit'])
  4. {
  5.   $skills = $_POST['achieved']; 
  6.  
  7.   // connect to mysql
  8.  
  9.   foreach ($skills as $skill)
  10.   {
  11.     $skillID = mysql_real_escape_string($skill); 
  12.     $sql = "UPDATE user_skills SET achieved = 'Yes' WHERE user_skillID = "$skillID"; 
  13.  
  14.   }
  15. }
  16. ?>
  17. <html>
  18. <body>
  19. <form action="" method="post" name="form1">
  20. Some Skill: <input type="checkbox" name="acheived[]" value="6484" />Yes
  21. <br/>
  22. Another Skill: <input type="checkbox" name="acheived[]" value="1234" />Yes
  23. <br/>
  24. ...
  25. <input type="submit" name="submit"/>
  26. </form>
  27. </body>
  28. </html>
  29.  
Got it?




Dan
Jul 23 '10 #4
@dlite922
Hi Dan,
Thank you for taking the time to post your reply. What you posted is exactly what I'm having trouble with. The first problem seems to be that the field "user_skill.user_skillID" really doesn't equal anything else in the database. It is an auto increment field in a join table for a many-to-many relationship. $skillsID can't equal mysql_real_escape_string($skill) because it's not text, it's a number. The skills that appear for a user to check that they are achieved are acquired from a query, so how can I enter the value when I don't know what that value will be?
Anyway, no matter how I enter the code, I get a blank white page when I run it, so it seems I have something messed up somewhere. Good try though.
Jul 26 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: WC Justice | last post by:
I have an ASP form that uses a recordset to build a table, with one of the columns containing a checkbox. Upon posting, the ASP code of the Post-To page uses the "For i = 1 to...
3
by: Paul Auleciems | last post by:
Hi: I'm having trouble using an Object which is created based on the following: Public CarDetail () as Car Where the CLASS "Car" is defined as: Public Class Car
7
by: Mark | last post by:
i have a "tile" class. i want to create a 2d-array of tiles. they should point to actual tiles to avoid storing repetitive data. ex. Tile *map; map = &tile_dirt; This does not work. I'm...
1
by: Paul | last post by:
HI I have a asp page which dynamically creates a table with 28 rows, 3 columns. Column 1 contains a label, column 2 contains a graphic, column 3 needs to contain a checkbox. I have no problems...
10
by: Tor Inge Rislaa | last post by:
Creating Control Array How to create an array of buttons, with common procedures based on the index of the control. How would this Example from VB 6.0 be in VB.NET? Private Sub...
4
by: Paul Morrison | last post by:
Hi, I have a checkbox array containing the id of a record in a MySQL database. The checkboxes are created dynamically depending on what is stored in the database. I want to send the checkbox...
3
by: JackM | last post by:
Okay, I'm starting to get a little ticked off because I've worked for hours on this and I can't seem to find the cause. I'm using PHP 5.1.6. I'm trying to get the values of some form checkboxes...
0
by: Nolanclark | last post by:
Hi there. I've read a previous thread regarding the Old VB 6 checkbox array and how it's not really needed any more. That's fine, but I'm not really sure how to implement the checkbox control array...
3
by: Jakes | last post by:
I need to associate the elements of a bool type matrix with the state of a checkbox from an array that i've created. For example: if the element checkBoxMatrix is checked then the the element in...
3
realin
by: realin | last post by:
Hiya all, i am in deep trouble, i thought it was an easy task, but now its getting on my nerves. Well i have a div on a form, which contains a number of checkboxes, as <div...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.