gpt4 book ai didi

PHP 将选定的数量发布到表中

转载 作者:行者123 更新时间:2023-11-29 19:45:17 24 4
gpt4 key购买 nike

我对 PHP 和一般的 Web 编程还比较陌生。当我尝试从一页中获取选定数量的项目,并将每个选择和在单独的 PHP 页面上的表格中选择的数量发布时,我遇到了问题。从那里,我需要显示选择、选择的数量、价格和总数。如果有人能够帮助引导我朝正确的方向前进,我将不胜感激!我已经缩小了问题范围,这样当我选择每个选项的数量并单击“提交”时,它就不会处理并将我的所有选择添加到表中。预先感谢您投入的时间和精力!

网站 03 shop.php

<?php
session_start();
include("website 03 library.php");
pagetop("WE WANT YOU TO SHOP");
echo "<span id = 'message' class = 'message'>Select How Many You Want of Each Product</span>";
$host_name = "localhost";
$user_name = "web";
$password = "programming";
$database = "assignments";
$connection = mysqli_connect($host_name, $user_name, $password, $database);
$sql = "select * from products";
$recordSet = mysqli_query($connection, $sql);
while ($record = mysqli_fetch_array ($recordSet, MYSQLI_ASSOC)) {
$pid = $record['pid'];
echo "<form action = 'website 03 cart.php' method = 'post'>";
echo "<h2 name = 'NAME$pid'>$record['name']</h2><br><br>";
echo "<img name = 'IMAGE$pid' style = 'height:280px; width:350px; ' src = '$record['url']'><br><br>";
echo "<span name = 'PRICE$pid'>Price of each $record['name'] is $record['price']. How many do you want? </span>";
echo "<select name = 'QUANTITY$pid'>";
echo "<option value = 0>0</option>";
echo "<option value = 1>1</option>";
echo "<option value = 2>2</option>";
echo "<option value = 3>3</option>";
echo "<option value = 4>4</option>";
echo "</select>";
echo "<input type = 'submit' class = 'button' name = 'SUBMIT$pid' value = 'SUBMIT'>";
echo "</form>";
};
mysqli_free_result($recordSet);
mysqli_close($connection);
?>

第二页(接收所选值)

网站 03 cart.php

<?php
session_start();
include("website 03 library.php");
pagetop("SHOPPING CART");
$host_name = "localhost";
$user_name = "web";
$password = "programming";
$database = "assignments";
$connection = mysqli_connect($host_name, $user_name, $password, $database);

echo "<table border = 1>";
$sql = "select * from products";
$recordSet = mysqli_query($connection, $sql);
while ($record = mysqli_fetch_array($recordSet, MYSQLI_ASSOC)) {
$pid = $record['pid'];
$url = $record['url'];
$price = $record['price'];
$key = "QUANTITY$pid";
$quantity = $_POST['$key'];
$total = $quantity * $price;
if ($quantity > 1) {
echo "<tr>";
echo "<td><img style = 'height:110; width:150; ' src = $url></td>";
echo "<td>$quantity</td>";
echo "<td>$price</td>";
echo "<td>$total</td>";
echo "</tr>";
echo "<tr>";
echo "<td>TOTAL:</td>";
echo "<td>$total</td>";
echo "</tr>";
echo "<tr>";
echo "</tr>";
};
};
echo "</table>";
?>

我的目的是从 03 商店获取发布的数据并将其放入 03 购物车的表格中。有点像网上商店。例如,我会选择 2 个“时钟”和 3 个“自行车”,然后该数据将按照以下顺序发布到 03 购物车的表格中:图像、数量、价格和总计(数量 * 价格)。我相信我的问题是“提交”按钮位于循环内,因此它只会从一个部分发布。但是,我不知道如何使用“提交”按钮将一篇文章中的所有选择引用到网站 03 购物车。

最佳答案

获取您的<form>循环外的标记,更改您的 03 shop while循环得到<form>标记在循环之外。

另请注意 $variables很复杂,例如 $record['name']不能嵌入没有大括号的双引号中,如下所示:{$record['name']} .

// print the form open 
echo "<form action = 'website 03 cart.php' method = 'post'>";
// do the loop
while ($record = mysqli_fetch_array ($recordSet, MYSQLI_ASSOC)) {
$pid = $record['pid'];

// complex vars inside double quotes require special syntax
echo "<h2 name = 'NAME$pid'>{$record['name']}</h2><br><br>";

echo "<img name = 'IMAGE$pid' style = 'height:280px; width:350px; ' src = '{$record['url']}'><br><br>";
echo "<span name = 'PRICE$pid'>Price of each {$record['name']} is {$record['price']}. How many do you want? </span>";
echo "<select name = 'QUANTITY$pid'>";
echo "<option value = 0>0</option>";
echo "<option value = 1>1</option>";
echo "<option value = 2>2</option>";
echo "<option value = 3>3</option>";
echo "<option value = 4>4</option>";
echo "</select>";
echo "<input type = 'submit' class = 'button' name = 'SUBMIT' value = 'SUBMIT'>";
};
// now close the form
echo "</form>";

关于PHP 将选定的数量发布到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41051193/

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