gpt4 book ai didi

php - 在文本字段订单表格中打印总数

转载 作者:行者123 更新时间:2023-11-30 22:35:34 26 4
gpt4 key购买 nike

我正在创建一个订单表格,其中数量是用户在文本字段中输入的位置,而价格已经存储在数据库中。

我希望产品的数量是产品价格的倍数,并将结果显示在总计字段中。

然后统计所有的总数并显示在ORDER TOTAL 字段中。但我被困在中间,不知道该怎么做。

我希望有人能在这方面指导我。这是我的代码:

<?php
//setting connection to the database
$connection = mysqli_connect("localhost", "root", "", "SCINFO");
//checking the connection
if(mysqli_connect_errno()){
echo "Connection Failed!";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Order -- SHOPPERS||COLLECTION</title>
</head>
<body>
<h2 align="left">Purchase/Order Items</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); >">
<table cellpadding="15">
<tr bgcolor="#996699">
<td>Product ID</td>
<td>Product Name</td>
<td>Quantity</td>
<td>Price (RM)</td>
<td>Total</td>
</tr>
<?php
//prints the data in table
$result = mysqli_query($connection, "SELECT * FROM PRODUCT");
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result)) {
// echo out the contents of each row
echo "<tr>";
echo '<td>' . $row['PRD_ID'] . '</td>'; //prints out the product ID
echo '<td>' . $row['PRD_NAME'] . '</td>'; //prints out the product name
echo '<td><input type="text" name="qty"> X</td>';
echo '<td>'.$row['PRD_PRICE'].'</td>';
echo '<td><input type="text" name="total" value="" disabled="disabled"></td>';
echo '</tr>';
}
?>
<tr>
<td colspan="5"><hr></td>
</tr>
<tr>
<td colspan="4" align="right">ORDER TOTAL:</td>
<td><input type="text" name="ototal" value="RM " disabled="disabled"></td>
</tr>
<tr>
<td colspan="4" align="right"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
if($_POST["submit"]){
$qty = $_POST['qty'];
$price = $_POST['prc'];
if(isset($_POST['submit'])){
$sum = $qty * $price;
}
}
}
?>
</body>
</html>

最佳答案

如果您说价格已经在数据库中,那么您为什么要使用 $_POST['prc'] ?因为据我所知,当您想从表单中获取值(value)时,会使用 $_POST[] 。假设

 <html>
<body>
<form method="post" >
<input type="text" name="userName" value="" />
<input type="submit" value="Click me"
</form>


<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// POST is used to collect value of input field
$name = $_POST['userName'];
if (empty($name))
{
echo "Name is empty";
} else {
echo $name;
}
?>

</body>
</html>

关于php - 在文本字段订单表格中打印总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32635639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com