Insert into MySQL from array 根据某些条件一次插入到两个表中

Insert into MySQL from array into two tables at once based on some conditions

我有这个数组,其中的数据行来自 excel 文件,在 php 检查 https://github.com/shuchkin/simplexlsx 中与 SimpleXLSX 一起使用 工作正常,但问题是我希望所有数据将它们插入两个 MySQL tables

在table 1 还可以,但是在table 2 就是问题所在,我想把从table 1 输入的所有最后插入的ID 都插入到table 2 带有 dansatori ID,如果你在 [dansatori]

查看 SimpleXLSX 输出,它是一个数组

请查看此图片以查看我想要实现的目标: MySQL Tables

在 PHP 中排列 SimpleXLSX 输出:

Array
(
    [0] => Array
        (
            [numecoregrafie] => Vampire Dance
            [niveldans] => 2
            [coregraf] => Damon Salvatore
            [sectiuni] => 1
            [disciplina] => 7
            [catvarsta] => 6
            [dansatori] => Array
                (
                    [0] => 84
                    [1] => 86
                )

            [nrdansatori] => 2
        )

    [1] => Array
        (
            [numecoregrafie] => End of the world
            [niveldans] => 1
            [coregraf] => Stephany
            [sectiuni] => 2
            [disciplina] => 14
            [catvarsta] => 4
            [dansatori] => Array
                (
                    [0] => 82
                    [1] => 87
                )

            [nrdansatori] => 2
        )

    [2] => Array
        (
            [numecoregrafie] => Slapping Chris Rock
            [niveldans] => 2
            [coregraf] => Will Smith
            [sectiuni] => 1
            [disciplina] => 13
            [catvarsta] => 18
            [dansatori] => Array
                (
                    [0] => 84
                )

            [nrdansatori] => 1
        )

)

到目前为止我已经尝试过:

$file  = "MomenteMultiple_RDCP_2.xlsx";  // The excel file 
$xlsx  = new SimpleXLSX( $file );  // SimpleXLSX object

$dns       = array();
$skip      = 1; 
$dansatori = array();
$lastID    = array();

foreach ($xlsx->rows() as $key => $fields)
 { 
    if($skip !=1)   // Skipping the first row from XLSX file.
    {
      if($fields[7] > 0) {
         
        $rowValue   = !empty($fields) && $fields != "" ? $fields : 'null';   
        $result     = array_filter($rowValue);  // remove empty values from array
        $values     = explode(",",  $result[6]);  // explode string into array
        $dancersIDS = array_filter($values);  
        
        $dns[] = [
            'numecoregrafie' => $result[0],
            'niveldans'      => $result[1],
            'coregraf'       => $result[2],
            'sectiuni'       => $result[3],
            'disciplina'     => $result[4],
            'catvarsta'      => $result[5],
            'dansatori'      => $dancersIDS,
            'nrdansatori'    => $result[7],
            'uid'            => $user->filter->id
        ];   // Add the values to the array
      }   
    }
    $skip++;  // Increment the skip value
 }
 
// Table 1
 $query = 
 'INSERT INTO `rdcp_momente` 
  (numecoregrafie, 
   niveldans, 
   coregraf, 
   sectiuni, 
   disciplina, 
   catvarsta, 
   nrdansatori
   ) VALUES';   // Query to insert values into table 1
  
  // Table 2
 $queryd = 
 'INSERT INTO `rdcp_dansatorim` 
  (`did`, 
  `uid`,
   `mid`
  ) VALUES';   // Query to insert values into table 2
  
  foreach($dns as $d) { 
      $query .= "
      (
        '".$d['numecoregrafie']."', 
        '".$d['niveldans']."', 
        '".$d['coregraf']."', 
        '".$d['sectiuni']."', 
        '".$d['disciplina']."', 
        '".$d['catvarsta']."', 
        '".$d['nrdansatori']."'
      ),"; // Query to insert values into table 1
      foreach($d['dansatori'] as $dansator) 
      { 
        $queryd .= "
        (
          '".$dansator."', 
          '".$user->filter->id."', 
          '".$lastID."'
        ),"; // LastID is the last inserted id of the table 1
      } 
  } 
  
  $query  = rtrim($query, ",");  // remove last comma
  $queryd = rtrim($queryd, ","); 
  $query .= ";";  
  $queryd .= ";"; 
  $db->query($query);  // insert into table 1 
  $lastID[] = $db->insertId(); // get the last inserted id
  $db->query($queryd); // insert into table 2
   
    echo '<pre>';  
    echo var_dump($query);    
    echo '<pre>';  
    echo var_dump($queryd);

只插入了一个 ID,而不是与最后插入的行对应的所有 ID

  1. 准备您的 INSERT 声明
  2. 绑定参数
  3. 遍历数据
    1. 执行你的第一个语句
    2. 获取最后插入的ID
    3. 遍历 dansatori 索引
      1. 执行你的第二条语句

因为 mysqli_stmt::bind_param 采用变量引用,您可以在开始迭代数据之前准备和绑定参数。

$stmt1 = $db->prepare('INSERT INTO `rdcp_momente` (numecoregrafie, niveldans, coregraf, sectiuni, disciplina, catvarsta, nrdansatori) VALUES (?, ?, ?, ?, ?, ?, ?)');
// you might want to tweak the parameter types
$stmt1->bind_param('sssssss',
    $d['numecoregrafie'], 
    $d['niveldans'], 
    $d['coregraf'], 
    $d['sectiuni'], 
    $d['disciplina'], 
    $d['catvarsta'], 
    $d['nrdansatori']);

$stmt2 = $db->prepare('INSERT INTO `rdcp_dansatorim` (`did`, `uid`, `mid`) VALUES (?, ?, ?)');
$stmt2->bind_param('ssi', $dansator, $user->filter->id, $lastId);

foreach ($dns as $d) {
    $stmt1->execute();
    $lastId = $db->insert_id;
    foreach($d['dansatori'] as $dansator) {
        $stmt2->execute();
    }
}