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
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');
Edited by ginerjm
Link to comment
Share on other sites

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 by OmarOHashim
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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