OmarOHashim Posted August 25, 2014 Share Posted August 25, 2014 I am a newbie PHP Web Developer and currently reading the book 'PHP and MYSQL Web Development' by Luke Welling and Laura Thomson. Following what was written in the book, that is what I got: <?php $tireqty = $_POST['tireqty']; $oilqty = $_POST['oilqty']; $sparkqty = $_POST['sparkqty']; $address = $_POST['address']; $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; $date = date('H:i, jS F Y'); ?> <html> <head> <title>Order Results</title> </head> <body> <h1>Bob's Auto Parts</h1> <h2>Order Results</h2> <?php if($tireqty == 0 && $oilqty == 0 && $sparkqty == 0){ echo '<p>You did not order anything on the previous page!</p>'; exit; }else{ echo '<p>Order Processed at '.$date.'</p>'; $totalqty = 0; $totalqty = $tireqty + $oilqty + $sparkqty; echo 'Items ordered: '.$totalqty.'<br />'; $totalamount = 0.00; define('TIREPRICE',100); define('OILPRICE',10); define('SPARKPRICE',4); $totalamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE; echo 'Subtotal: $'.number_format($totalamount,2).'<br />'; $taxrate = 0.10; $totalamount = $totalamount * (1 + $taxrate); echo '<p>'.'Total including tax: $'.number_format($totalamount,2).'<br /></p>'; echo '<p>Adress to ship to is '.$address.'<br />'; $outputString = $date."\t".$tireqty." tires \t".$oilqty." oil \t".$sparkqty." spark plugs \t".$totalamount."\t".$address."\n"; //opening files. @ $fp = fopen("$DOCUMENT_ROOT/booktutorials/orders.txt", 'ab'); /////////////////////////////////HERE IS WHERE IT WOULD NOT WORK AS I WANT TO //////////////////////////////////////////////////// if(!$fp){ echo "<p><strong> Your order could not be processed at this time. Please try again later.</strong></p>"; exit; } flock($fp, LOCK_EX); fwrite($fp, $outputString, strlen($outputString)); flock($fp, LOCK_UN); fclose($fp); echo "<p>Order written.</p>"; echo '<hr />'. file_get_contents("$DOCUMENT_ROOT/booktutorials/orders.txt"); } ?> </body> </html> All I get is 'Your order could not be processed at this time. Please try again later'. Even though I am pretty sure that the URL of the file is correct. Please help. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/290645-php-code-not-working/ Share on other sites More sharing options...
ginerjm Posted August 25, 2014 Share Posted August 25, 2014 (edited) Uhhh..... I loaded this up in my ide and cleaned up the line breaks and such and the line with that message is commented out. Don't know how you could get that message with the code as it stands. So - assuming that the mangled linefeeds and carrigage returns in the cut and paste I did of your code, I removed the comments. 1 - Do Not Use the @ character to suppress an error. If there is an error You Should Fix It, not ignore it! 2 - In order to even get to that message you had to have been shown the messages (echos) prior to that. Did you in fact get them? You said all you get is that message but I don't see how you could get to the message you state without getting the others. 3 - Obviously the file open did not work. Check your path and name. AND - as mentioned in my signature - turn on error checking which will confirm the failure of the fopen. error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); Edited August 25, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/290645-php-code-not-working/#findComment-1488905 Share on other sites More sharing options...
OmarOHashim Posted August 25, 2014 Author Share Posted August 25, 2014 (edited) Thanks for the reply. I did in fact get the rest but the code stops at Your order could not be processed at this time. Please try again later. I removed The '@' and added what you have in your signature, now I get this error: Warning: fopen(/Applications/XAMPP/xamppfiles/htdocs/booktutorials/orders.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/booktutorials/proccessorder.php on line 40 Line 40: $fp = fopen("$DOCUMENT_ROOT/booktutorials/orders.txt", 'ab'); Edited August 25, 2014 by OmarOHashim Quote Link to comment https://forums.phpfreaks.com/topic/290645-php-code-not-working/#findComment-1488907 Share on other sites More sharing options...
ginerjm Posted August 25, 2014 Share Posted August 25, 2014 As I already told you. Actually it is a permission issue. Do you have permitted access to that folder and/or file? Quote Link to comment https://forums.phpfreaks.com/topic/290645-php-code-not-working/#findComment-1488908 Share on other sites More sharing options...
OmarOHashim Posted August 25, 2014 Author Share Posted August 25, 2014 Hmm.. It tells me 'You can read and write' Is that it? Or is it something else? Quote Link to comment https://forums.phpfreaks.com/topic/290645-php-code-not-working/#findComment-1488909 Share on other sites More sharing options...
ginerjm Posted August 25, 2014 Share Posted August 25, 2014 (edited) What is 'it'? Actually when I ran your code, my error message simply said it couldn't access the file - no mention of permissions. So - PHP can see the file. You (or your php session) cannot. Edited August 25, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/290645-php-code-not-working/#findComment-1488910 Share on other sites More sharing options...
OmarOHashim Posted August 25, 2014 Author Share Posted August 25, 2014 Oh okay I'll try to check the permissions thanks a lot for the help! Quote Link to comment https://forums.phpfreaks.com/topic/290645-php-code-not-working/#findComment-1488911 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.