带分隔符的CSV格式数据输入到mysql

时间:2019-01-02 08:26:08

标签: php mysql mysqli export-to-csv

我想将带有分隔符(|)的csv格式的输入列表从Textarea导入我的MySQL数据库。

但是它总是失败,即使格式正确也是如此。

消息:Error, 0 added successfully.

我的格式:

Thomas|Maier|5778011152|Bahnweg|232

有一个主意吗?

if(isset($_POST["base_name"]) && isset($_POST["new_entry"])) {
    $myList = preg_replace("/\r|\n/", "", preg_split("/$\R?^/m", $_POST["new_entry"]));
    foreach($myList as $info) $csv[] = explode($_POST["separator"], $info);

    $csvParams = $csv[0];
    array_shift($csv);

    $success = 0;
    $total = 0;

    foreach($csv as $info) {
        $sqlInsert = array_combine($csvParams, $info);
        $sqlInsert["base"] = $_POST["base_name"];
        $sqlInsert["info"] = "unbenutzt";

        $p = softwareInsertArray($softwareSqlLink, $sqlInsert, "person");
        if($p) $success++;
        $total++;
    }

    $q = softwareRunQuery($softwareSqlLink, false, "INSERT INTO statistics (`key`, `val`) VALUES ('".$_POST["base_name"]."|profit', '0');");
}

if(isset($total)) {
    if($total == $success && $total > 0 && $q) {
        $alert = array("type" => "success", "header" => "Erfolgreich!", "text" => "Success.");
        softwareSqlLog($softwareSqlLink, "person", array("status" => 1, "info" => array("base" => $_POST["base_name"], "text" => $success." person added successfully")));
    } else {
        $alert = array("type" => "danger", "header" => "Fehler!", "text" => $success."/".$total." sucess.");
        softwareSqlLog($softwareSqlLink, "person", array("status" => 0, "info" => array("base" => $_POST["base_name"], "text" => $success."/".$total." added.")));
    }
}
  

softwareInsertArray代码:pastebin.com/raw/vUnQTpxU

1 个答案:

答案 0 :(得分:0)

您是否只想用逗号替换管道? IE SELECT replace('Thomas | Maier | 5778011152 | Bahnweg | 232','|',',')