473,473 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

INSERT values into mysql table with PHP using checkboxes

2 New Member
Hello everyone,

I'm working on adding checkboxes to the edit_profile section where members should be able choose with checkboxes the multiple options of massage techniques offered... When I checked only one, either, option 1, or option 2 or option 3 I'm able to see it in the column in the correct table of mysql but when I check more then one box only the last box would insert into the column, ignoring the rest. In this case, if I check the three options, only lomi lomi would insert into the column... I hope you guys can help me. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. // set all checkboxes to false initially
  2.  
  3. $Deeptissue="";
  4.  
  5. $Swedish="";
  6.  
  7. $Lomilomi="";
  8.  
  9. $checkbox=$_POST['typeTech'];
  10.  
  11. //if appear in the POST, they are checked, set to true
  12.  
  13. foreach($checkbox as $typeTech ) {
  14.  
  15. if($typeTech=="deeptissue")
  16.  
  17. $Deeptissue="Deep Tissue";
  18.  
  19. else if($typeTech=="swedish")
  20.  
  21. $Swedish="Swedish";
  22.  
  23. else if($typeTech=="lomilomi")
  24.  
  25. $Lomilomi="Lomi Lomi";
  26.  
  27. }
  28.  
  29. echo "Technique checked: ".$Deeptissue."<br/>\n";
  30.  
  31. echo "Tchnique checked: ".$Swedish."<br/>\n";
  32.  
  33. echo "Technique checked: ".$Lomilomi."<br/>\n";
  34.  
  35.  
  36. $sql = mysql_query("INSERT INTO Members (Technique)
  37.  
  38. VALUES('$typeTech')")
  39.  
  40. or die (mysql_error());
Nov 1 '11 #1
1 1968
Dormilich
8,658 Recognized Expert Moderator Expert
there are 2 problems:
- with that SQL you can insert only one value (don’t know how inserting multiple values is intended to be)
- you use a variable from the foreach construct, naturally, after the loop is done, this will only have the last value.

how to solve the second problem depends on how you tackle the first one. generally, for the insert you could do with a few lines of verification and one line for the SQL.

Expand|Select|Wrap|Line Numbers
  1. // assuming there are only the checkboxes, omitting any verification/sanitisation
  2. $res = mysql_query("INSERT INTO Members (Technique) VALUES (" . implode("), (", $_POST) . ")";
Nov 1 '11 #2

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

Similar topics

2
by: aseaman | last post by:
I am a big novice with MySQL and PHP. I need a script that enters information from a html form into a MySQL database. Can anyone tell me where I can find one that I can just replace the table and...
1
by: Lars Erik Hansen | last post by:
I want to write/insert the value of foundFiles into a table. How can i paste a value into a table using VB?? Can someone help? I want to get "FoundFiles(i)" and paste it into a table Set...
2
by: Max | last post by:
Hi, I have the following problem. I want to insert records into one table using the combination of two other tables. Here is a simplified example: Let's say the first table has a column with...
3
by: Materialised | last post by:
Hello everyone, I am having a issue inserting values into a MYSQL table, and for the life of me, I can figure out why. I know the connection is successful, however I am getting errors. The...
1
by: Sirisha | last post by:
Hello, I want code for insert values into database using storedprocedure and how to write stored procedures. I want code in codebehined file, imean .aspx code Regards,
0
by: Sirisha | last post by:
Hi, I am inserting values into databse using sqlserver stored procedures. i wrote stored preocedure,but in codebehind file(.vb file) i dont know how to pass the parameters, i got error message...
3
by: printline | last post by:
Hello All I need a little help with a phph script to display some specific data from a mysql table. I have a mysql table with 4 columns and 10 rows. I want to display fx. data from row 4, 6, 8...
13
nabh4u
by: nabh4u | last post by:
hi, I am trying to insert multiple rows in a table using PL/SQL. I have two procedures and two tables. the first procedure gets the data from the source table, manipulates it and then sends the...
1
by: chennaibala | last post by:
hi frds... in my hiden textbox.i have following values... robert|true|true|false|arun|true|false|true|anu|true|true|false| i want to splits in to token and insert in mysql table in following...
1
pradeepjain
by: pradeepjain | last post by:
Hii guys, I have 2 tables in which data exists of a same user like his table1:login details and table2: his further details . I have a form where in his both login and rest of details...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.