0

Hi guys I have implemented within my project an importer of scv files where the relative data go inside a mysql table, my question is the data go in the different tables if separated by the comma as I do instead to have them loaded without using the comma, but the code recognizes that the comma equals the code:

   <?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');
    } 

?>

I would like that in the csv file that is divided into rows and columns each parameter was written in a different cell and that this is recognized as such, not that to insert values ​​into my db I have to write everything in a cell separated by comma

I simply would like it in my csv file in openoffice which is made from cells 1a 1b 1c 1d etc ... i can put it for each cell:

1a = firstname
2a = lastname
3a = address

and that this is processed by the code in such a way as to delimit the values ​​to be inserted in the database, something that it does not do now because it limits them only if:

1a = firstname, lastname, gender

so only if they are separated by a comma
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • nope, not work ! –  Sep 02 '18 at 16:16
  • Try showing an example before string & how you're using rtrim.. – thecoolestguyever123 Sep 02 '18 at 16:32
  • Please show us an example of the CSV file and ALSO an example of the file you are trying to explain to us without commas in it – RiggsFolly Sep 02 '18 at 16:35
  • I simply would like it in my csv file in openoffice which is made from cells 1a 1b 1c 1d etc ... i can put it for each cell: 1a = firstname 2a = lastname 3a = address and that this is processed by the code in such a way as to delimit the values ​​to be inserted in the database, something that it does not do now because it limits them only if: 1a = firstname, lastname, gender so only if they are separated by a comma –  Sep 02 '18 at 16:42
  • i edit my question for show : rtrim($string, ","); –  Sep 02 '18 at 16:43

1 Answers1

0

You just need to rtrim the string.

rtrim($string, ",");