Unholy Prayer Posted February 22, 2007 Share Posted February 22, 2007 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."; } ?> Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 22, 2007 Share Posted February 22, 2007 Did you test this code yet? Did you test it on an online server when you tested? If so did you get any errors? If so did you have the permissions for config.php set correctly? Quote Link to comment Share on other sites More sharing options...
Unholy Prayer Posted February 23, 2007 Author Share Posted February 23, 2007 Hmm... forgot the permissions but then after i changed them, all it wrote to the file was this: = ''; = ''; = ''; = ''; Quote Link to comment Share on other sites More sharing options...
Grant132 Posted February 23, 2007 Share Posted February 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
Unholy Prayer Posted February 24, 2007 Author Share Posted February 24, 2007 It kinda worked but I didn't save the form as an HTML file. I just changed the GET function to POST function but it didn't include the strings, it just wrote the data from the inputs. Quote Link to comment Share on other sites More sharing options...
Grant132 Posted February 25, 2007 Share Posted February 25, 2007 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."; ?> Quote Link to comment Share on other sites More sharing options...
Unholy Prayer Posted February 25, 2007 Author Share Posted February 25, 2007 Will those strings work in a database configuration file even though they don't have the $ in front of it? Or is there a way to write to certain lines of the page you're writing the content to? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 25, 2007 Share Posted February 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
Unholy Prayer Posted February 25, 2007 Author Share Posted February 25, 2007 It's still not doing what I want it to. It isn't adding the semicolon at the end of the lines which is causing the configuration file to not work correctly. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 25, 2007 Share Posted February 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
Unholy Prayer Posted February 25, 2007 Author Share Posted February 25, 2007 Ah, now it's working. Thank you very much for your help. Quote Link to comment 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.