将数组添加到Mysql的最佳方法是什么

时间:2022-11-24 15:46:33

I am making a simple order form with 5 products and they can be select with a checkbox from the user and then I want to add the products that the visitors will select into mysql database, but as you know for example INSERT array[] doest work!

我正在制作一个包含5个产品的简单订单,可以从用户选中一个复选框,然后我想将访问者选择的产品添加到mysql数据库中,但正如您所知,例如INSERT array [] doest work !

I made an FOR stamtment with php but it adds only the last product of an array!

我用PHP做了一个FOR stamtment但是它只添加了数组的最后一个产品!

I know a way the serialize and unserialize php functions but I dont really like it!

我知道序列化和反序列化PHP函数的方式,但我真的不喜欢它!

Thank you for your help.

感谢您的帮助。

Here is the code:

这是代码:

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$products = $_POST['products'];
$email = $_POST['email'];

echo '<h3>Sehr geehrte/er '.$firstname.' '.$lastname.'</h3>';


  if(empty($products)) 
  {
    echo("Keine bestellung.");
  } 
  else 
  {
    $N = count($products);

    echo("Sie haben $N produkte bestellt:<br /> ");
    for($i=0; $i < $N; $i++)
    {
      echo $order = ($products[$i]);    
    }
    $query = "INSERT INTO salt_orders (products, vorname, nachname, date)
        VALUES ( '$order', '$firstname', '$lastname', NOW())";
    $result = mysql_query($query);
  }

3 个解决方案

#1


1  

Create a seperate table which references your products and their details. Then create another table to hold order line items.

创建一个单独的表,引用您的产品及其详细信息。然后创建另一个表来保存订单行项目。

When a customer places and order, insert a new line referencing which product they bought and which order number it applies to in the line items table. Create a record in salt_orders which holds the order number.

当客户下订单时,插入一个新行,引用他们购买的产品以及它应用于订单项表中的订单号。在salt_orders中创建一个包含订单号的记录。

Then you can get it all back out by joining them together to retrieve the full order details.

然后,您可以将它们连接在一起以检索完整的订单详细信息。

Adding multiple products to one cell in a row will make it very hard on you, so I suggest normalizing your data like stated above.

将多个产品连续添加到一个单元格会让你很难,所以我建议如上所述规范化你的数据。

This just a simple example, it can be much more complex depending on what you need it to do.

这只是一个简单的例子,它可能会更加复杂,具体取决于您需要它做什么。

#2


1  

You can use this to concat the products in one field:

您可以使用它在一个字段中连接产品:

$order = "";
for($i=0; $i < $N; $i++)
{
  echo $order .= $products[$i] . "," ;    
}
$order = substr($order ,0 ,strlen($order )-1 ) ;

But you should really consider normalization of your orders table, as all others have suggested.

但是你应该像所有其他人所建议的那样考虑你的订单表的规范化。

#3


1  

Yes, normalization is what you are probably missing. When you have finished that, add functions to your database that handle the inserting to your normalized tables. This way the logic for the data stays with the data.

是的,规范化是你可能缺少的。完成后,将数据库中的函数添加到处理规范化表的插入中。这样,数据的逻辑就与数据保持一致。

#1


1  

Create a seperate table which references your products and their details. Then create another table to hold order line items.

创建一个单独的表,引用您的产品及其详细信息。然后创建另一个表来保存订单行项目。

When a customer places and order, insert a new line referencing which product they bought and which order number it applies to in the line items table. Create a record in salt_orders which holds the order number.

当客户下订单时,插入一个新行,引用他们购买的产品以及它应用于订单项表中的订单号。在salt_orders中创建一个包含订单号的记录。

Then you can get it all back out by joining them together to retrieve the full order details.

然后,您可以将它们连接在一起以检索完整的订单详细信息。

Adding multiple products to one cell in a row will make it very hard on you, so I suggest normalizing your data like stated above.

将多个产品连续添加到一个单元格会让你很难,所以我建议如上所述规范化你的数据。

This just a simple example, it can be much more complex depending on what you need it to do.

这只是一个简单的例子,它可能会更加复杂,具体取决于您需要它做什么。

#2


1  

You can use this to concat the products in one field:

您可以使用它在一个字段中连接产品:

$order = "";
for($i=0; $i < $N; $i++)
{
  echo $order .= $products[$i] . "," ;    
}
$order = substr($order ,0 ,strlen($order )-1 ) ;

But you should really consider normalization of your orders table, as all others have suggested.

但是你应该像所有其他人所建议的那样考虑你的订单表的规范化。

#3


1  

Yes, normalization is what you are probably missing. When you have finished that, add functions to your database that handle the inserting to your normalized tables. This way the logic for the data stays with the data.

是的,规范化是你可能缺少的。完成后,将数据库中的函数添加到处理规范化表的插入中。这样,数据的逻辑就与数据保持一致。