Page 1 of 1

Check Box Array Problem

Posted: Tue Nov 25, 2003 8:39 pm
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

Posted: Tue Nov 25, 2003 9:42 pm
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 );

Posted: Tue Nov 25, 2003 10:23 pm
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?

Loops?

Posted: Wed Nov 26, 2003 5:19 am
by gjb79
Anybody know of a loop that might work better than the "for" loop I am using?

Maybe a While loop?

Posted: Wed Nov 26, 2003 5:42 am
by m3mn0n
foreach() maybe?