Can't update a document: T_ENCAPSED_AND_WHITESPACE error

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
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Post by Sindarin »

I am trying to update the variable values in a vars.php document by completely rewriting it. Everything seems ok, but I get an error when I submit the form and execute the update.php. Error code is:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/myname/public_html/sys/update.php on line 6


code of update.php:

Code: Select all

<?
 
//Update vars
 
$vars_file=fopen("vars.php","w");
fwrite($vars_file,"<?$duration1=$_POST['dura1'];$location1=$_POST['tech1'];$duration2=$_POST['dura2'];$location2=$_POST['tech2'];$duration3=$_POST['dura3'];$location3=$_POST['tech3'];$duration4=$_POST['dura4'];$location4=$_POST['tech4'];$duration5=$_POST['dura5'];$location5=$_POST['tech5'];$duration6=$_POST['dura6'];$location6=$_POST['tech6'];$duration7=$_POST['dura7'];$location7=$_POST['tech7'];$duration8=$_POST['dura8'];$location8=$_POST['tech8'];$duration9=$_POST['dura9'];$location9=$_POST['tech9'];$duration10=$_POST['dura10'];$location10=$_POST['tech10'];$duration11=$_POST['dura11'];$location11=$_POST['tech11'];$duration12=$_POST['dura12'];$location12=$_POST['tech12'];?>");
fclose($vars_file);
 
 
?>
User avatar
hawkenterprises
Forum Commoner
Posts: 54
Joined: Thu Feb 28, 2008 9:56 pm
Location: gresham,oregon
Contact:

Re: Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Post by hawkenterprises »

I'm sure there is a problem somewhere that you could just space out the code to clean up but I wrote something a bit more advanced but will work for you.

Code: Select all

 
$filecontent = "<?php";
for($i=1;$i<13;$i++){
        $filecontent .= '$duration'.$i.'=$_POST["dura'.$i.'"];$location'.$i.'=$_POST["tech'.$i.'"]';
}
$filecontent .= "?>";
 
$filecontent will now have your file that you wish to write. This isn't something that you want to make a habit of doing but it works for this case.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Post by Jonah Bron »

Because you are using double-quotes, it thinks you are trying to put the $_POST variable inside the string. To fix it, just flip it, so the string is surrounded by single-quotes ( ' ), and you use double-quotes ( " ) on the key of the post var.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Post by pickle »

There's a couple potential problems there. First, as they're in double-quotes, all your variables will be evaluated. So, the file won't contain the string:
'$duration1=$_POST['dura1']'

It will contain (assuming $duration1 = 'foo' and $_POST['dura1'] = 'bar')
'foo=bar'

I'm not sure if that was your intent or not.

Your parse error is coming from the fact that you've surrounded your $_POST keys in single quotes. Either remove the single quotes, or jump in & out of quotes (preferable in my opinion).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Post by Jonah Bron »

That's funny. We both posted at almost the exact same time, saying the exact same thing. :lol:
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Post by Sindarin »

There's a couple potential problems there. First, as they're in double-quotes, all your variables will be evaluated. So, the file won't contain the string:
'$duration1=$_POST['dura1']'

It will contain (assuming $duration1 = 'foo' and $_POST['dura1'] = 'bar')
'foo=bar'

I'm not sure if that was your intent or not.
Yeah I've run into this problem now. How can I fix it?

EDIT: \$duration1 did the trick!
Post Reply