[英]php and mysql problems with updating two tables, and inserting into two tables with one button
這是我的結帳頁面的一些代碼。 我是php新手,這是我的第一學期,但我仍在努力。 此頁面收集添加到購物車的訂單,並完美顯示和匯總訂單。 這是我的問題。
有時會有OrderIn產品,可能不存在或不止其中之一,也可能存在OrderOut產品,或者不存在。 我知道很復雜。 我可能正在嘗試做太多事情。 當我按“付款此發票”按鈕時,我想收集(無論是多少還是哪種)訂單ID(出庫或入庫),並將“已支付的訂單ID”設置為“是”,然后將OrderId插入相應的發票invoice_in或invoice_out,並設置為否。
是否可以,將第一個產品OrderId_in更改為yes,現在我收到一個MySQL錯誤“ SQL語法有誤;請查看與您的MySQL服務器版本相對應的手冊以獲取正確的語法,在第2行的'75 .18','No')'附近使用。” 我可以在這里使用一些指示。
<div class="tablecheckOut">
<form action='checkout.php' method='post'>
<p><strong>Purchases this invoice: </strong><br><br>
<?php
echo "<table class='middlecheckOut'>
<tr>
<td class='td2'><b>Order ID: </b></td>
<td class='td2'><b>Product Name: </b></td>
<td class='td2'><b>Quantity: </b></td>
<td class='td2'><b>Price: </b></td>
</tr>";
if (isset($_GET['user_id'])) {
$user_id = $_GET['user_id'];
} elseif (isset($_POST['user_id'])) {
$user_id = $_POST['user_id'];
}
$display="SELECT *
FROM order_instate JOIN in_Product ON
order_instate.ip_id = in_product.ip_id
WHERE user_id = '$user_id'; " ;
$displayResult = @mysqli_query($dbhandle, $display)
or die(mysqli_error($dbhandle));
$priceIn = 0;
while($row = mysqli_fetch_array($displayResult, MYSQLI_ASSOC)) {
if($row['orderIn_paid'] == "No") {
echo "<tr>
<input type='hidden' name='ip_id' value='" . $row['ip_id'] . "' />
<td class='td2'>" . $row['orderIn_id'] . "   </td>
<td class='td2'>" . $row['ip_name'] . "   </td>
<td class='td2'>" . $row['orderIn_quantity'] . "   </td>
<td class='td2'>$" . $row['orderIn_total'] . "   </td>
</tr>";
$priceIn += $row['orderIn_total'];
$orderIn_id = $row['orderIn_id'];
$_SESSION['orderIn'] = $orderIn_id;
}
}
if (isset($_GET['user_id'])) {
$user_id = $_GET['user_id'];
} elseif (isset($_POST['user_id'])) {
$user_id = $_POST['user_id'];
}
$display2="SELECT *
FROM order_outstate JOIN op_Product ON
order_outstate.op_id = op_product.op_id
WHERE user_id = '$user_id'; " ;
$displayResult2 = @mysqli_query($dbhandle, $display2)
or die(mysqli_error($dbhandle));
$priceOut = 0;
while($row2 = mysqli_fetch_array($displayResult2, MYSQLI_ASSOC)) {
if($row2['orderOut_paid'] == "No") {
echo "<tr>
<input type='hidden' name='op_id' value='" . $row2['op_id'] . "' />
<td class='td2'>" . $row2['orderOut_id'] . "   </td>
<td class='td2'>" . $row2['op_name'] . "   </td>
<td class='td2'>" . $row2['orderOut_quantity'] . "   </td>
<td class='td2'>$" . $row2['orderOut_total'] . "   </td>
</tr>";
$priceOut += $row2['orderOut_total'];
$orderOut_id = $row['orderOut_id'];
$_SESSION['orderOut'] = $orderOut_id;
}
}
echo "</table>";
$subtotal = 0;
$tax = 0;
$gtotal = 0;
$subtotal = number_format($priceIn + $priceOut, 2);
$tax = number_format($subtotal * .074, 2);
$gtotal = number_format($subtotal + $tax, 2);
?>
</p>
<p><strong>Total Amount of Purchase(s): <?php echo "$" . " $subtotal " ?></strong></p>
<p><strong>Tax this invoice (7.4%): <?php echo "$" . " $tax " ?> </strong></p>
<p><strong>Grand Total of Invoice: <?php echo "$" . " $gtotal " ?> </strong></p>
<p>
<input type="submit" name="submit" value="Pay This Invoice" style="width: 162px; height: 37px" >
<input type="button" name="print" value="Print This Invoice" style="width:162px; height: 37px" onclick="window.print()">
</p>
</form>
</div>
</body>
</html>
<?php
if($_SERVER['METHOD'] == 'POST') {
if(isset($_SESSION['orderIn'])) {
$orderIn_id = $_SESSION['orderIn'];
$orderIn_paid = "Yes";
$changeVal="UPDATE order_instate
SET orderIn_paid = '$orderIn_paid'
WHERE orderIn_id = '$orderIn_id'; " ;
$changeCheck=mysqli_query($dbhandle, $changeVal)
or die(mysqli_error($dbhandle));
}
if(isset($_SESSION['orderOut'])) {
$orderOut_id = $_SESSION['orderOut'];
$orderOut_paid = "Yes";
$changeVal2="UPDATE order_outstate
SET orderOut_paid = '$orderOut_paid'
WHERE orderOut_id = '$orderOut_id'; " ;
$changeCheck2=mysqli_query($dbhandle, $changeVal2)
or die(mysqli_error($dbhandle));
}
$invoiceIn_total = 0;
$invoiceIn_total = $gtotal;
$invoiceIn_shipped = "No";
$add ="INSERT INTO invoice_in(user_id, orderIn_id, invoiceIn_total, invoiceIn_shipped)
VALUES ('$user_id', '$orderIn_id '$invoiceIn_total', '$invoiceIn_shipped')";
$addCheck=mysqli_query($dbhandle, $add)
or die(mysqli_error($dbhandle));
$invoiceOut_total = 0;
$invoiceOut_total = $gtotal;
$invoiceOut_shipped = "No";
$add2 ="INSERT INTO invoice_out(user_id, orderOut_id, invoiceOut_total, invoiceOut_shipped)
VALUES ('$user_id', '$orderOut_total '$invoiceOut_total', '$invoiceOut_shipped')";
$addCheck2=mysqli_query($dbhandle, $add2)
or die(mysqli_error($dbhandle));
header("location: userOrders.php");
}
?>
您的代碼有些錯誤。
有
VALUES ('$user_id', '$orderIn_id '$invoiceIn_total',
^^
缺少引號和逗號
做
VALUES ('$user_id', '$orderIn_id', '$invoiceIn_total',
同樣的事情
VALUES ('$user_id', '$orderOut_total '$invoiceOut_total',
^^
做
VALUES ('$user_id', '$orderOut_total', '$invoiceOut_total',
這些是SQL錯誤的來源。
您發布的代碼中未定義$orderOut_total
。
另外,根據您的評論:
“弗雷德,我發現了為什么我的OrderOut_id沒有被填充,我發現了一個語法錯誤,我在創建和定義變量時沒有使用正確的$ row2來抓取它。盡管我尚未測試,但它現在適用於OrderIn和OrderOut。多訂單。但是,由於弗雷德(Fred)的幫助,我找到了確切的語法錯誤。
我必須注意 ,您當前的代碼可以進行SQL注入 。 使用預處理語句或將PDO與預處理語句結合 使用起來更加安全 。
將錯誤報告添加到文件頂部,這將有助於發現錯誤。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:錯誤報告僅應在登台進行,而不應在生產過程中進行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.