[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 743
  • Last Modified:

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

0
DReade83
Asked:
DReade83
  • 4
  • 4
1 Solution
 
Roger BaklundCommented:
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.
0
 
DReade83Author Commented:
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

0
 
Roger BaklundCommented:
Do you have [] prefix on the names of the checkboxes in the html:
<input type="checkbox" name="checkbox[]" value="<?php echo $row['ProductId']; ?>" />

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Roger BaklundCommented:
Un-checking will not be allowed with this code, only checking...?
0
 
DReade83Author Commented:
Sorry I don't. Is that all I need?
0
 
Roger BaklundCommented:
Yes, I think so. Everything else looks good. :)
0
 
DReade83Author Commented:
Awesome! I'll give that a go now and report back soon... :-)
0
 
DReade83Author Commented:
Excellent, it works! Thanks for your time and help! :D
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now