如何从mySQL表中选择行并合并在一起(PHP)

时间:2022-09-21 15:17:00

Hi I am trying to create a shopping site for a project. I have a table in a database that contains the items. I have managed to read from the table. Alongside each row I have added a checkbox. I want the user to select one or more item and press the submit button. When they do this, the details of the order should merge and get added to another table. How can I do this?

嗨,我正在尝试为项目创建一个购物网站。我在包含项目的数据库中有一个表。我已经设法从表中读取。在每行旁边,我添加了一个复选框。我希望用户选择一个或多个项目,然后按“提交”按钮。当他们这样做时,订单的细节应合并并添加到另一个表。我怎样才能做到这一点?

Here is the code to display the items. Each row in the table contains a checkbox. There is also a submit button.

这是显示项目的代码。表中的每一行都包含一个复选框。还有一个提交按钮。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Shopping</title>
</head>
<body>
<form action='buy.php' method='post'>
<input type='submit' value='Submit' />
</form> 
<h1>Buy</h1>
<?php // Script 12.7 - shopping.php

$db = mysql_connect('localhost', '#####', '#####');
mysql_select_db('shopping', $db);

$query = 'SELECT * FROM Items';

if ($r = mysql_query($query, $db)) { 
    print "<table>";
    while ($row = mysql_fetch_array($r)) {
        print 
        "<tr>
        <td>{$row['ID']}</td>
        <td>{$row['Name']}</td>
        <td>{$row['Cost']}</td>
        <td><input type='checkbox' name='buy' value='buy' /></td>
        </tr>";
    }
    print "</table>";

} else { 
    print '<p style="color: blue">Error!</p>';
} 

mysql_close($db); // Close the connection.

?>
</body>
</html>

The page it navigates to (buy.php) is currently empty and thats wat I need help on. I want the details of the order to be entered into the following table:

它导航到的页面(buy.php)目前是空的,那就是我需要帮助。我想将订单的详细信息输入下表:

Table name: order_details
fields: ID, Items, Total_Cost

I want the ID to obviously be a random number. I want the items column to merge the ID's of the items. Finally, I want the Total_cost column to display the sum of all the selected items.

我希望ID显然是一个随机数。我希望items列合并项目的ID。最后,我希望Total_cost列显示所有选定项目的总和。

1 个解决方案

#1


0  

Your checkboxes with name and value set to 'buy' are a bad start. There is no way of identifying the checked item in the script that processes this form. The name should be changed to name='buy[{$row['ID']}]' so you can retrieve the ids of the selected items. Your form should wrap around all the checkboxes (the whole table).

您的名称和值设置为“购买”的复选框是一个糟糕的开始。无法识别处理此表单的脚本中的已检查项目。该名称应更改为name ='buy [{$ row ['ID']}]',以便您可以检索所选项目的ID。您的表单应该包围所有复选框(整个表)。

Your order_details table is a perfect example of how not to do it. Multi-value fields are evil for all sorts of reasons. In a typical order structure you might have orders(id, user_id, created, status) and order_items(id, order_id, item_id, quantity, unit_price). This is a slightly naive but usable structure. Total order value can easily be calculated on the fly based on the quantities and unit_prices.

您的order_details表是如何不这样做的完美示例。由于各种原因,多值字段是邪恶的。在典型的订单结构中,您可能有订单(id,user_id,created,status)和order_items(id,order_id,item_id,quantity,unit_price)。这是一个略显天真但可用的结构。总订单价值可以根据数量和unit_prices轻松计算。

You need to make an attempt at a solution and then ask specific questions instead of asking us to write the whole solution for you. A quick search on Google for "php mysql shopping cart tutorial" returns lots of tutorials on this topic.

您需要尝试解决方案,然后提出具体问题,而不是要求我们为您编写整个解决方案。在Google上快速搜索“php mysql购物车教程”会返回有关此主题的大量教程。

#1


0  

Your checkboxes with name and value set to 'buy' are a bad start. There is no way of identifying the checked item in the script that processes this form. The name should be changed to name='buy[{$row['ID']}]' so you can retrieve the ids of the selected items. Your form should wrap around all the checkboxes (the whole table).

您的名称和值设置为“购买”的复选框是一个糟糕的开始。无法识别处理此表单的脚本中的已检查项目。该名称应更改为name ='buy [{$ row ['ID']}]',以便您可以检索所选项目的ID。您的表单应该包围所有复选框(整个表)。

Your order_details table is a perfect example of how not to do it. Multi-value fields are evil for all sorts of reasons. In a typical order structure you might have orders(id, user_id, created, status) and order_items(id, order_id, item_id, quantity, unit_price). This is a slightly naive but usable structure. Total order value can easily be calculated on the fly based on the quantities and unit_prices.

您的order_details表是如何不这样做的完美示例。由于各种原因,多值字段是邪恶的。在典型的订单结构中,您可能有订单(id,user_id,created,status)和order_items(id,order_id,item_id,quantity,unit_price)。这是一个略显天真但可用的结构。总订单价值可以根据数量和unit_prices轻松计算。

You need to make an attempt at a solution and then ask specific questions instead of asking us to write the whole solution for you. A quick search on Google for "php mysql shopping cart tutorial" returns lots of tutorials on this topic.

您需要尝试解决方案,然后提出具体问题,而不是要求我们为您编写整个解决方案。在Google上快速搜索“php mysql购物车教程”会返回有关此主题的大量教程。