我在 php 檢查https://github.com/shuchkin/simplexlsx中有一個包含來自與 SimpleXLSX 一起使用的 excel 檔案的資料行的陣列,作業得很好,但問題是我希望將所有資料插入到兩個 MySQL 表中
在表 1 中沒問題,但在表 2 中是問題所在,我想獲取從表 1 輸入的所有最后插入的 ID,并將它們插入到表 2 中,如果您查看 [dansatori ]
請檢查此影像以查看我想要實作的目標: MySQL 表
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
uj5u.com熱心網友回復:
- 準備你的
INSERT陳述 - 系結引數
- 遍歷資料
- 執行你的第一條陳述句
- 獲取最后一個插入 ID
- 遍歷
dansatori索引- 執行你的第二條陳述句
因為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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/460078.html
上一篇:C中堆疊上陣列的記憶體分配限制?
下一篇:如何回傳一個空陣列Java
