Solved

Inserting / updating multiple checkbox values into a field using php / mysql

Posted on 2007-12-05
14
12,092 Views
Last Modified: 2013-12-12
Hi, I've been trying to set up a form using checkboxes to update multiple values into a field database (MYSQL) using php.  Can multiple values be inserted into a single field name within a table?, and if so, how can I insert them using checkboxes??  I've included the code example to give you an idea of where I am.......

Cheers
<?php
$names = $_REQUEST['category'];
foreach ($arr as $names)  {
 
$updateSQL = sprintf("UPDATE Table SET category='%s' WHERE id=%s", $names, (int)$_SESSION['user']->getID());
}
?>
 
<form name="updateForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 
<input type="checkbox" name="category[]" value="orange"></input>Orange
<input type="checkbox" name="category[]" value="purple"></input>purple
 
<input type="submit" name="sender" value="Save Profile">
 
</form>

Open in new window

0
Comment
Question by:Simbob
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 20413488
You want to add multiple rows ? or add all the values in a single row ?
0
 

Author Comment

by:Simbob
ID: 20414071
I want to add all or some of the checked values in a single row preferably.  What do you think the best solution / code is for this?
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20415476
Hi Simbob,

Is serialization an option?

If you serialize the array, and want want to retrieve it later, you can unserialize() it using the unserialize() function, which turns it back into an array.

Hope this helps.

<?php
$names = serialize($_REQUEST['category']);
$updateSQL = sprintf("UPDATE Table SET category='%s' WHERE id=%s", $names, (int)$_SESSION['user']->getID());
?>
 
<form name="updateForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 
<input type="checkbox" name="category[]" value="orange"></input>Orange
<input type="checkbox" name="category[]" value="purple"></input>purple
 
<input type="submit" name="sender" value="Save Profile">
 
</form>

Open in new window

0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:Simbob
ID: 20418625
Hi, I've tried the serialize option, and still I'm getting array being posted with no values.

How do developers usually store / post multiple values from checked lists?  I could always create separate field names to store the checked values, but then this seems like a long-winded route and more complicated to retrieve...

Any further ideas?
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20418869
Simbob what are you trying to achieve ? ... serializing the data and storing it in  the database will make the information unusable until they are retrieve and unserialized.

E.g

SELECT * FROM table WHERE catagory = 'blue' will not work.

0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20418944
Again i dont know what your and goal is ... but the standard design for this would be

TABLE users
user_id
category_id

TABLE categories
category_id
category_name

TABLE users_categories
user_id
category_id

0
 

Author Comment

by:Simbob
ID: 20419449
Sorry for sound amateurish :0)

Basically I'm trying to post multiple checked values into a single field for e.g

if orange and purple are both checked, then post / update these values as "orange purple" into the ['category' ] field in the users 'Table'

My database structure is simply:

TABLE: users
FIELD NAME: category

Hope this helps in my explanation.  
Cheers
0
 
LVL 2

Expert Comment

by:jaydm
ID: 20429806
You would probably want to build up the list of categories first - then do the update.

Here is an example (of course you know that with this code sample, there is no actual execution of the sql).
<?php
	if (isset($_POST["submit"])) {
		$categories = "";
 
		for ($i=0; $i < count($_POST['category']); $i++) {
			$categories = categories . $_POST['category'][$i] . " "; 
 
		$updateSQL = sprintf("UPDATE Table SET category='%s' WHERE id=%s", $categories, (int)$_SESSION['user']->getID());
	}
?>
 
<form name="updateForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 
<input type="checkbox" name="category[]" value="orange"></input>Orange
<input type="checkbox" name="category[]" value="purple"></input>purple
 
<input type="submit" name="sender" value="Save Profile">
 
</form>

Open in new window

0
 
LVL 2

Expert Comment

by:jaydm
ID: 20430160
Oops, I forgot the closing brace from the for loop.
<?php
        if (isset($_POST["submit"])) {
                $categories = "";
 
                for ($i=0; $i < count($_POST['category']); $i++) {
                        $categories = categories . $_POST['category'][$i] . " "; 
                }
 
                $updateSQL = sprintf("UPDATE Table SET category='%s' WHERE id=%s", $categories, (int)$_SESSION['user']->getID());
        }
?>
 
<form name="updateForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 
<input type="checkbox" name="category[]" value="orange"></input>Orange
<input type="checkbox" name="category[]" value="purple"></input>purple
 
<input type="submit" name="sender" value="Save Profile">
 
</form>

Open in new window

0
 
LVL 2

Expert Comment

by:jaydm
ID: 20430200
I won't repost the code, but if (isset(... should be:

if (isset($_POST["sender"])) {

I didn't correct the variable name to reflect the name you have in the form.
0
 

Author Comment

by:Simbob
ID: 20442502
Ok, thats great jaydm - how would I build up the categories first as you mentioned??  

I've used the code as you explained, but when I post the information I only manage to update the last checkbox record instead of all checked checkboxes....

I've read somewhere else that a foreach loop could be used - would this be relevant in this case?

Cheers.
0
 
LVL 2

Accepted Solution

by:
jaydm earned 500 total points
ID: 20443494
Sorry, I had a typo on the "$categories = $categories....." line.

I forgot the '$' after the equals sign.

The following should have all of the problems corrected.
<?php
        if (isset($_POST["sender"])) {
                $categories = "";
 
                for ($i=0; $i < count($_POST['category']); $i++) {
                        $categories = $categories . $_POST['category'][$i] . " "; 
                }
 
                $updateSQL = sprintf("UPDATE Table SET category='%s' WHERE id=%s", $categories, (int)$_SESSION['user']->getID());
        }
?>
 
<form name="updateForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 
<input type="checkbox" name="category[]" value="orange"></input>Orange
<input type="checkbox" name="category[]" value="purple"></input>purple
 
<input type="submit" name="sender" value="Save Profile">
 
</form>

Open in new window

0
 

Author Comment

by:Simbob
ID: 20443656
Excellent!  It works now!!

Thanks for your help Jaydm
0
 

Expert Comment

by:boypetiks
ID: 20747164
Hi, I'm trying to build a form which include a couple of text fields and check boxes, the purpose of the form is basically for reporting, data can be added and deleted, example:
text fields such as: first name, last name, street address, city, state, zip
check boxes: Purchase, Cashout, Refinance, Second Mortgage, Others

I don't have problem saving the data for each text fields, my problem is how can I save the selected value for each checkboxes, so I could retrieve and print the report later on. thank you for your help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question