Jump to content

[SOLVED] Adding to database and writing to file?!


illuz1on

Recommended Posts

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";
}
?>

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

   }

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.