Jump to content

PHP Code Not Working


OmarOHashim

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/290645-php-code-not-working/
Share on other sites

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');

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');

Archived

This topic is now archived and is closed to further replies.

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