Page 1 of 1

for loop for dynamic form var names

Posted: Thu Dec 23, 2004 3:20 pm
by Burrito
I have done this in the past and it worked beautifully, however now when I try to include a dynamic name for one of my POST vars, It's not coming up with a value:

here's what I have:

Code: Select all

<?php
for($i=1;$i<$_POST['scrollpos']+1;$i++){
					$sql = "INSERT INTO My_Table VALUES (".$m_id.",".$_POST['crs$i'].",'y',0,'blah',0)";
				$result = mysql_query($sql, $db)
				or trigger_error(mysql_error(),E_USER_ERROR);
				}
?>
and as I said before, I did something almost identical to this and it worked fine...see below:

Code: Select all

<?php
for($j=1;$j<=$getpl["tournamentplayernum"];$j++){

	mysql_query("insert into tour_players (
tournamentid,
teamid,
playername,
ipadd,
todaysdate
)
values
(
".$_GET["id"].",
".$_POST["teamid"].",
'".mysql_escape_string($_POST["player$j"])."',
'".$_SERVER["REMOTE_ADDR"]."',
now()
)
")
	or trigger_error("could not insert to players table".mysql_error(),E_USER_ERROR);
}
?>
now though, for the first one, when I echo $_POST['crs$i']...I get nothing. If I hardcode the value for $i in there (ex: "1") then it works just fine.

can someone assist here?

I'm at a complete loss???

thx,

Burr

Posted: Thu Dec 23, 2004 3:39 pm
by kettle_drum

Code: Select all

$_POST['some_string'.$some_var];
I.e. dont put the variable in the quotes.

Posted: Thu Dec 23, 2004 3:43 pm
by John Cartwright
atleast not single quotes... double quotes are fine

Posted: Thu Dec 23, 2004 3:52 pm
by Burrito
Kettle, tried what you suggested and my machine about had a heart attack.

phenom, did it with dbl quotes and it worked beatitfully...ty very much.

can you explain to me why there's a difference though?

I thought that using single or dbl was essentially the same and you'd use them for other things.

ie $bob = "my name's bob";
or $bob = 'my name is "bob"';

so you could insert the other quote type in the string w/o having to escape them with \.

apparently there is more to it than that...can you let me know what it is?

thx again for the help,

Burr