Page 1 of 1

Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Posted: Fri Feb 29, 2008 8:46 am
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);
 
 
?>

Re: Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Posted: Fri Feb 29, 2008 10:03 am
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.

Re: Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Posted: Fri Feb 29, 2008 10:35 am
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.

Re: Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Posted: Fri Feb 29, 2008 10:48 am
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).

Re: Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Posted: Fri Feb 29, 2008 11:10 am
by Jonah Bron
That's funny. We both posted at almost the exact same time, saying the exact same thing. :lol:

Re: Can't update a document: T_ENCAPSED_AND_WHITESPACE error

Posted: Mon Mar 17, 2008 5:13 am
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!