创建安装程序脚本

发布于 2024-09-07 18:54:13 字数 704 浏览 1 评论 0原文

我正在尝试使用 fwrite 和表单的组合来创建一个安装程序。这是我的代码:

<?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); 
?>

这是错误:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪眸﹌ 2024-09-14 18:54:14

您在行尾缺少 .

[...]. $_POST['name'] . [<--] ";" ;)

You are missing a .at the end of the line

[...]. $_POST['name'] . [<--] ";" ;)
狠疯拽 2024-09-14 18:54:14

相当多的问题 tbh.. 排序

<?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);

注意:我不建议这样做,通常最好只写出一个简单的配置文件并保持所有代码静态,但上面回答了您关于所得到的错误的问题。

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.

无畏 2024-09-14 18:54:14

问题可能出在 ?> 中。你必须逃避它。另外,您没有将 放在 conf 文件的开头。

为什么你想要它是 php 文件呢?为什么不使用 ini 文件和 parse_ini_file() 或 xml或许?

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?

相守太难 2024-09-14 18:54:14

它给出错误的原因是因为它正在第 4 行查找变量 $host - 如果将双引号更改为单引号,它应该可以解决问题 - 见下文:

<?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); 
?>

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); 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文