Link to home
Start Free TrialLog in
Avatar of DReade83
DReade83Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Submitting several dynamic checkboxes using PHP

I need to be able to take multiple checkbox values from a form, submitted using PHP, and post them into a DB. I have some code below but it doesn't work. Can anyone help me get this working?

Thanks in advance!
<?
if(isset($_POST['form'])) {
    
    # Has checkbox been ticked ?
    if(isset($_POST['checkbox']) && !empty($_POST['checkbox'])) {
        
        # Simple mysql_connect function
        $this->ConnectDB();
        
        foreach($_POST['checkbox'] as $box) {
        
            # Generate query
            $sql = sprintf('UPDATE Table SET TableCheckbox = %d', $box);
            
            # Execute query
            $query = mysql_query($sql);
        }
    }
}
?>

Open in new window

Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Does the checkboxes have different names, or are all named "checkbox" ?

Is the snippet a part of a class definition? If not, you can not use $this->ConnectDB();

This statement:

foreach($_POST['checkbox'] as $box)

This will only work if you have multiple checkboxes named "checkbox[]". The [] prefix tells PHP that this is an array.

This SQL: UPDATE Table SET TableCheckbox = %d

This will update all rows and make TableCheckbox contain the value of the last checkbox only.

What kind of values do the checkboxes have? They must be numeric to make the %d in the SQL work, if not use '%s' in the SQL.

You say you want to "post them into a DB", do you mean insert them as new records, or update existing records? If you want to update, you must also have a way to identify each checkbox, so you know which is which. Put this identification in a WHERE clause in the SQL. If you want to insert new rows, use INSERT, not UPDATE.
Avatar of DReade83

ASKER

Thanks for your reply.

This snippet is part of another class, sorry for not mentioning that.

Basically I want to update existing records in the database - the WHERE clause will be the checkbox value and the product ID it's associated with (see new snippet). The query used is an example, it's not what I actually have in mind. For now though I just need to be able to get the PHP to accept the checkbox values as an array, and execute the SQL query for each "checked" checkbox only. The ones that aren't checked shouldn't be included. The checkbox values are all numeric.

Hope this helps, let us know if you need anything else.

Many thanks.
<?
$sql = sprintf('UPDATE Products SET Enabled = 1 WHERE ProductId = %d', $box);
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Un-checking will not be allowed with this code, only checking...?
Sorry I don't. Is that all I need?
Yes, I think so. Everything else looks good. :)
Awesome! I'll give that a go now and report back soon... :-)
Excellent, it works! Thanks for your time and help! :D