Forum Moderators: coopster

Message Too Old, No Replies

not passing variables with foreach

         

trimast

1:32 pm on Aug 24, 2005 (gmt 0)

10+ Year Member



Hi all,

I'm using the foreach function to pass a multiple $_POST feilds, to a mysql db.

The problem is there are a couple of fields that i don't want to be passed into the db, i've done a bit of searching and found a few people suggest the unlink() function but i can't get that to work, so i was wondering if anybody knows if it is actually possible to remove specific $_POST variables from the array and if so, how do i go about doing it?

Failing that are there any better ways?

Many thanks,

Rob

lobo235

2:52 pm on Aug 24, 2005 (gmt 0)

10+ Year Member



You could do something like this:


foreach( $_POST as $val ) {
if( $val!= "something i don't want" ) {
mysql_query( "INSERT INTO `table` (`data`) VALUES ('".$val."')" );
}
}

gliff

7:08 pm on Aug 24, 2005 (gmt 0)

10+ Year Member



$_POST, despite it's big scary capital letter and underscore, is just an array. You can remove an element from an array by using the unset [us2.php.net] function.

unset($_POST['field1']);

unset($_POST['field2']);

unset($_POST['field3']);

trimast

10:55 am on Aug 29, 2005 (gmt 0)

10+ Year Member



Thanks both.

I knew there would be an easy solution, as always it just wasn't the same variant of a solution i'd tried!

Rob

trimast

8:09 pm on Aug 29, 2005 (gmt 0)

10+ Year Member



Hi,

Just one additional question, i seem to be getting a syntax problem:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'page_template VALUES ('three')' at line 1

I've tried to use the foreach option to update and insert data and i always seem to get his error on the last field. any ideas why?

If i post each value as a single post then the mysql will work fine, it just seems to be when i post the variables using foreach.

Thanks

Rob