473,586 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a checkbox array

6 New Member
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.p hp") 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.ph p 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=62 84. 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 2165
dlite922
1,584 Recognized Expert Top Contributor
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
Joni Seth
6 New Member
Hi. Thanks for your reply. Here is some of my page source:

<tr><input type="hidden" name="check_sub mit" value="1"/>
<input type="hidden" name="user_skil lID" value="6191"/>
<td align="center"> &nbsp;&nbsp;619 1</td>
<td align="left">&n bsp;&nbsp;Acces s</td>
<td align="left">&n bsp;&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_sub mit" value="1"/>
<input type="hidden" name="user_skil lID" value="6192"/>
<td align="center"> &nbsp;&nbsp;619 2</td><td align="left">&n bsp;&nbsp;Acces s</td>
<td align="left">&n bsp;&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_sub mit" value="1"/>
<input type="hidden" name="user_skil lID" value="6193"/>
<td align="center"> &nbsp;&nbsp;619 3</td><td align="left">&n bsp;&nbsp;Acces s</td>
<td align="left">&n bsp;&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_sub mit" 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=62 84

<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&gtArra y" 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 Recognized Expert Top Contributor
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
Joni Seth
6 New Member
@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.use r_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_esca pe_string($skil l) 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
8189
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 request.form("chkAddToInvoice").count" method to go through the array, but it only counts checked boxes. Not only is this causing the corresponding Update statement to...
3
333
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
1835
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 assuming because it thinks I'm pointing to an array, not creating an array of pointers. How would I fix this?
1
2152
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 with column 1 & 2, but column 3 gives me this error :- Control 'CHECKBOX1' of type 'CheckBox' must be placed inside a form tag with runat=server. ...
10
1588
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 Command1_Click(Index As Integer) Select Case Index
4
3878
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 array to my stop_subscriptions.php file, running the code on all checked boxes, however I dont seem to be able to actually send the array, I was hoping...
3
3622
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 along with another fixed variable and pass them along in an email. For demonstration purposes, I have checked the Wed and Fri checkboxes and set the...
0
1641
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 in .NET. I am using an ARCGIS map control which has "Layers" of spatial data (i.e. Roads, Creeks, Rivers, etc.) When I load the form and the map...
3
1580
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 boolMatrix is changed to true and if it is unchecked it is changed to false. However; this is not my problem. The size of the matrices aren't fixed. I...
3
5300
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 class="modalData" id="multipleCommSelect" style="padding: 50px;"> <center>Please select the communities from the list below:<br></center> <label><input...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.