Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/272331-php-need-help/
Share on other sites

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 by Christian F.
Link to comment
https://forums.phpfreaks.com/topic/272331-php-need-help/#findComment-1401130
Share on other sites

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. :happy-04: 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) { 

Link to comment
https://forums.phpfreaks.com/topic/272331-php-need-help/#findComment-1401155
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.