【问题标题】:Getting Excel row data to mysql column in php将Excel行数据获取到php中的mysql列
【发布时间】:2017-09-26 16:59:05
【问题描述】:

我知道如何使用 php 从 excel 获取数据到 mysql。请看下面的excel图表:

我想在下面的mysql表中输入数据。 从 excel 文件列 D、E、F、G 数据将作为行插入 mysql 表中,列 A、B、C 和 H 将作为列输入,但将遵循行号为 A、B、C 和 D

我觉得我问的有点复杂。但请尝试就如何做到这一点提出一些想法或建议。我不能更改excel文件,因为有很多文件要这样处理。

【问题讨论】:

  • 问题的状态如何?如果有人解决了它,则应通过接受答案将其标记为已解决。如果您不知道如何,请询问。只需勾选答案旁边的灰色复选标记,直到它变为绿色。
  • 这一切的代码在哪里?

标签: php mysqli phpexcel excel-match


【解决方案1】:

基本上,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 不是免费的编码服务。
【解决方案2】:

下面是代码,现在可以了。

$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

【讨论】:

    猜你喜欢
    • 2013-03-19
    • 2019-11-23
    • 2013-09-09
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多