script running without errors, but not doing tasks

Reply

Join Date: Feb 2008
Posts: 2
Reputation: johnr is an unknown quantity at this point 
Solved Threads: 0
johnr johnr is offline Offline
Newbie Poster

script running without errors, but not doing tasks

 
0
  #1
Feb 16th, 2008
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...

  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 84
Reputation: johnsquibb is an unknown quantity at this point 
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

Re: script running without errors, but not doing tasks

 
0
  #2
Feb 16th, 2008
try these changes...I put some comments where I thought they might help you understand what I changed...

  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. ?>
The End
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 2
Reputation: johnr is an unknown quantity at this point 
Solved Threads: 0
johnr johnr is offline Offline
Newbie Poster

Re: script running without errors, but not doing tasks

 
0
  #3
Feb 16th, 2008
hali - frickkin - louia!!!

It works!

Thank you!

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

You are awesome!

Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 84
Reputation: johnsquibb is an unknown quantity at this point 
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

Re: script running without errors, but not doing tasks

 
0
  #4
Feb 16th, 2008
glad to be of assistance!
The End
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC