Jump to content

Help With fwrite Fucntion


Unholy Prayer

Recommended Posts

I'm trying to create an install file for my program.  I have a form where the user inputs the database information and strings to represent the GET function for each of the inputs.  How would I be able to use the fwrite(); function to write different strings to a file?  Here's my coding to show what I mean.  Any help would be greatly appreciated.

 

<?php

echo "<table align='center' cellspacing='1' cellpadding='1' border='0'>
        <tr>
       <td align='center' colspan='2'>MySQL Info</td>
</tr><tr>
<form action='install.php' method='POST'>
<td align='right'>Database Name:</td>
<td align='left'><input type='text' name='db_name' size='30'></td>
</tr><tr>
<td align='right'>Username:</td>
<td align='left'><input type='text' name='username' size='30'></td>
</tr><tr>
<td align='right'>Database Password:</td>
<td align='left'><input type='text' name='password' size='30'></td>
</tr><tr>
<td align='right'>Host Name:<br><small>Usually 'local host'</small></td>
<td align='left'><input type='text' name='host' size='30'></td>
</tr><tr>
<td align='center' colspan='2'><input type='submit' name='Submit' value='Submit'></td>
</tr>
</form>
</table>";

if($_POST['Submit'])

{

  $dbname = $_GET['db_name'];
  $username = $_GET['username'];
  $password = $_GET['password'];
  $host = $_GET['host'];

  $file = fopen("config.php", "w");
  fwrite($file,"$database = '$dbname';
                $user = '$username';
                $pass = '$password';
                $dbhost = '$host';");
  fclose($file); 

  echo "The configuration file was successfully updated.  Click below to continue with the installation.";
}
?>

Link to comment
Share on other sites

Try this:

<html>
<head>
</head>
<body>
<table align='center' cellspacing='1' cellpadding='1' border='0'>
<tr>
<td align='center' colspan='2'>MySQL Info</td>
</tr><tr>
<form action='install.php' method='POST'>
<td align='right'>Database Name:</td>
<td align='left'><input type='text' name='db_name' size='30'></td>
</tr><tr>
<td align='right'>Username:</td>
<td align='left'><input type='text' name='username' size='30'></td>
</tr><tr>
<td align='right'>Database Password:</td>
<td align='left'><input type='text' name='password' size='30'></td>
</tr><tr>
<td align='right'>Host Name:<br><small>Usually 'local host'</small></td>
<td align='left'><input type='text' name='host' size='30'></td>
</tr><tr>
<td align='center' colspan='2'><input type='submit' name='Submit' value='Submit'></td>
</tr>
</form>
</table>
</body>
</html

 

Save that as a html file.

 

<?php
  $dbname = $_POST['db_name'];
  $username = $_POST['username'];
  $password = $_POST['password'];
  $host = $_POST['host'];

  $file = fopen("config.php", "w");
  fwrite($file,"$database = '$dbname';
                $user = '$username';
                $pass = '$password';
                $dbhost = '$host';");
  fclose($file); 

  echo "The configuration file was successfully updated.  Click below to continue with the installation.";

?>

 

Save that as "install.php" (your action name from the form).

 

Basically i've just split them up into two different files as you weren't getting the variables before in order to print them in the config.php file.

 

I haven't test that as im at school but if it doesn't work just post back

Link to comment
Share on other sites

Do you mean like its meant to show

database = $dbname;

user = $username;

pass = $password;

dbhost = $host;

 

like that with the data from the form where the variables are? If so just remove "$" from in front of the database, user, pass and dbhost.

 

<?php
  $dbname = $_POST['db_name'];
  $username = $_POST['username'];
  $password = $_POST['password'];
  $host = $_POST['host'];

  $file = fopen("config.php", "w");
  
  $stringdata = "database = $dbname;
                user = $username;
                pass = $password;
                dbhost = $host;";
  
  fwrite($file, $stringdata);
  
  fclose($file); 

  echo "The configuration file was successfully updated.  Click below to continue with the installation.";

?>

Link to comment
Share on other sites

What you probably want to do is something like this:

<?php
  $stringdata = array();
  $stringdata[] = '$database = ' . $dbname;
  $stringdata[] = '$user = ' . $username;
  $stringdata[] = '$pass = ' . $password;
  $stringdata[] = '$dbhost = ' . $host;
  
  fwrite($file, implode("\n",$stringdata)."\n");
?>

 

When you use single quotes, variable names are not evaluated, therefore, the literal strings are added to the string that gets written to the file.

 

Ken

Link to comment
Share on other sites

Then change the code to:

<?php
  $stringdata = array();
  $stringdata[] = '$database = "' . $dbname . '";';
  $stringdata[] = '$user = "' . $username . '";';
  $stringdata[] = '$pass = "' . $password . '";';
  $stringdata[] = '$dbhost = "' . $host . '";';
  
  fwrite($file, implode("\n",$stringdata)."\n");
?>

 

This iteration of the code also adds double quotes around the values in the file.

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.