Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在db中插入csv文件,但不使用逗号-PHP_Php_Mysql - Fatal编程技术网

如何在db中插入csv文件,但不使用逗号-PHP

如何在db中插入csv文件,但不使用逗号-PHP,php,mysql,Php,Mysql,大家好,我在我的项目中实现了一个scv文件导入器,其中相关数据位于mysql表中,我的问题是,如果数据以逗号分隔,则会进入不同的表中,就像我在加载数据时不使用逗号一样,但代码识别出逗号等于代码: <?php session_start(); //connection $conn = new mysqli('localhost', 'root', 'root', 'ca2solution'); if(isset($_POST['import'])){

大家好,我在我的项目中实现了一个scv文件导入器,其中相关数据位于mysql表中,我的问题是,如果数据以逗号分隔,则会进入不同的表中,就像我在加载数据时不使用逗号一样,但代码识别出逗号等于代码:

   <?php

    session_start();
    //connection
    $conn = new mysqli('localhost', 'root', 'root', 'ca2solution');

    if(isset($_POST['import'])){
        //check if input file is empty
        if(!empty($_FILES['file']['name'])){
            $filename = $_FILES['file']['tmp_name'];
            $fileinfo = pathinfo($_FILES['file']['name']);

            //check file extension
            if(strtolower($fileinfo['extension']) == 'csv'){
                //check if file contains data
                if($_FILES['file']['size'] > 0){

                    $file = fopen($filename, 'r');

                    while(($impData = fgetcsv($file, 1000, ',')) !== FALSE){
                        $sql = "INSERT INTO members (firstname, lastname, address) VALUES ('$impData[0]','$impData[1]','$impData[2]')";
                        rtrim($string, ",");

                        $query = $conn->query($sql);

                        if($query){
                            $_SESSION['message'] = "Data imported successfully";
                        }
                        else{
                            $_SESSION['message'] = "Cannot import data. Something went wrong";
                        }
                    }
                    header('location: index.php');

                }
                else{
                    $_SESSION['message'] = "File contains empty data";
                    header('location: index.php');
                }
            }
            else{
                $_SESSION['message'] = "Please upload CSV files only";
                header('location: index.php');
            }
        }
        else{
            $_SESSION['message'] = "File empty";
            header('location: index.php');
        }

    }

    else{
        $_SESSION['message'] = "Please import a file first";
        header('location: index.php');
    } 

?>
代码以一种限定值的方式对其进行处理​​要插入到数据库中,它现在不做的事情,因为它仅在以下情况下限制它们:

1a = firstname, lastname, gender

so only if they are separated by a comma

你只需要把绳子绕起来


rtrim($string,“,”)

不,不行!尝试在字符串之前显示一个示例&您是如何使用rtrim的..请向我们显示一个CSV文件的示例,以及一个您试图向我们解释的文件的示例,在itI中不使用逗号。我只是希望它出现在openoffice中的我的CSV文件中,该文件由单元格1a 1b 1c 1d等组成。。。我可以把它放在每个单元格中:1a=firstname 2a=lastname 3a=address,代码以一种限定值的方式处理它​​要插入到数据库中,它现在没有这样做,因为它仅在以下情况下对它们进行限制:1a=firstname、lastname、gender,因此仅在它们之间用逗号分隔时编辑我的问题以显示:rtrim($string,“,”);
1a = firstname, lastname, gender

so only if they are separated by a comma