1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <form action="processorder.php" method="post"> 9 <table border="1"> 10 <tr> 11 <td>Item</td> 12 <td>Qty</td> 13 </tr> 14 <tr> 15 <td>Tires</td> 16 <td><input type="text" name="tireqty"></td> 17 </tr> 18 <tr> 19 <td>oilqty</td> 20 <td><input type="text" name="oilqty"></td> 21 </tr> 22 <tr> 23 <td>sparkqty</td> 24 <td><input type="text" name="sparkqty"></td> 25 </tr> 26 <tr> 27 <td>Shipping Address</td> 28 <td><input type="text" name="address"></td> 29 </tr> 30 <tr> 31 <td><input type="submit" value="提交"></td> 32 </tr> 33 </table> 34 35 </form> 36 </body> 37 </html>
下面是PHP代码
1 <?php 2 3 $tireqty = $_POST['tireqty']; 4 $oilqty = $_POST['oilqty']; 5 $sparkqty = $_POST['sparkqty']; 6 $address = $_POST['address']; 7 8 $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; 9 $date = date('H:i,jS F Y'); 10 11 ?> 12 <!--以下是HTML--> 13 <!DOCTYPE html> 14 <html> 15 <head lang="en"> 16 <meta charset="UTF-8"> 17 <title>Bob's Auto Pars - Order Results</title> 18 </head> 19 <body> 20 <h1>Bob's Auto Parts</h1> 21 22 <h2>Order Results</h2> 23 24 <?php 25 26 echo '<p>Order processed at' . date('H:i,jS F Y') . '</p>'; 27 echo '<p>Your order is as follows:</p>'; 28 29 $totalqty = 0; /*购物总数量初始化*/ 30 $totalqty = $tireqty + $oilqty + $sparkqty; 31 echo 'Items ordered:' . $totalqty . '<br/>'; 32 33 if ($totalqty == 0) { 34 echo "You did not order anything on the previous page!<br/>"; 35 } else { 36 if ($tireqty > 0) { 37 echo $tireqty . "tires<br/>"; 38 } 39 if ($oilqty > 0) { 40 echo $oilqty . "oilqtys<br/>"; 41 } 42 if ($sparkqty > 0) { 43 echo $sparkqty . "sparkqtys<br/>"; 44 } 45 } 46 /*以下计算购买物品金额代码*/ 47 //物品单价 开始 48 $totalamount = 0.00; 49 define('TIREPRICE', 100); 50 define('OILPRICE', 100); 51 define('SPARKPRICE', 100); 52 //物品单价 结束 53 54 //物品总金额 55 $totalamount = $tireqty * TIREPRICE 56 + $oilqty * OILPRICE 57 + $sparkqty * SPARKPRICE; 58 $totalmount = number_format($totalamount, 2, '.', ''); 59 60 echo "<p>Total of order is $" . $totalmount . "<p/>"; 61 echo 'Address to ship to is:' . $address . '<p/>'; 62 63 $outputstring = $date . "\t" . $tireqty . "tires\t" . $oilqty . "oil\t" 64 . $sparkqty . "sparkplugs\t\$" . $totalamount . "\t" . $address . "\n";; 65 66 //存储购物数据到本地 67 @$fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab'); 68 if (!$fp) { 69 echo "<p> 70 <strong>Your order could not be processed at this time.Please try again later.</strong> 71 </p>"; 72 exit; 73 } 74 flock($fp, LOCK_EX); 75 76 fwrite($fp, $outputstring, strlen($outputstring)); 77 flock($fp, LOCK_UN); 78 fclose($fp); 79 echo "<p>Order written.</p>"; 80 81 82 echo "</body> 83 </html>";
以下是输出信息
Bob's Auto Parts
Order Results
Order processed at00:16,12th July 2015
Your order is as follows:
Items ordered:6 1tires 2oilqtys 3sparkqtys
Total of order is $600.00
Address to ship to is:4
Warning: fopen(F:/stadyCode/PHP/test/../orders/orders.txt) [function.fopen]: failed to open stream: No such file or directory in F:\stadyCode\PHP\test\processorder.php on line 67
Your order could not be processed at this time.Please try again later.
你这是保存到服务器上了,
不懂 这是按着PHP和MySQL 开发 这本书的源码报的错
不知道是不是本身就有问题
不怎么懂刚开始学