Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将数组数据插入mysql表_Php_Mysql - Fatal编程技术网

Php 如何将数组数据插入mysql表

Php 如何将数组数据插入mysql表,php,mysql,Php,Mysql,我有两个名为op_group和options的表。这两个表由数组值填充。例如,我想在我的表格中插入比萨饼的详细信息 它看起来像这样: <div> <input type="text" id="first_order" name="orders[0][name]" /><br /> <input type="text" name="orders[0][options][0][price]" /><br /> <input

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

它看起来像这样:

<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>
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")
        )
    )
)
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
    }
  }
}
  • 奶酪比萨饼和香肠比萨饼将被插入
    op_组
    表中
  • 其他数据将插入
    选项
    表中。应根据相关外键
    op_组
    id插入这些选项
  • 这就是我尝试过的。插入所有详细信息,但不针对相关的
    op\u组

    $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());
    }
    
    $opg_name=$_POST['opg_name'];
    $price=$_POST['price'];
    $itemCountz=count($opg_name);
    $itemValues=0;
    $queryvalue=“”;
    
    对于($i=0;$i而言,您看到每个选项组的选项都被复制的原因是($i=0;$i<$itemCountz;$i++)的
    $queryValue2
    在最外层的
    循环中对每个选项组重复执行

    目前,您组织
    $\u 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>
    
    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")
            )
        )
    )
    
    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
        }
      }
    }
    
    现在,您可以使用以下内容整理后端PHP代码:

    <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>
    
    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")
            )
        )
    )
    
    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
        }
      }
    }
    
    此外,您还可以使用获取最后一个插入行的行ID。然后您可以摆脱愚蠢的
    $query6
    及其
    while
    循环

    当然,在使用您的
    $\u POST
    输入之前,不要忘记对其进行消毒!


    我有点累了,如果有错误,我明天会解决。希望有帮助!

    您可以使用PHP serialize()数组将数组值存储到数据库中,并在检索时取消序列化。或者使用json_encode()和json_decode()将其存储到数据库中并进行检索。$query6=“按opg_id DESC LIMIT 1从op_group ORDER中选择*”;这是一个问题,您要做的是重新选择op_group中的所有条目,然后为每个op_group插入所有选项,而不检查它是否属于奶酪比萨饼或香肠pizza@AchalSaraiya它只选择插入的opg_id。($opg_id=$row6['opg_id'];)。例如,它是echo 221和222。我如何检查相关选项组的选项?您可以将所有这些选项删除。“-yuk。并且不要以您建议的方式存储数据。只需将所有内容保持良好和正常。