Welcome Guest, Not a member yet? Register   Sign In
How to Handle Multiple Checkbox Values When Posting to the DB
#1

[eluser]AndrewTurner[/eluser]
Hey Everybody,

I've run into a bit of a problem when trying to parse multiple checkbox data and post that to a database.

I'm using a bit of a complicated db setup, but I'm storing the standard input data in one table and then for each checkbox selected it's given it's own row with the output of the value of the checkbox.

I've got the generation of the checkboxes setup alright (code below), and It's running through a foreach loop as the data being parsed to the checkboxes is from the database.

Code:
<?php echo form_checkbox('product-sides',$product_sides['product_sides_id'], FALSE); ?>

But I've run into a problem, I've tried serializing the checkbox data (posted value) and storing them in an array, etc but they just aren't working and are returning insert problems.

What would be the best way to setup the output of the checkboxes to be posted to the db?

I've grabbed this sections Psudeocode from the rest for those interested...

Quote:BEGIN
WHILE Checkbox is Selected DO
Get Checkbox Value (product_id)
Save product_id to Database
ENDWHILE
END
(the product_id in brackets indicates value being saved as a variable)

Any help is very much appreciated, thanks!
#2

[eluser]Georgi Budinov[/eluser]
Can you explain a bit more on how and why you serialize data and what insert problems do you have ? Perhaps show us a little real code.
#3

[eluser]AndrewTurner[/eluser]
Sure.

Serialising was the wrong term (sorry there), I was trying separating the posted values and then trying to place them into an array to have that try and be posted into individual records. That wasn't working, as it was just returning insert errors in regards to trying to post the whole array into the DB.

In terms of code, What is it you'd like to see? Controller Function, Model, etc?
#4

[eluser]bretticus[/eluser]
[quote author="AndrewTurner" date="1279815472"]
But I've run into a problem, I've tried serializing the checkbox data (posted value) and storing them in an array, etc but they just aren't working and are returning insert problems.[/quote]

Try ...
Code:
<?php echo form_checkbox('product-sides[]',$product_sides['product_sides_id'], FALSE); ?>

In PHP, if you name each checkbox 'product-sides[]', the combined values of those checkboxes will be treated as an array when posted.
#5

[eluser]AndrewTurner[/eluser]
Thanks very much. It's been a long time since I've done this normally in PHP, Any suggestions on the best way to separate the array as to be posted to separate records? (The posting part I'm okay with, just the separation part) - Arethere any CI functions/libraries/etc?
#6

[eluser]bretticus[/eluser]
Once the data is posted, it's a native PHP array and you'd handle it the same as any other typical array. No magic needed really. For example:

Code:
<?php
$sql = "INSERT INTO mytable (myvalue) VALUES ";
$sql .= "('" . implode("'),('", $_POST['product-sides']) . "')";
// ci query call.
$this->db->query($sql);
?>

I'm using an implode to facilitate multi-row insert syntax in MySQL. I could have used a foreach to create separate insert statements however. Note: this is sample code and whether it works depends on what you have. As for suggestions on which code to post, you might try the code you are struggling with. :-)
#7

[eluser]Phil Sturgeon[/eluser]
MySQL injecterific.
#8

[eluser]bretticus[/eluser]
[quote author="Phil Sturgeon" date="1279819400"]MySQL injecterific.[/quote]

Well sure, besides all the security vulnerabilities imposed by my example, it is just an example. Smile
#9

[eluser]AndrewTurner[/eluser]
[quote author="bretticus" date="1279819150"]Once the data is posted, it's a native PHP array and you'd handle it the same as any other typical array. No magic needed really. For example:

Code:
<?php
$sql = "INSERT INTO mytable (myvalue) VALUES ";
$sql .= "('" . implode("'),('", $_POST['product-sides']) . "')";
// ci query call.
$this->db->query($sql);
?>

I'm using an implode to facilitate multi-row insert syntax in MySQL. I could have used a foreach to create separate insert statements however. Note: this is sample code and whether it works depends on what you have. As for suggestions on which code to post, you might try the code you are struggling with. :-)[/quote]

The only code really I was having problems with was a method as to how to run through the array and so on, I've been working at it for days and funnily enough it just never came to mind Tongue

Thanks for you're help, So far with the code and a few minor changes to modify it to a foreach loop it seems to be working as planned, so thanks there.

[quote author="Phil Sturgeon" date="1279819400"]MySQL injecterific.[/quote]

[quote author="bretticus" date="1279819495"][quote author="Phil Sturgeon" date="1279819400"]MySQL injecterific.[/quote]

Well sure, besides all the security vulnerabilities imposed by my example, it is just an example. Smile[/quote]

Agree, I might be silly, but not silly enough to leave it as is with the example. I'll modify and chop and change it around. Once It's using CI's db classes for the input, I'll post up the results.




Theme © iAndrew 2016 - Forum software by © MyBB