Error inserting records with PDO in php

1

Well I have the following error, that when I try to register data in the database I get the following error

  

Error: SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' ARTICLE CODE ',' SECTION ',' NAMEBREAK ',' PRICE ',' DATE ',' IMPORTED 'at line 1 On Line 36

It turns out that the error lies in Line 36 because of some syntax error problem in the SQL statement but I have been trying to find it for a while and I can not find anything, besides that if it were for the fields of the DB neither because the fields are:

The code is as follows:

    $c_articulo=$_POST["c_articulo"];
$seccion=$_POST["seccion"];
$n_art=$_POST["nombre_articulo"];
$precio=$_POST["precio"];
$fecha=$_POST["fecha"];
$importado=$_POST["importado"];
$p_origen=$_POST["p_origen"];

try { 
    $base=new PDO("mysql:host=localhost;dbname=pruebas", "root", "");

    $base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $base->exec("SET CHARACTER SET utf8"); 
    $sql= "INSERT INTO PRODUCTOS ('CÓDIGOARTÍCULO', 'SECCIÓN', 'NOMBREARTÍCULO', 'PRECIO', 'FECHA', 'IMPORTADO', 'PAÍSDEORIGEN') VALUES (:c_articulo, :seccion, :n_art, :precio, :fecha, :importado, :p_origen)";

    $resultado = $base->prepare($sql); 

    $resultado->execute(array(":c_articulo"=>$c_articulo, ":seccion"=>$seccion, ":n_art"=>$n_art, ":precio"=>$precio, ":fecha"=>$fecha,":importado"=>$importado, ":p_origen"=>$p_origen)); 

    echo "Hemos insertado el registro";
    $resultado->closeCursor();
}           
catch(Exception $e){ 
    die ("Error: " . $e->GetMessage() . " En la Linea " .  $e->getline());
}

Form code to insert the data:

<form action="pagina_busqueda_pdo.php" method="post" accept-charset="utf-8">
<table>     
    <tr><td><label>C.Artículo<input type="text" name="c_articulo" placeholder="Código Articulo"></label></td></tr>
    <tr><td><label>Seccion<input type="text" name="seccion" value=""></label></td></tr>
    <tr><td><label>Nombre Art<input type="text" name="nombre_articulo" value=""></label></td></tr>
    <tr><td><label>Precio<input type="text" name="precio" value=""></label></td></tr>
    <tr><td><label>Fecha<input type="text" name="fecha" value=""></label></td></tr>
    <tr><td><label>¿Importado?<input type="text" name="importado" value=""></label></td></tr>
    <tr><td><label>Pais de Origen<input type="text" name="p_origen" value=""></label></td></tr>
    <tr><td><input type="submit" name="enviar" value="Enviar"></td></tr>
</table>

    
asked by Victor Escalona 08.04.2018 в 06:19
source

2 answers

3

The first recommendation as I mentioned in my comment, avoiding the use of words with tilde and ñ for names of columns or tables can often cause error now or later.

Then there are concepts that may not be clear which may be causing syntax errors.

  • ' is used to attach chain literals, that is, to pass a string simply
  • 'append identifiers as table and column names

The solution to eliminate the single quotes for the columns in the table

$sql= "INSERT INTO PRODUCTOS (CODIGOARTICULO, SECCION, NOMBREARTICULO, PRECIO, 
      FECHA, IMPORTADO, PAISDEORIGEN)  VALUES 
      (:c_articulo, :seccion, :n_art, :precio, :fecha, :importado, :p_origen)";

or use the second option for both cases, I already take into account the modification of the columns and eliminate the tildes.

  $sql= "INSERT INTO PRODUCTOS ('CODIGOARTICULO', 'SECCION', 'NOMBREARTICULO', 'PRECIO', 
          'FECHA', 'IMPORTADO', 'PAISDEORIGEN')  VALUES 
          (:c_articulo, :seccion, :n_art, :precio, :fecha, :importado, :p_origen)";
    
answered by 08.04.2018 / 06:49
source
0

My recommendation is that you use placeholders in the SQL, and when you execute you pass an array with the variables that you receive by POST and do not use the quotes or accents in the names of the fields, like this:

$c_articulo=$_POST["c_articulo"];
$seccion=$_POST["seccion"];
$n_art=$_POST["nombre_articulo"];
$precio=$_POST["precio"];
$fecha=$_POST["fecha"];
$importado=$_POST["importado"];
$p_origen=$_POST["p_origen"];

try { 
  $base=new PDO("mysql:host=localhost;dbname=pruebas", "root", "");
  $base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $base->exec("SET CHARACTER SET utf8");

  $sql= "INSERT INTO PRODUCTOS (CODIGOARTÍCULO, SECCION, NOMBREARTICULO, PRECIO, FECHA, IMPORTADO, PAISDEORIGEN) 
                                VALUES (?,?,?,?,?,?,?)";

  $resultado = $base->prepare($sql); 

  $resultado->execute(array($c_articulo,$seccion,$n_art,$precio,$fecha,$importado,$p_origen)); 

 echo "Hemos insertado el registro";
 $resultado->closeCursor();
}           
 catch(Exception $e){ 
 die ("Error: " . $e->GetMessage() . " En la Linea " .  $e->getline());
}
    
answered by 08.04.2018 в 07:39