如何向mysql表插入数组数据

时间:2022-09-25 19:58:01

I have two tables called op_group and options .These two tables are filled by array values. For example, I want to insert pizza details into my tables.

我有两个名为op_group和options的表,这两个表由数组值填充。例如,我想在我的表中插入比萨饼的详细信息。

It look like this:

它看起来像这样:

  1. Cheese pizza and Sausage pizza will get inserted into the op_group table.
  2. 奶酪披萨和香肠披萨将被插入到op_group表中。
  3. Other data will get inserted into the options table. These options should be inserted based on the relevant foreign key op_group id.
  4. 其他数据将被插入到options表中。应该根据相关的外键op_group id插入这些选项。

如何向mysql表插入数组数据

This is what i have tried. All details are inserted but not for the relevant op_group.

这就是我所尝试的。插入所有细节,但不插入相关的op_group。

$opg_name=$_POST['opg_name'];
$price=$_POST['price'];

$itemCountz = count($opg_name);
$itemValues=0;
$queryValue1 = "";

for($i=0; $i<$itemCountz; $i++) {
  $itemValues++;
  if($queryValue1!="") {
    $queryValue1 .= ",";
  }
  $queryValue1 = "INSERT INTO op_group  
                  (opg_id,ml_id,cat_id,res_id,res_name,op_grp)                 
                 VALUES(NULL,'".$ml_id."','".$cat_id."','".$res_id."','".$res_name."','".$_POST["opg_name"][$i]."')";

  $result1= mysql_query($queryValue1)or die(mysql_error());                                 

  $query6= "SELECT  * FROM op_group ORDER BY opg_id DESC LIMIT 1" ;
  $result6= mysql_query($query6);

  while($row6 = mysql_fetch_assoc($result6)){
    $opg_id=$row6['opg_id'];
  }

  $itemCount2 = count($price);
  $itemValues1 = 0;

  $queryValue2 = "";
  for($j=0;$j<$itemCount2;$j++) {
    if(!empty($_POST["op_name"][$j])||!empty($_POST["price"][$j])) {
      $itemValues1++;
      if($queryValue2!="") {
        $queryValue2 .= ",";
      }

      $queryValue2 = "INSERT INTO options (op_id,opg_id,ml_id,cat_id,res_id,res_name,opt,price) VALUES (NULL,'".$opg_id."','".$ml_id."','".$cat_id."','".$res_id."','".$res_name."','".$_POST["op_name"][$j]."','".$_POST["price"][$j]."')";
    }           
  }

  $result2=mysql_query($queryValue2)or die(mysql_error());
}

This code give result like this

这段代码给出的结果是这样的

如何向mysql表插入数组数据

options table inserted data looks like this 如何向mysql表插入数组数据

选项表插入数据如下所示

how to solve this?

如何解决呢?

1 个解决方案

#1


1  

The reason why you are seeing options being duplicated for each option group is because your $queryValue2 is being repeatedly executed for each option group inside your outer most for($i = 0; $i < $itemCountz; $i++) loop.

您看到每个选项组的选项被重复的原因是,您的$queryValue2在您的最外层的每个选项组被重复执行($i = 0;我< itemCountz美元;我+ +)美元循环。

As it stands, your current way of organizing the $_POST inputs are quite messy (and so are your duplicated row attributes). I will suggest you group your HTML inputs fields by each option group. You can try something like this:

目前,组织$_POST输入的当前方式非常混乱(重复的行属性也是如此)。我建议您将HTML输入字段按每个选项组分组。你可以试试这样的方法:

<div>
  <input type="text" id="first_order" name="orders[0][name]" /><br />
  <input type="text" name="orders[0][options][0][price]" /><br />
  <input type="text" name="orders[0][options][0][size]" /><br />   
  <input type="text" name="orders[0][options][1][price]" /><br />
  <input type="text" name="orders[0][options][1][size]" /><br /> 
</div>

<div>
  <input type="text" id="second_order" name="orders[1][name]" /><br />
  <input type="text" name="orders[1][options][0][price]" /><br />
  <input type="text" name="orders[1][options][0][size]" /><br />   
  <input type="text" name="orders[1][options][1][price]" /><br />
  <input type="text" name="orders[1][options][1][size]" /><br /> 
</div>

Notice how I use the HTML name attribute to group the food options by the food. Then if you submit this form, you will see that the $_POST array appears to look something like this:

注意我如何使用HTML name属性将食物选项按食物分组。然后,如果您提交此表单,您将看到$_POST数组看起来如下所示:

