如果表单中只有一个值,如何插入多个值

时间:2022-12-25 04:22:36

I need to know how can I insert or update a value in my DB for a invoice form if the value of the input that I need to save is only one time?

如果我需要保存的输入值只有一次,我需要知道如何在发票表单的数据库中插入或更新值?

I have a invoice form where I select a product in every row, and finally I have the input(user_id) with the value I need to save with the rest of the inputs

我有一张发票表单,我在每行中选择一个产品,最后我输入(user_id),其中包含我需要保存的值以及其余输入

Like per example, I choose in the invoice:

就像每个例子,我在发票中选择:

10 tomatoes 
 5 garlics
 2 beans

and finally there is my Id (user_id, not the Id of PRODUCTOS table that is unique)

最后有我的Id(user_id,而不是PRODUCTOS表的Id是唯一的)

Id=1

Here is the schema of my table, and how save or update until now:

这是我的表的架构,以及如何保存或更新到现在:

| cantidad   | nombre del producto |   Id  | 
+------------+---------------------+-------+
|     10     |   Tomatoes          |     1 |
|      5     |   garlics           |     0 |
|      2     |   beans             |     0 |

Here what I need to save or update:

这是我需要保存或更新的内容:

| cantidad   | nombre del producto |   Id  | 
+------------+---------------------+-------+
|     10     |   Tomatoes          |     1 |
|      5     |   garlics           |     1 |
|      2     |   beans             |     1 |

Here is the code:

这是代码:

            $conn->beginTransaction();
            $sql = "INSERT INTO PRODUCTOS
            (cantidad, nombreProd, Id)
             VALUES ";
            $insertQuery = array();
            $insertData = array();
            foreach ($_POST['cantidad'] as $i => $cantidad) {
                $insertQuery[] = '(?, ?, ?)';
                $insertData[] = $_POST['cantidad'][$i];
                $insertData[] = $_POST['nombreProd'][$i];
                $insertData[] = $_POST['Id'][$i];
            }
            if (!empty($insertQuery)) {
                $sql .= implode(', ', $insertQuery);
                $stmt = $conn->prepare($sql);
                $stmt->execute($insertData);
            }
            $conn->commit();

Thank you

1 个解决方案

#1


So you have a single userID you want to INSERT for each product record. I can probably provide a better answer if you post the output of print_r($_POST), but basically in your foreach $POST loop, keep the user ID static:

因此,您希望为每个产品记录插入一个用户ID。如果您发布print_r($ _ POST)的输出,我可以提供更好的答案,但基本上在您的foreach $ POST循环中,保持用户ID为静态:

foreach ($_POST['cantidad'] as $i => $cantidad) {
  $insertQuery[] = '(?, ?, ?)';
  $insertData[] = $_POST['cantidad'][$i];
  $insertData[] = $_POST['nombreProd'][$i];
  $insertData[] = $_POST['Id']; //OR $insertData[] = $_POST['Id'][0], depending on $_POST array
            }

#1


So you have a single userID you want to INSERT for each product record. I can probably provide a better answer if you post the output of print_r($_POST), but basically in your foreach $POST loop, keep the user ID static:

因此,您希望为每个产品记录插入一个用户ID。如果您发布print_r($ _ POST)的输出,我可以提供更好的答案,但基本上在您的foreach $ POST循环中,保持用户ID为静态:

foreach ($_POST['cantidad'] as $i => $cantidad) {
  $insertQuery[] = '(?, ?, ?)';
  $insertData[] = $_POST['cantidad'][$i];
  $insertData[] = $_POST['nombreProd'][$i];
  $insertData[] = $_POST['Id']; //OR $insertData[] = $_POST['Id'][0], depending on $_POST array
            }