Check Box Array Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Check Box Array Problem

Post by gjb79 »

I'm having trouble getting my dynamic check boxes to become an array which I will use to update an sql database.

Here is the form code:

Code: Select all

<input name="selectedfile[]" type="checkbox" value="<?php echo $row_filelist['ID']; ?>">
Here is the insert into database part:

Code: Select all

<?php
if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "addfiles")) {
for( $i = 0; $i < sizeof( $selectedfile ); $i++ ){ 
  $insertSQL = sprintf("INSERT INTO filetopage (page, file) VALUES (%s, %s)",
                       GetSQLValueString($HTTP_POST_VARS['deletedpageid'], "int"),
                       GetSQLValueString($HTTP_POST_VARS['selectedfile[$i]'], "int"));

  mysql_select_db($database_cms, $cms);
  $Result1 = mysql_query($insertSQL, $cms) or die(mysql_error());
 }
}
?>
There error I am getting is
Column 'file' cannot be null
I do not understand why it gives this error. I tested it prior to adding the database info, by having it echo the results of the array, and it worked perfectly. It showed me all the values stored in the array.

Any ideas?

Thanks
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Not too sure because it looks ok from here. The only thing I could suggest is changing this..

Code: Select all

sizeof( $selectedfile );
..to this..

Code: Select all

count( $selectedfile );
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Post by gjb79 »

Tried it with no success. Thanks anyways!

Ok When I remove the insert command and run the following script

Code: Select all

<?php
for( $i = 0; $i < sizeof( $selectedfile ); $i++ ){ 
        print( $selectedfile[$i] . " was checked<br>" ); 
} 
?>
I get this:
5 was checked
6 was checked
When I insert the newly modified insert sql command

Code: Select all

<?php
if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "addfiles")) {
for( $i = 0; $i < sizeof( $selectedfile ); $i++ ){ 

  $insertSQL = sprintf("INSERT INTO filetopage (page, file) VALUES (%s, %s)",
                       GetSQLValueString($HTTP_POST_VARS['deletedpageid'], "int"),
                       $selectedfile[$i]);
} 
?>
I no longer get the error that column 'file' cannot be null, HOWEVER, It does not insert a new row for each item in the array as I am trying to achieve.

Yet the print command still returns the same result, showing that there are multiple items in the array.


Whats up with this?
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Loops?

Post by gjb79 »

Anybody know of a loop that might work better than the "for" loop I am using?

Maybe a While loop?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

foreach() maybe?
Post Reply