我知道如何使用php从excel中获取数据到mysql。请通过下面的Excel图表已经看清楚:获取Excel行数据到php的mysql列
我想输入的数据在MySQL见下表。 从excel文件列D,E,F,G数据将行插入mysql表中&列A,B,C & H将作为列输入,但将按照行号排列为A,B,C & D
我觉得我所要求的有点复杂。但请尝试提供一些想法或建议如何做到这一点。我无法更改excel文件,因为有很多文件需要以这种方式进行。
我知道如何使用php从excel中获取数据到mysql。请通过下面的Excel图表已经看清楚:获取Excel行数据到php的mysql列
我想输入的数据在MySQL见下表。 从excel文件列D,E,F,G数据将行插入mysql表中&列A,B,C & H将作为列输入,但将按照行号排列为A,B,C & D
我觉得我所要求的有点复杂。但请尝试提供一些想法或建议如何做到这一点。我无法更改excel文件,因为有很多文件需要以这种方式进行。
基本上Excel中的一行将在MySQL中插入/更新4行。 可以使用PhpSpreadsheet来轻松迭代Excel文件。
逻辑在pseudo-php-code
将是这样的:
对于只插入:
// Statement prepare
$DB = new PDO('mysql:host='.$host.';dbname='.$base., $user, $pass);
$ST = $DB->prepare('insert into table(field1, fiel2, ..) values (:field1, :field2, ..)');
// Excel iteration
while ($Row = $Excel->NextRow()) {
$ST->execute(['field1' => $Row['A'], .. 'field2' => $Row['D']]);
$ST->execute(['field1' => $Row['A'], .. 'field2' => $Row['E']]);
$ST->execute(['field1' => $Row['A'], .. 'field2' => $Row['F']]);
$ST->execute(['field1' => $Row['A'], .. 'field2' => $Row['G']]);
}
对于同步:
// Statement prepare
$DB = new PDO('mysql:host='.$host.';dbname='.$base., $user, $pass);
// Update statement
$ST = $DB->prepare('update table set field2 = :field2 where field1 = :field2 and ..)');
// Excel iteration
while ($Row = $Excel->NextRow()) {
$ST->execute(['field1' => $Row['A'], .. 'field2' => $Row['D']]);
$ST->execute(['field1' => $Row['A'], .. 'field2' => $Row['E']]);
$ST->execute(['field1' => $Row['A'], .. 'field2' => $Row['F']]);
$ST->execute(['field1' => $Row['A'], .. 'field2' => $Row['G']]);
}
希望你有这个想法。
嗨,谢谢你的回应。作为一个初学者,你能否给我完整的代码/脚本,这让我有点难以理解。 –
完整的工作脚本仅用于复制/粘贴?我喜欢慈善,但很少代码:)我给你的方法,开始思考。 StackOverflow不是免费的编码服务。 – Marcodor
下面的代码,现在它很好。
$fft1=888;
$fft2=777;
$fft3="";
$fft4=999;
$fft5=100;
$ctnqty=10;
$friendslist = "$fft1,$fft2,$fft3,$fft4";
$id = $fft5;
$friendarray = explode(",", $friendslist);
$frienduserarray = array();
for ($n = 0; $n < count($friendarray); $n++) {
$friendidpush = "('".$id."','".$friendarray[$n]."','".$ctnqty."'),";
array_push($frienduserarray, $friendidpush);
}
$query = "INSERT INTO freddyhipment (style, orderno,ctnqty) VALUES ";
$friendarray = explode(",", $friendslist);
foreach ($friendarray as $order) {
$query .= "('" . $id . "','" . $order . "','" . $ctnqty . "'),";
}
$query = substr($query, 0, -1); // remove trailing comma
问题的状态是什么?如果解决了这个问题,应该通过接受答案来标记为解决问题。如果你不知道如何,请询问。只需在答案旁边勾上灰色复选标记,直到它变成绿色即可。 –
所有这些代码在哪里? –