illuz1on Posted April 18, 2007 Share Posted April 18, 2007 Ok well ive tried posting and tried what I got told to do, but it doesnt seem to be working for me... tried for ages.. I had an add.php which adds a user to database. I wanted to make it add the user, and create a file $username.php, with the contents of $supplier1 .... This is my attempt will someone PLEASE tell me where I am wrong <?php include ("dbConfig.php"); if ( $_GET["op"] == "reg" ) { $bInputFlag = false; foreach ( $_POST as $field ) { if ($field == "") { $bInputFlag = false; } else { $bInputFlag = true; } } if ($bInputFlag == false) { die( "Problem with processing of information. " ."Please go back and try again"); } $supplierinfo = mysql_query("select * from dbSuppliers WHERE username=\"$username\""); while($r1=mysql_fetch_array($supplierinfo)){ extract($r1); $username = "$_POST[\"username\"]"; $filename= "$username.php" $supplier1 = "$_POST[\"supplier1\"]"; if (is_writable($filename)) { if (!$handle = fopen($username, 'a')) { echo "Cannot open file ($filename)"; exit; } if (fwrite($handle, $supplier1) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote the content to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } $q = "INSERT INTO `dbSuppliers` (`supplier1`,`username`,`password`,`email`) " ."VALUES ('".$_POST["supplier1"]."', '".$_POST["username"]."', " ."PASSWORD('".$_POST["password"]."'), " ."'".$_POST["email"]."')"; $r = mysql_query($q); if ( !mysql_insert_id() ) { die("Error: User not added to database..."); } else { Header("Location: add1.php?op=success"); } } elseif ( $_GET["op"] == "success" ) { echo "<h2>The supplier has been added successfully...</h2>"; } else { echo "<form action=\"?op=reg\" method=\"POST\">\n"; echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n"; echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n"; echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n"; echo "Supplier Info: <input name=\"supplier1\" MAXLENGTH=\"25\"><br />\n"; echo "<input type=\"submit\">\n"; echo "</form>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
Orio Posted April 18, 2007 Share Posted April 18, 2007 If the file does not exist, then is_writable() returns false... If you just want to create it, use fopen(). Then you can write to it using fwrite() or file_put_contents(). Orio. Quote Link to comment Share on other sites More sharing options...
illuz1on Posted April 18, 2007 Author Share Posted April 18, 2007 Ok cool i used those now, and it looks more successful than it did, gets to the form. I also moved the making of the file to the op=success place... Now im getting this error, and there is no file with the username.php Warning: Wrong parameter count for fclose() in /home/capetown/public_html/dev/test.php on line 54 The supplier has been added successfully... User section and content added... <?php include ("dbConfig.php"); if ( $_GET["op"] == "reg" ) { $bInputFlag = false; foreach ( $_POST as $field ) { if ($field == "") { $bInputFlag = false; } else { $bInputFlag = true; } } if ($bInputFlag == false) { die( "Problem with processing of information. " ."Please go back and try again"); } $supplierinfo = mysql_query("select * from dbSuppliers WHERE username=\"$username\""); while($r1=mysql_fetch_array($supplierinfo)){ extract($r1); $username = "$_POST[\"username\"]"; $filename= "$username.php" $supplier1 = "$_POST[\"supplier1\"]"; if (is_writable($filename)) { if (!$handle = fopen($username, 'a')) { echo "Cannot open file ($filename)"; exit; } if (fwrite($handle, $supplier1) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote the content to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } $q = "INSERT INTO `dbSuppliers` (`supplier1`,`username`,`password`,`email`) " ."VALUES ('".$_POST["supplier1"]."', '".$_POST["username"]."', " ."PASSWORD('".$_POST["password"]."'), " ."'".$_POST["email"]."')"; $r = mysql_query($q); if ( !mysql_insert_id() ) { die("Error: User not added to database..."); } else { Header("Location: add1.php?op=success"); } } elseif ( $_GET["op"] == "success" ) { echo "<h2>The supplier has been added successfully...</h2>"; } else { echo "<form action=\"?op=reg\" method=\"POST\">\n"; echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n"; echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n"; echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n"; echo "Supplier Info: <input name=\"supplier1\" MAXLENGTH=\"25\"><br />\n"; echo "<input type=\"submit\">\n"; echo "</form>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
rcorlew Posted April 18, 2007 Share Posted April 18, 2007 use 'a+' here (!$handle = fopen($username, 'a')) { The + will create the file if it does not exist and put the cursor at the end of the file. Quote Link to comment Share on other sites More sharing options...
illuz1on Posted April 18, 2007 Author Share Posted April 18, 2007 k cool, done that and still getting an error and no $username.php file .... Warning: fclose(): supplied argument is not a valid stream resource in /home/capetown/public_html/dev/test.php on line 54 The supplier has been added successfully... User section and content added... Quote Link to comment Share on other sites More sharing options...
illuz1on Posted April 18, 2007 Author Share Posted April 18, 2007 ok well tried to print("username1 - $supplier1"); and it didnt show anything these right? $username1 = "$_POST['username']"; $supplier1 = "$_POST['supplier1']"; Quote Link to comment Share on other sites More sharing options...
rcorlew Posted April 18, 2007 Share Posted April 18, 2007 Try this part of code, it looks like you are not even writing or opeing the file, only checking to see if it is false to get errors. $filename = "$username.php"; $handle = fopen($filename, 'a+') Looked like you were not using a file, rather using a username only to open. Quote Link to comment Share on other sites More sharing options...
illuz1on Posted April 18, 2007 Author Share Posted April 18, 2007 what would that go in place of ? Quote Link to comment Share on other sites More sharing options...
bsprogs Posted April 18, 2007 Share Posted April 18, 2007 ok well tried to print("username1 - $supplier1"); and it didnt show anything these right? $username1 = "$_POST['username']"; $supplier1 = "$_POST['supplier1']"; Try taking off the double quotes around $_POST[]; on both of these. Quote Link to comment Share on other sites More sharing options...
rcorlew Posted April 18, 2007 Share Posted April 18, 2007 It would go right here, I changed the code for you somewhat: $username = "$_POST[\"username\"]"; $filename= "$username.php" $supplier1 = "$_POST[\"supplier1\"]"; if($_POST["submit"]) { $handle = fopen($username, 'a+'); fwite($handle,$supplier1); fclose($handle); } //I think you should get the idea from that. if (is_writable($filename)) { if (!$handle = fopen($username, 'a')) { echo "Cannot open file ($filename)"; exit; } if (is_writable($filename)) { if (!$handle = fopen($username, 'a')) { echo "Cannot open file ($filename)"; exit; } Quote Link to comment Share on other sites More sharing options...
illuz1on Posted April 18, 2007 Author Share Posted April 18, 2007 Thanks alot, got it figured out! Thanks hey Quote Link to comment Share on other sites More sharing options...
rcorlew Posted April 18, 2007 Share Posted April 18, 2007 Your welcome. 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.