pcw Posted March 12, 2011 Share Posted March 12, 2011 Hi, I have the following code which allows a user to enter a name for a mysql column field. Once they have submitted the form they are then redirected back to the form which allows them to enter the name for another mysql column form. When they are returned to the form, I would like the new column field added to the file named userinfo.php The problem is, I need the column field variable entered into the userinfo.php in the following format $newcolumn = $info['newcolumn'] but when I put it like this $stringData = "$newcolumn = $info['newcolumn']"; I get this error Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/tropicsb/public_html/MemberSiteMaker/admin.php on line 398 // Profile and Application details case "profileFields": session_start(); if(isset($_SESSION['username'])) { $newcolumn = $_GET['newcolumn']; $file = "lib/userinfo.php"; $fh = fopen($file, 'a') or die ("Cant open file"); $stringData = "$newcolumn = $info['newcolumn']"; fwrite($fh, $stringData); fclose($fh); echo "This is the logged in page\n<br>"; ECHO <<<PAGE <form action=admin.php?cmd=addfield&username=admin method=POST> <input type=text name=newcolumn> <input type=submit name=submit> PAGE; } else { echo "This is a failed login"; } break; case "addfield": include_once("data/mysql.php"); $newcolumn = $_POST['newcolumn']; $mysqlPassword = (base64_decode($mysqlpword)); $con = mysql_connect("$localhost", "$mysqlusername", "$mysqlPassword") or die(mysql_error()); mysql_select_db("$dbname", $con) or die(mysql_error()); $result=mysql_query("ALTER TABLE members ADD $newcolumn VARCHAR(30) AFTER approved") or die("Alter Error: ".mysql_error()); mysql_close($con); echo "Database has been modified successfully."; echo '<meta http-equiv="REFRESH" content="5;url=admin.php?cmd=profileFields&username=admin&$newcolumn">'; break; thanks Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/ Share on other sites More sharing options...
flolam Posted March 12, 2011 Share Posted March 12, 2011 if you use variables in double-quoted strings, the PHP interpreter replaces them with their values. Thus, you will have to use single-quotes: $stringData = '$newcolumn = $info[\'newcolumn\']'; Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/#findComment-1186756 Share on other sites More sharing options...
pcw Posted March 13, 2011 Author Share Posted March 13, 2011 Hi thanks for your reply, I changed the code to what you said, but now all what is written to the file is $newcolumn = $info['newcolumn'] it does not carry the value given to the $newcolumn variable from $newcolumn = $_POST['newcolumn']; however if I just put $stringData = '$newcolumn'; it will write the value to the file such as $name I am not sure how stringData can actually write the following string to file. I am using name as the value given to $newcolumn $stringData = '$name = $info[\'name\']'; As always, any help is much appreciated Paul Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/#findComment-1187025 Share on other sites More sharing options...
kenrbnsn Posted March 13, 2011 Share Posted March 13, 2011 Try this: <?php $stringData = "\$newcolumn = {$info['newcolumn']}"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/#findComment-1187026 Share on other sites More sharing options...
pcw Posted March 13, 2011 Author Share Posted March 13, 2011 Hi thanks for your reply, I changed the code and only the following is written to the file $newcolumn = Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/#findComment-1187027 Share on other sites More sharing options...
pcw Posted March 13, 2011 Author Share Posted March 13, 2011 if i change $stringData = "\$newcolumn = {$info['newcolumn']}"; to $stringData = "$newcolumn = {$info['newcolumn']}"; then the variable value is written to the file such as name is what i am trying to achieve possible? Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/#findComment-1187030 Share on other sites More sharing options...
kenrbnsn Posted March 13, 2011 Share Posted March 13, 2011 Try <?php $stringData = $newcolumn . " = \$info['newcolumn']"; ?> If that doesn't work, please tell us exactly what needs to be written to the file. Ken Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/#findComment-1187037 Share on other sites More sharing options...
pcw Posted March 13, 2011 Author Share Posted March 13, 2011 thanks for your reply, but still no luck. The script adds a new column to the database using the $newcolumn variable it then adds the variable value such as $name to the file, however it has to be written to the file like this. $newcolumn = $info['newcolumn']; where newcolumn will be the variable value so: $name = $info['name']; the $info['name']; is required in the file as it is used to retreive information from the mysql database Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/#findComment-1187048 Share on other sites More sharing options...
pcw Posted March 13, 2011 Author Share Posted March 13, 2011 Ideally something like this should work $stringData = '$' . $newcolumn . '= $info[' . $newcolumn ']'; but i get this error Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/tropicsb/public_html/MemberSiteMaker/admin.php on line 429 any ideas on how to fix this? It would really help as it should write what I need to file then Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/#findComment-1187077 Share on other sites More sharing options...
kenrbnsn Posted March 13, 2011 Share Posted March 13, 2011 You forgot a concatenation operator <?php $stringData = '$' . $newcolumn . '= $info[' . $newcolumn . ']'; ?> Or, try it this way <?php $stringData = "\${$newcolumn} = \$info['{$newcolumn}']"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/#findComment-1187083 Share on other sites More sharing options...
pcw Posted March 13, 2011 Author Share Posted March 13, 2011 kenrbnsn thank you so much, your second example worked better, absolutely fantastic, thanks very much for your help. CLOSED Quote Link to comment https://forums.phpfreaks.com/topic/230457-trouble-appending-file/#findComment-1187090 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.