Error in INSERT SQL php

3

When trying to make a query from php to a database, I get this error:

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 ' ' at line 1

I know it's something of SQL Injection , but I can not solve it. The php code is this:

<?php
    session_start();
    if($_SESSION["tipoUsuario"] != 'P'){
        header("Location:index.php");
    }
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["xmlDocument"]["name"]);
    $uploadOk = 1;
    $docFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    if ( $_FILES['xmlDocument']['error'] > UPLOAD_ERR_OK ){
          header("Location:upload.php");
          exit();
    }else{

        if($docFileType != "xml") {
            $uploadOk = 0;
            echo "Sólo está permitida la subida de archivos XML <br>";
        }

        if ($uploadOk == 0) {
        // if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["xmlDocument"]["tmp_name"], $target_file)) {
                echo "<br><br>El archivo ". basename( $_FILES["xmlDocument"]["name"]). " ha sido subido satisfactoriamente. Ahora se procederá a actualizar la BBDD con los datos del archivo.";

                /*

                PARA PERICO: EN ESTE MOMENTO EL ARCHIVO ESTÁ SUBIDO, AHORA PUEDES TRABAJAR CON ÉL. SE ENCUENTRA EN LA VARIABLE $target_file;
                TAL COMO ESTÁ, SE GUARDA UNA COPIA DE TODOS LOS ARCHIVOS SUBIDOS. SI YA EXISTE UNO CON ESE NOMBRE, SE SOBREESCRIBE.

                */
                $xml = simplexml_load_file($target_file);
                   include("conexion.php");
                $mysqli = abrirConexion();

                 $elemento = [
                     "ProductoID" => "",
                     "ProveedorID" => "",
                     "Nombre" => "",
                     "Precio" => "",
                     "Stock" => "",
                     "StockSeguridad" => "",
                     "Color" => "",
                     "Confirmacion" => "",
                     "DadoDeBaja" => "",
                 ];

                   foreach($xml->Producto as $producto){
                       $elemento["ProductoID"] =$producto->ProductoID;
                       $elemento["ProveedorID"] =$_SESSION["ID"];
                       $elemento["Nombre"] =$producto->Nombre;
                       $elemento["Precio"] =$producto->Precio;
                       $elemento["Stock"] =$producto->Stock;
                       $elemento["StockSeguridad"] =$producto->StockSeguridad;
                       $elemento["Color"] =$producto->Color;
                       $elemento["Confirmacion"] =$producto->Confirmacion;
                       $elemento["DadoDeBaja"] =$producto->DadoDeBaja;

                    $sentencia = "SELECT ProductoID FROM productos WHERE ProductoID =" .$elemento["ProductoID"];
                    $result = $mysqli->query($sentencia);

                       if ($result == NULL) { //insert
                           $sentenciaFin = 'INSERT INTO productos(ProveedorID, Nombre, Precio, Stock, StockSeguridad, Color, Confirmacion, DadoDeBaja) VALUES ('
                           . $elemento["ProveedorID"] . ","
                           . '"' .$elemento['Nombre'] . '",'
                           . $elemento['Precio'] . ','
                           . $elemento['Stock'] . ','
                           . $elemento['StockSeguridad'] . ','
                           . '"' .$elemento['Color'] . '",'
                           . '"' .$elemento['Confirmacion'] . '",'
                           . '"' .$elemento['DadoDeBaja'] .'");<br>';

                           if($mysqli->query($sentenciaFin) === TRUE){
                               echo "OLEEE";
                           } else {
                            echo "Error: " . $sentenciaFin . "<br>" . $mysqli->error;
                        }


                       }else{ //update

                       }
                   }

            } else {
                echo "Lo sentimos, no se ha podido subir el archivo.";
            }
        }
    }
?>

<!DOCTYPE HTML>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="estilos_pablo.css">
<html>
    <body>
        <div class="subido"><img src="https://freeodoo.com/website/image/product.template/21_7bd0018/image"></div>
        <a href="index.php" class="atras">Atrás</a>
    </body>
</html> 

The string obtained from executing this code is:

INSERT INTO productos(ProveedorID, Nombre, Precio, Stock, StockSeguridad, Color, Confirmacion, DadoDeBaja) VALUES (0000000004,"Coche5",250000,25,NULL,"Verde oliva","No","No");

However, if I copy the string that comes out of doing a echo and paste it in the console of PHPMyAdmin , the query is resolved correctly and I insert the data in the Database. The syntax is not bad, it's PHP error.

    
asked by Pablo González 02.05.2018 в 23:21
source

1 answer

5

The problem is that you have a <br> extra at the end of your INSERT .

    
answered by 02.05.2018 / 23:31
source