Array
(
    [orders] => Array
    (
        [0] => Array
        (
            [name] => "Cheese Pizza"
            [options] => [0] => Array([size] => "8'" 
                                      [price] => "12")
                         [1] => Array([size] => "12'" 
                                      [price] => "14")
        )

        [1] => Array
        (
            [name] => "Sausage Pizza"
            [options] => [0] => Array([size] => "8'" 
                                      [price] => "13")
                         [1] => Array([size] => "12'" 
                                      [price] => "16")
        )
    )
)

Now you can tidy up your backend PHP codes with something like this:

现在,您可以用以下方法整理后端PHP代码:

foreach ($_POST['orders'] as $orders) {

  // .. some codes

  foreach($orders as $order) {
    // .. add codes to insert into op_groups where $order['name'] will give you the pizza name

    foreach($order['options'] as $details) {
      // .. add codes to insert into options where $details['size'] and $details['price'] will give you the corresponding values
    }
  }
}

Also, you can use mysqli.insert-id to get the row ID of the last inserted row. Then you can get rid of that silly $query6 and its while loop.

此外,还可以使用mysqli。插入ID以获取最后插入行的行ID。然后你就可以摆脱那个愚蠢的$query6和它的while循环。

Of course, don't forget to sanitize your $_POST inputs before using them!

当然,在使用$_POST输入之前,不要忘记对其进行清理!

I am getting a bit tired, if there are mistakes, I will fix them up tomorrow. Hope it helps!

我有点累了,如果有错误,我明天就去补上。希望它可以帮助!

#1


1  

The reason why you are seeing options being duplicated for each option group is because your $queryValue2 is being repeatedly executed for each option group inside your outer most for($i = 0; $i < $itemCountz; $i++) loop.

您看到每个选项组的选项被重复的原因是,您的$queryValue2在您的最外层的每个选项组被重复执行($i = 0;我< itemCountz美元;我+ +)美元循环。

As it stands, your current way of organizing the $_POST inputs are quite messy (and so are your duplicated row attributes). I will suggest you group your HTML inputs fields by each option group. You can try something like this:

目前,组织$_POST输入的当前方式非常混乱(重复的行属性也是如此)。我建议您将HTML输入字段按每个选项组分组。你可以试试这样的方法:

<div>
  <input type="text" id="first_order" name="orders[0][name]" /><br />
  <input type="text" name="orders[0][options][0][price]" /><br />
  <input type="text" name="orders[0][options][0][size]" /><br />   
  <input type="text" name="orders[0][options][1][price]" /><br />
  <input type="text" name="orders[0][options][1][size]" /><br /> 
</div>

<div>
  <input type="text" id="second_order" name="orders[1][name]" /><br />
  <input type="text" name="orders[1][options][0][price]" /><br />
  <input type="text" name="orders[1][options][0][size]" /><br />   
  <input type="text" name="orders[1][options][1][price]" /><br />
  <input type="text" name="orders[1][options][1][size]" /><br /> 
</div>

Notice how I use the HTML name attribute to group the food options by the food. Then if you submit this form, you will see that the $_POST array appears to look something like this:

注意我如何使用HTML name属性将食物选项按食物分组。然后,如果您提交此表单,您将看到$_POST数组看起来如下所示:

Array
(
    [orders] => Array
    (
        [0] => Array
        (
            [name] => "Cheese Pizza"
            [options] => [0] => Array([size] => "8'" 
                                      [price] => "12")
                         [1] => Array([size] => "12'" 
                                      [price] => "14")
        )

        [1] => Array
        (
            [name] => "Sausage Pizza"
            [options] => [0] => Array([size] => "8'" 
                                      [price] => "13")
                         [1] => Array([size] => "12'" 
                                      [price] => "16")
        )
    )
)

Now you can tidy up your backend PHP codes with something like this:

现在,您可以用以下方法整理后端PHP代码:

foreach ($_POST['orders'] as $orders) {

  // .. some codes

  foreach($orders as $order) {
    // .. add codes to insert into op_groups where $order['name'] will give you the pizza name

    foreach($order['options'] as $details) {
      // .. add codes to insert into options where $details['size'] and $details['price'] will give you the corresponding values
    }
  }
}

Also, you can use mysqli.insert-id to get the row ID of the last inserted row. Then you can get rid of that silly $query6 and its while loop.

此外,还可以使用mysqli。插入ID以获取最后插入行的行ID。然后你就可以摆脱那个愚蠢的$query6和它的while循环。

Of course, don't forget to sanitize your $_POST inputs before using them!

当然,在使用$_POST输入之前,不要忘记对其进行清理!

I am getting a bit tired, if there are mistakes, I will fix them up tomorrow. Hope it helps!

我有点累了,如果有错误,我明天就去补上。希望它可以帮助!