Jump to content


Photo

Php Need Help

php write help

  • Please log in to reply
5 replies to this topic

#1 TRaWolTa89

TRaWolTa89

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 24 December 2012 - 07:32 AM

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.

#2 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 24 December 2012 - 09:30 AM

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., 24 December 2012 - 09:31 AM.

Keeping it simple.

#3 TRaWolTa89

TRaWolTa89

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 24 December 2012 - 12:33 PM

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) {


#4 Pikachu2000

Pikachu2000

    I hate everything.

  • Staff Alumni
  • 11,378 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 24 December 2012 - 12:46 PM

$file_put_contents <--- Do you see why that's being interpreted as a variable?
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

#5 TRaWolTa89

TRaWolTa89

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 24 December 2012 - 01:01 PM

Silly me :happy-04: !! It works now. Thanks for all your help

#6 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 24 December 2012 - 01:32 PM

Ah, sorry about those errors, was in a bit of a hurry.
Glad you got it sorted out though. :)
Keeping it simple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com