POST issues with a certain variable name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
awilki01
Forum Newbie
Posts: 3
Joined: Tue Apr 14, 2009 10:29 pm

POST issues with a certain variable name

Post 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!!!!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: POST issues with a certain variable name

Post 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.
awilki01
Forum Newbie
Posts: 3
Joined: Tue Apr 14, 2009 10:29 pm

Re: POST issues with a certain variable name

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: POST issues with a certain variable name

Post 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.
awilki01
Forum Newbie
Posts: 3
Joined: Tue Apr 14, 2009 10:29 pm

Re: POST issues with a certain variable name

Post 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?
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: POST issues with a certain variable name

Post 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"]
Post Reply