TRaWolTa89 Posted December 24, 2012 Share Posted December 24, 2012 Hello I'm having a problem with my php script. It's about writing lines to a text file. The script runs normally without any errors, but it leaves the text file empty, no matter how many times I run it. I'm using WAMP server. Here's the code: <?php //kratke varijable $tireqty=$_POST['tireqty']; $oilqty=$_POST['oilqty']; $sparkqty=$_POST['sparkqty']; $adress=$_POST['adress']; $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; $date = date('H:i, jS F Y'); ?> <html> <head> <meta charset="utf-8"> <title>Bob's Auto parts - Order results</title> </head> <body> <h1>Bob's Auto Parts</h1> <h2> Order Results</h2> <?php echo "<p>Order processed at ".date ('H:i, jS F Y')."</p>"; echo "<p>Your order is as follows: </p>"; $totalqty=0; $totalqty=$tireqty+$sparkqty+$oilqty; echo "Item ordered: ".$totalqty."<br />"; if ($totalqty == 0) { echo "You didn't order anyting from previous page!<br />"; } else { if($tireqty>0) echo $tireqty." tires<br />"; if($oilqty>0) echo $oilqty." oil<br />"; if($sparkqty>0) echo $sparkqty." sparks<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.20; // local sales tax is 20% $totalamount = $totalamount * (1 + $taxrate); echo 'Total including tax: $'.number_format($totalamount,2).'<br />'; echo '<p>Adress to ship to is '.$adress.'</p>'; $outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t" .$sparkqty." spark plugs\t\$".$totalamount ."\t". $adress."\n"; //open file for appending @ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab'); if(!$fp) { echo "<p><strong> Your order could not be processed at this time.<br> Please try again later. </strong></p></body></html>"; exit; } flock($fp, LOCK_EX); echo fwrite($fp, $outputstring, strlen($outputstring)); flock($fp, LOCK_UN); fclose($fp); echo "<p>Order written.</p>"; ?> </body> </html> Thanks in advance for any help. Quote Link to comment https://forums.phpfreaks.com/topic/272331-php-need-help/ Share on other sites More sharing options...
Christian F. Posted December 24, 2012 Share Posted December 24, 2012 (edited) Try to replace this part: $outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t" .$sparkqty." spark plugs\t\$".$totalamount ."\t". $adress."\n"; //open file for appending @ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab'); if(!$fp) { echo "<p><strong> Your order could not be processed at this time.<br> Please try again later. </strong></p></body></html>"; exit; } flock($fp, LOCK_EX); echo fwrite($fp, $outputstring, strlen($outputstring)); flock($fp, LOCK_UN); fclose($fp); echo "<p>Order written.</p>"; With this: $outputstring = "{$date}\t{$tireqty} tires\t{$oilqty} oil\t". "{$sparkqty} spark plugs\t\${$totalamount}\t{$adress}\n"; //File to write order to. $file = $DOCUMENT_ROOT."/../orders/orders.txt"; // Append order info to orders file, adding an exclusive lock while writing. if(!$file_put_contents ($file, $outputstring, FILE_APPEND|LOCK_EX) { echo "<p><strong> Your order could not be processed at this time.<br> Please try again later. </strong></p></body></html>"; exit; } echo "<p>Order written.</p>"; Also, make sure you have error reporting turned on, and that you're reporting all errors (to screen). PS: Please use the [code][/code] tags around your code, as it helps make both your post and your code a lot easier to read. Edited December 24, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/272331-php-need-help/#findComment-1401130 Share on other sites More sharing options...
TRaWolTa89 Posted December 24, 2012 Author Share Posted December 24, 2012 Sorry about the code tag. First time posting. I replaced the code with the one you gave me. You forgot to close ) in if statement and it gave me an error, but I fixed it. Now it gives me this error:" Notice: Undefined variable: file_put_contents in C...." This is the line: if(!$file_put_contents ($file, $outputstring, FILE_APPEND|LOCK_EX) { Quote Link to comment https://forums.phpfreaks.com/topic/272331-php-need-help/#findComment-1401155 Share on other sites More sharing options...
Pikachu2000 Posted December 24, 2012 Share Posted December 24, 2012 $file_put_contents <--- Do you see why that's being interpreted as a variable? Quote Link to comment https://forums.phpfreaks.com/topic/272331-php-need-help/#findComment-1401157 Share on other sites More sharing options...
TRaWolTa89 Posted December 24, 2012 Author Share Posted December 24, 2012 Silly me !! It works now. Thanks for all your help Quote Link to comment https://forums.phpfreaks.com/topic/272331-php-need-help/#findComment-1401158 Share on other sites More sharing options...
Christian F. Posted December 24, 2012 Share Posted December 24, 2012 Ah, sorry about those errors, was in a bit of a hurry. Glad you got it sorted out though. Quote Link to comment https://forums.phpfreaks.com/topic/272331-php-need-help/#findComment-1401160 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.