Page 1 of 1

POST issues with a certain variable name

Posted: Tue Apr 14, 2009 10:41 pm
by awilki01
I've looked at this, and I just cannot figure it out.

My PHP code outputs the following HTML snippet on my form page. There are several iterations of this depending on the number of lines output from the previous form's SQL query. The number on the end of each name is incremented by one for each row.

Code: Select all

 
<td><input type="text" name="client_as1" value=""</td>
<td><input type="text" name="interface1" value=""</td>
<td><input type="text" name="interface_speed1" value=""</td>
<td><input type="text" name="interface_encapsulation1" value=""</td>
 
The form posts this to my next page when the user hits 'submit'. However, for some reason, I cannot import the 'interfaceX' variable. Here is my next page's PHP code where I have some echo commands for debugging ($x is a looping variable....loop not shown):

Code: Select all

 
 $client_as = $_POST[client_as . $x];
 $interface = $_POST[interface . $x];
 $interface_speed = $_POST[interface_speed . $x];
 $interface_encapsulation = $_POST[interface_encapsulation . $x];
...
...
 echo $client_as;
 echo "\n";
 echo $interface;
 echo "\n";
 echo $interface_speed;
 echo "\n";
 echo $interface_encapsulation;
 echo "\n";
 
When leaving this as is, I get the following error after hitting 'submit' on the first form page:
Parse error: syntax error, unexpected T_INTERFACE, expecting ']' in /home/user123/www-develop/path/path/filename.php on line 38
When I comment out the following, everything works fine:

Code: Select all

 
//$interface = $_POST[interface . $x];
 
What is wrong with the $interface variable here? Is there something awry with using that variable with other two interface_xxxxxxx variables?

Thanks in advance!!!!

Re: POST issues with a certain variable name

Posted: Tue Apr 14, 2009 10:49 pm
by requinix

Code: Select all

$client_as = $_POST[client_as . $x];
What is "client_as" supposed to be? A constant? Because it really looks like one.

Strings have quotes around them.

Re: POST issues with a certain variable name

Posted: Tue Apr 14, 2009 10:56 pm
by awilki01
tasairis wrote:

Code: Select all

$client_as = $_POST[client_as . $x];
What is "client_as" supposed to be? A constant? Because it really looks like one.

Strings have quotes around them.

"client_as" is a number the user inputs between 1 and 65535 on the previous form page.

The code,

Code: Select all

$client_as = $_POST[client_as . $x];
works fine.

For the first iteration of the loop, it is basically doing this: $client_as = $_POST[client_as1];

The second iteration does this: $client_as = $_POST[client_as2];

...and so on.

Each loop iteration moves the SQL pointer to the new row of data thereby updating the appropriate database records.

I just cannot figure out why the one line I mention in my original post doesn't work when all the rest do. I am wondering if it has something to do with the variable name "interfaceX" as compared to "interface_speedX" and "interface_encapsulationX". Is the underscore causing issues with the one that has no underscore? I know its a stretch, but I have seen odder things.

Re: POST issues with a certain variable name

Posted: Tue Apr 14, 2009 11:16 pm
by php_east
awilki01 wrote:When I comment out the following, everything works fine:

Code: Select all

 
//$interface = $_POST[interface . $x];
 
interface is a reserved word in PHP. $interface is fine but $_POST[interface . $x] is interpreted as PHP Internal interface instruction. you know what to do. i would recomend you use quotes for all $_POST vars.

Re: POST issues with a certain variable name

Posted: Wed Apr 15, 2009 8:23 am
by awilki01
Thanks so much!

Can you give me an example of using the quotes? Where in the $_POST would I use the quotes?

And, just curious, why? Is it a best practice or something?

Re: POST issues with a certain variable name

Posted: Wed Apr 15, 2009 8:51 am
by php_east
the detailed explanation is in the PHP manual,
$_POST[interface . $x] is interpreted as an interface command.

$_POST['interface'. $x] would be what you want.

as explained by tasairis
tasairis wrote:

Code: Select all

$client_as = $_POST[client_as . $x];
What is "client_as" supposed to be? A constant? Because it really looks like one.
Strings have quotes around them.
$POST[myname] is wrong, but it works if myname exists.
the correct version is $POST['myname'] or $POST["myname"]