views:

38

answers:

4

Hi all, I'm trying to create an installer, using a combination of fwrite and forms. Here's my code:

<?php
$myFile = "db_config.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, "$host = " . $_POST['host'] . ";\n $username = " . $_POST['username'] . ";\n     $password = " . $_POST['password'] . ";\n $name = " . $_POST['name']";" ;);
fwrite($fh, 'mysql_connect("{$host}", "{$db_username}", "{$db_password}")\n or     die(mysql_error());\n mysql_select_db("{$db_name}")\n or die(mysql_error()); ?>' ;);
fclose($fh); 
?>

Here's the error:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home2/runetyco/public_html/ballpointradio/new/install_action.php on line 4
A: 

problem might be in ?>. you'll have to escape it. also, you are not putting <?php in the beginning of your conf file.

why do you want it to be php file anyway. why not use ini file and parse_ini_file(), or xml maybe?

kgb
How does this look?.... <?php$myFile = "db_config.php";$fh = fopen($myFile, 'w') or die("can't open file");fwrite($fh, "<?php $host = " . $_POST['host'] . ";\n $username = " . $_POST['username'] . ";\n $password = " . $_POST['password'] . ";\n $name = " . $_POST['name'] . ";" );fwrite($fh, 'mysql_connect("{$host}", "{$db_username}", "{$db_password}")\n or die(mysql_error());\n mysql_select_db("{$db_name}")\n or die(mysql_error()); ?' );fclose($fh); ?>
Sam
+2  A: 

You are missing a .at the end of the line

[...]. $_POST['name'] . [<--] ";" ;)
DrColossos
Thanks, this fixed the error!
Sam
fixed one of many errors..
nathan
A: 

The reason it's giving an error is because it's looking for the variable $host on line 4 - if you change the double quotes to single it should fix the problem - see below:

<?php
$myFile = "db_config.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, '$host = ' . $_POST['host'] . ';\n $username = ' . $_POST['username'] . ';\n $password = ' . $_POST['password'] . ';\n $name = ' . $_POST['name'] . ';\n\n';
fwrite($fh, 'mysql_connect("{$host}", "{$db_username}", "{$db_password}")\n or die(mysql_error());\n mysql_select_db("{$db_name}")\n or die(mysql_error()); ?>' ;);
fclose($fh); 
?>
xil3
Ah, beat me to it. You are however missing the closing `)` on line 4 ;)
FreekOne
if you take a look at what that will output, it'll have solid '\n' in the code, none of the variables will have quotes around them and a few other bits ;)
nathan
No, this wasn't the error, but I'm curious as to how this could work, because it would only grab '$host = host etc.'...
Sam
+2  A: 

quite a few problems tbh.. sorted

<?php
$myFile = "db_config.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, '<?php' . PHP_EOL . '$host = "' . $_POST['host'] . '";' . PHP_EOL . '$username = "' . $_POST['username'] . '";' . PHP_EOL . '$password = "' . $_POST['password'] . '";' . PHP_EOL . '$name = "' . $_POST['name'] . '";' . PHP_EOL );
fwrite($fh, 'mysql_connect($host, $db_username, $db_password)' . PHP_EOL . 'or die(mysql_error());' . PHP_EOL . 'mysql_select_db($db_name) or die(mysql_error());' . PHP_EOL);
fclose($fh);

note: i wouldn't recommend doing it this way, typically much better to only write out a simple config file and keep all code static, but the above answers your question about the error you get.

nathan