Forum Moderators: coopster

Message Too Old, No Replies

Question about $_POST variables from a Web Form

Accessing the variables to insert into a MYSQL database

         

gardfish

5:08 am on Oct 13, 2003 (gmt 0)

10+ Year Member



I'm getting information from a web form where the data is dynamically generated. The name=value pairs are both being used as variables where name is the uid and value is the value (either Complete, Incomplete, or N/A).

I'm trying to figure out a way to implement the following:

if ($_POST) {
reset ($_POST);
list($key, $val) = each($_POST);
}
$x=0;
while {$key[$x]) {
$result = mysql_query("UPDATE ".$tablename." SET column2=".$val[$x]." where uid=".$key[$x].";", $db) or die("Invalid query: " . mysql_error());
$x++;
}

Obviously $key and $val are not arrays so this doesn't work. Any ideas on how to enumerate $key and $val or split $_POST into two arrays would be appreciated.

Thanks.

MonkeeSage

5:55 am on Oct 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, gardfish!

$key = array();  
$value = array();
foreach ($_POST as $k => $v) {
array_push ($key, $k);
array_push ($value, $v);
}

That should do it if you just want an array of keys and an array values from $_POST.

Jordan

jatar_k

6:19 am on Oct 13, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld gardfish,

what about

foreach ($_POST as $k => $v) {
$result = mysql_query("UPDATE ".$tablename." SET column2=".$v." where uid=".$k, $db) or die("Invalid query: " . mysql_error());
}

you also don't need the semi colon when you pass the query and if $v or $k are strings you will need to put single quotes around them ;)

coopster

1:50 pm on Oct 13, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



jatar_k is right in that
you also don't need the semi colon when you pass the query

but I wanted to add that the PHP manual specifically states

The query string should not end with a semicolon. [us3.php.net]

Best to follow the rules ;)

gardfish

2:36 pm on Oct 13, 2003 (gmt 0)

10+ Year Member



Thanks for your help and welcome.

In case anyone else might find this useful, I've used the following:

foreach ($_POST as $k => $v) {
if ($k!=$v) {
$result = mysql_query("UPDATE ".$tablename." SET column2=\"".$v."\" where uid=".$k, $db) or die("Invalid query: " . mysql_error());
}
}

Adding the if statement removes the Submit:Submit name value pair that gets posted with the rest of the data.

Once again...thanks for the help.