Error in expression

Discussion in 'MySQL' started by piropeator, Nov 19, 2015.

  1. #1
    Some body tell me why does not work this expression?
    $sql = "INSERT into tabla(campo1, campo2, campo3, campo4) values('$data[0]','$data[1]', "","")";  
    PHP:
    Thanks.
     
    Solved! View solution.
    piropeator, Nov 19, 2015 IP
  2. gunslingor

    gunslingor Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    typo: 'tabla', needs to be the actual table name not a misspelled keyword I suspect. If that's not it, try adding the database name dot firt (i.e. [mydb].tabla)
    Example: INSERT INTO tbl_group_users (user_name, group_name, group_member, group_admin) VALUES ('blah', 'blah', 'blah', true)
     
    gunslingor, Nov 20, 2015 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Also, if you're using a sane DB handler, and showing/logging errors, you'll be able to see exactly what the error is, and fix it.
     
    PoPSiCLe, Nov 21, 2015 IP
  4. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #4
    This is the error message:
    this value is in data[1].
    I need to escape values but how?
    <?php
    //conexiones, conexiones everywhere
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    $db_host = 'localhost';
    $db_user = 'root';
    $db_pass = '';
    $database = 'eaton';
    $table = 'order';
    if (!mysql_connect($db_host, $db_user, $db_pass))
        die("No se pudo establecer conexión a la base de datos");
    if (!mysql_select_db($database))
        die("base de datos no existe");
        if(isset($_POST['submit']))
        {
            //Aquí es donde seleccionamos nuestro csv
             $fname = $_FILES['sel_file']['name'];
             echo 'Cargando nombre del archivo: '.$fname.' ';
             $chk_ext = explode(".",$fname);
            
             if(strtolower(end($chk_ext)) == "csv")
             {
                 //si es correcto, entonces damos permisos de lectura para subir
                 $filename = $_FILES['sel_file']['tmp_name'];
                 $handle = fopen($filename, "r");
           
                 while (($data = fgetcsv($handle, 100000, ",")) !== FALSE)
                 {
                   //Insertamos los datos con los valores...
                    $sql = "INSERT into table (field1, field2, field3, field4) values('$data[0]','$data[1]','$data[2]','$data[3]')";
                    mysql_query($sql) or die(mysql_error());
                 }
                 //cerramos la lectura del archivo "abrir archivo" con un "cerrar archivo"
                 fclose($handle);
                 echo "Importación exitosa!";            
             }
             else
             {
                //si aparece esto es posible que el archivo no tenga el formato adecuado, inclusive cuando es cvs, revisarlo para             //ver si esta separado por " , "
                 echo "Archivo invalido!";
             }  
        }
        
        ?>
    PHP:
    This code is for upload csv file and insert into database.
     
    piropeator, Nov 21, 2015 IP
  5. #5
    The immediate problem is that you haven't put curly brackets around the arrays
    so
    $sql = "INSERT into table (field1, field2, field3, field4) values('$data[0]','$data[1]','$data[2]','$data[3]')";
    Code (markup):
    will break
    but this will work
    $sql = "INSERT into actualtablename (field1, field2, field3, field4) values('{$data[0]}','{$data[1]}','{$data[2]}','{$data[3]}')";
    Code (markup):
    and this is even better
    $sql = "INSERT into `actualtablename` (`field1`, `field2`, `field3`, `field4`) values('{$data[0]}','{$data[1]}','{$data[2]}','{$data[3]}')";
    Code (markup):
     
    sarahk, Nov 21, 2015 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    And none of them are good. You DO NOT USE mysql_ EVER. If you'd used mysqli_ eller PDO, a prepared statement would've fixed this error before it even was an issue.
     
    PoPSiCLe, Nov 22, 2015 IP