Problems with insert rows

Discussion in 'PHP' started by piropeator, May 12, 2016.

  1. #1
    Hi everyone. I have a csv file and I try to insert in my table (codigo, stud).
        if(file_exists($ruta.$nombre_archivo.'.'."csv")) {
            $registro = fopen($ruta.$nombre_archivo.'.'."csv", "r");
            $BD = new ConectionDB();
            while (($data = fgetcsv($registro, 200, ",")) !== FALSE) {
                $sth = "INSERT into SC_TEMP (cod, nom) values ('$data[0]', '$data[1]')";
                $recordSet = $BD->prepare($sth);
                $recordSet->bindParam('data[0]', $cod);
                $recordSet->bindParam('data[1]', $nom);
                $recordSet->execute();
            }
    }
    PHP:
    My csv has this rows:
    11111,BRIAN
    11252,JOSEPH
    52362,MC'DONALD

    In the third rows the code is truncate. What is the error?
     
    Solved! View solution.
    piropeator, May 12, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You're doing it wrong. Here, try this:
    
    if (file_exists($ruta.$nombre_archivo.'.'."csv")) {
      $registro = fopen($ruta.$nombre_archivo.'.'."csv", "r");
      $BD = new ConectionDB();
      while (($data = fgetcsv($registro, 200, ",")) !== FALSE) {
      $sth = $BD->prepare("INSERT INTO SC_TEMP (cod, nom) VALUES (:cod, :nom)"); //you should really not have a all-caps table name
      $sth->execute(array(':cod'=>$data[0],':nom'=>$data[1]));
      }
    }
    
    PHP:
     
    PoPSiCLe, May 12, 2016 IP
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    I get this message:
    ( ! ) Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in Controller.php on line xx
    ( ! ) PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in Controller.php on line xx

    (line xx is $sth->execute(array(':cod'=>$data[0],':nom'=>$data[1]));)

    Just in case, this is mi ConectionDB.php
    class ConexionDB extends PDO {
        public function __construct () {
            try {
                    parent:: __construct('mysql:host='.DB_HOST.';dbname='.DB_DATA.';charset=utf8', DB_USER, DB_KEY);
                    parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                } catch (Exception $ex) {
                        die ('Database is not exist');
            }
        }
        function __destruct(){
        }
    } 
    PHP:
    :(


     
    piropeator, May 12, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Works fine here. However, you're using Conection in the PHP file, and Conexion in the class (which shouldn't work at all).
    But yeah, it works here. So I'm guessing you're doing something else than just testing the code I gave you. Here's the code I'm currently testing (I made a test DB so I could test the script):
    
    <?php
    
    class ConexionDB extends PDO {
      public function __construct () {
      try {
      parent:: __construct('mysql:host=localhost;dbname=cntrl;charset=utf8', 'root','root');
      parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      } catch (Exception $ex) {
      die ('Database is not exist');
      }
      }
      function __destruct(){
      }
    }
    
    if (file_exists('test.csv')) {
      echo 'file exists';
      $registro = fopen('test.csv', "r");
      $BD = new ConexionDB();
      while (($data = fgetcsv($registro, 200, ",")) !== FALSE) {
      $sth = $BD->prepare("INSERT INTO SC_TEMP (cod, nom) VALUES (:cod, :nom)"); //you should really not have a all-caps table name
      $sth->execute(array(':cod'=>$data[0],':nom'=>$data[1]));
      }
    } else {
      echo 'nope';
    }
    
    ?>
    
    PHP:
     
    PoPSiCLe, May 12, 2016 IP
  5. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #5
    Great!!!! Thank you very much.
     
    Last edited: May 12, 2016
    piropeator, May 12, 2016 IP
  6. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #6
    I have a one question more about this topic. In case in my csv file there are ñ or Ñ ?? Why this code doesn't work?
     
    piropeator, May 12, 2016 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Doesn't work - as in...? The character isn't stored correctly?
     
    PoPSiCLe, May 12, 2016 IP
  8. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #8
    For example:
    In csv file:
    123456,garraña
    345678,iñaquita

    In the table (DB)
    123456,garra
    345678,i

    Maybe need utf-8 in the insert?
     
    piropeator, May 13, 2016 IP
  9. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #9
    Using this is work:
    <?php
    
    class ConexionDB extends PDO {
      public function __construct () {
      try {
      parent:: __construct('mysql:host=localhost;dbname=xprueba;charset=utf8', 'root','tecsup');
      parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      } catch (Exception $ex) {
      die ('Database is not exist');
      }
      }
      function __destruct(){
      }
    }
    
      $BD = new ConexionDB();
      $sth = $BD->prepare("INSERT INTO xxx (codigo, stud) VALUES (:codigo, :stud)");
    
      $codigo = "666555";
      $stud   = "mac'pato";
      $sth->bindParam(':codigo', $codigo);
      $sth->bindParam(':stud', $stud);
      $sth->execute();
    
      $codigo = "777888";
      $stud   = "Array ñÑáéíóú";
      $sth->execute(array(':codigo'=>$codigo, ':stud'=>$stud))
    
    ?>
    PHP:
    But, using this, the letters ñ, Ñ and á,é,í,ó,ú doesn't work.
    <?php
    
    class ConexionDB extends PDO {
      public function __construct () {
      try {
      parent:: __construct('mysql:host=localhost;dbname=xprueba;charset=utf8', 'root','tecsup');
      parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      } catch (Exception $ex) {
      die ('Database is not exist');
      }
      }
      function __destruct(){
      }
    }
    
      $BD = new ConexionDB();
        if(file_exists('prueba_caracteres.csv')) {
            $registro = fopen('prueba_caracteres.csv', "r");
            $BD = new ConexionDB();
            while (($data = fgetcsv($registro, 200, ",")) !== FALSE) {
                $sth = $BD->prepare("INSERT INTO xxx (codigo, stud) VALUES (:codigo, :stud)");
                $sth->execute(array(':codigo'=>$data[0], ':stud'=>$data[1]));
            }
        }
    ?>
    PHP:
    What do you think is the problem?
     
    piropeator, May 13, 2016 IP
  10. #10
    Probably something wrong with the input data - ie the CSV-file not being saved with UTF-8 - I tested here, copying and pasting the first two entries you posted for the CSV, into the CSV, and ran the script, and it works just fine - hence I would assume that somewhere in the chain there is a file saved with another character encoding.
     
    PoPSiCLe, May 13, 2016 IP
  11. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #11
    You are right, I have checked the csv file. The source of that csv file is a excel, the character encoding is Unicode. That is the mistake. Now I have the corrects data.
    Thank you very much.
     
    piropeator, May 14, 2016 IP