943,919 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 686
  • PHP RSS
Feb 16th, 2008
0

script running without errors, but not doing tasks

Expand Post »
Hi,

I'm very new to PHP. Any help is appreciated.

I wrote a script that should create a flat file and store information in the flat file that was entered in a form on an earlier page. The script runs successfully and with no errors, and creates the file with the correct name (so my variable values are being passed ok by $_POST), but when I look at the file, nothing was written to it. It was only created and named.

Here's the script...

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $accountname = $_POST['accountname'];
  4. $phone = $_POST['phone'];
  5. $fax = $_POST['fax'];
  6. $cell = $_POST['cell'];
  7. $add1 = $_POST['add1'];
  8. $add2 = $_POST['add2'];
  9. $city = $_POST['city'];
  10. $state = $_POST['state'];
  11. $zip = $_POST['zip'];
  12. $data = "$accountname \n $phone \n $fax \n $cell \n $add1 \n $add2 \n $city \n $state \n $zip \n";
  13. $ending = ".txt";
  14. $filename = "$accountname$ending";
  15. $handle = fopen($filename, 'x+');
  16.  
  17. if (file_exists('$filename')){
  18. echo "The accountname $accountname already exists.";
  19. }else{
  20. $handle;
  21. fwrite($filename, $data);
  22. fclose($filename);
  23. echo "Data Stored Successfully";
  24. }
  25. ?>

also, if an existing file name is used, it is not stopping the script to echo "The accountname $accountname already exists."

Thanks in advance for any help. I'd look it up on my own, but I'm not getting any errors to google and I can't find a good tutorial for what I'm trying to do.

- John
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnr is offline Offline
2 posts
since Feb 2008
Feb 16th, 2008
0

Re: script running without errors, but not doing tasks

try these changes...I put some comments where I thought they might help you understand what I changed...

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $accountname = $_POST['accountname'];
  4. $phone = $_POST['phone'];
  5. $fax = $_POST['fax'];
  6. $cell = $_POST['cell'];
  7. $add1 = $_POST['add1'];
  8. $add2 = $_POST['add2'];
  9. $city = $_POST['city'];
  10. $state = $_POST['state'];
  11. $zip = $_POST['zip'];
  12. $data = "$accountname \n $phone \n $fax \n $cell \n $add1 \n $add2 \n $city \n $state \n $zip \n";
  13. $ending = ".txt";
  14. $filename = "$accountname$ending";
  15.  
  16. //strings are only interpolated in double quotes! do not use single quotes if placing variables in strings.
  17. //'$filename' = $filename
  18. //"$filename" = the contents of $filename
  19.  
  20. if (file_exists("$filename"))//check for file in this directory
  21. {
  22. echo "The accountname $accountname already exists.";
  23. }
  24. else
  25. {
  26. //open file handle in this directory
  27. $handle = fopen($filename, 'x+');
  28.  
  29. //write to handle
  30. fwrite($handle, $data);
  31.  
  32. //close file handle
  33. fclose($handle);
  34.  
  35. //the next line will echo regardless if what it says is true...
  36. echo "Data Stored Successfully";
  37. }
  38. ?>
Reputation Points: 14
Solved Threads: 14
Junior Poster in Training
johnsquibb is offline Offline
84 posts
since Nov 2007
Feb 16th, 2008
0

Re: script running without errors, but not doing tasks

hali - frickkin - louia!!!

It works!

Thank you!

Even better, the comments you made make sense to me now.

You are awesome!

Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnr is offline Offline
2 posts
since Feb 2008
Feb 16th, 2008
0

Re: script running without errors, but not doing tasks

glad to be of assistance!
Reputation Points: 14
Solved Threads: 14
Junior Poster in Training
johnsquibb is offline Offline
84 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: New php user need help
Next Thread in PHP Forum Timeline: How to stop spam?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC