Insert values in database using php

6

It gives me an error when inserting some values in the database. I've been watching for a while now and I'm not right with the error. This is my code:

HTML form:

<form action="#" method="post">
                            <fieldset class="fieldset">
                                <legend>Introduce los datos de tu evento:</legend>
                                <input class="inputForm1" type="text" name="nombre" placeholder="Nombre del Evento..." required>
                                <input class="inputForm1" type="text" name="lugar" placeholder="Lugar..." required>
                                <input class="inputForm1" type="text" name="descripcion" placeholder="Descripción..." required>
                                <input class="inputForm1" type="number" name="precio" placeholder="Precio..." required>
                                <input class="inputForm1" type="number" name="plazas" placeholder="Plazas..." required>
                                <input class="inputForm1" type="date" name="fecha" placeholder="Fecha..." required>
                                <input class="inputForm1" type="time" name="hora" placeholder="Hora..." required>
                                <input type="submit" value="Crear Evento" class="btnForm"/> 
                            </fieldset>
                        </form>

PHP code that receives the form that is in the same file:

 require '../php/eventos.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

     if (isset($_POST['nombre'])) {

        // Obtener parámetro idalumno
        $nombre = $_POST['nombre'];
        $lugar = $_POST['lugar'];
        $descripcion = $_POST['descripcion'];
        $precio = $_POST['precio'];
        $plazas = $_POST['plazas'];
        $fecha = $_POST['fecha'];
        $hora = $_POST['hora'];
        //$idUsuario = $_POST['idUsuario'];
         
         function cambiaf_a_mysql($fecha){
            ereg( "([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})", $fecha, $mifecha);
            $lafecha=$mifecha[3]."-".$mifecha[2]."-".$mifecha[1];
            return $lafecha;
        }
    


        // Tratar retorno
        $retorno = Eventos::insertWeb($nombre,$lugar,$descripcion,$precio,$plazas,$fecha,$hora);


        if ($retorno) {

            //$alumno["estado"] = 1;        // cambio "1" a 1 porque no coge bien la cadena.
           // $alumno["alumno"] = $retorno;
            // Enviar objeto json del alumno
            //print json_encode($retorno);
            header('Location: actividades.php');
        } else {
            // Enviar respuesta de error general
            print json_encode(
                array(
                    'estado' => '2',
                    'mensaje' => 'No se obtuvo el registro'
                )
            );
        }

    } else {
        // Enviar respuesta de error
        print json_encode(
            array(
                'estado' => '3',
                'mensaje' => 'Se necesita un identificador'
            )
        );
    }
}

PHP code that inserts in the BD, is in eventos.php:

public static function insertWeb($nombre,$lugar,$descripcion,$precio,$plazas,$fecha,$hora) {
        // Sentencia INSERT
        $comando = "INSERT INTO evento ( " ."nombre," ."lugar,"  ."descripcion," ."precio," ."plazas," ."fecha," ."hora" . " VALUES ( ?,?,?,?,?,?,?)";

        // Preparar la sentencia
        $sentencia = Database::getInstance()->getDb()->prepare($comando);

        return $sentencia->execute(array($nombre,$lugar,$descripcion,$precio,$plazas,$fecha,$hora));

    }

And the error that shows:

  

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'VALUES (' a ',' a ',' a ',' 1 ',' 1 ',' 2016-06-09 ',' 00 : 00 ')' at line 1 'in ... / php / events.php: 161 Stack trace: # 0 /home/u695092214/public_html/php/eventos.php(161): PDOStatement-> execute (Array) # 1 ... / views / createEvent.php (150): Events :: insertWeb ('a', 'a', 'a', '1', '1', '2016-06-09', '00 : 00 ') # 2 {main} thrown in ... / php / events.php on line 161

    
asked by tripossi 06.06.2016 в 13:54
source

2 answers

3

The problem is how you generate the insertion statement: you are missing a parentheses closing at the end of the field list . Right now you have (I divide it into different lines to make it look good):

$comando = "INSERT INTO evento ( " .
                "nombre," ."lugar,"  ."descripcion," ."precio," .
                "plazas," ."fecha," ."hora" . 
           " VALUES ( ?,?,?,?,?,?,?)";

You just have to close the parentheses after the hora field:

$comando = "INSERT INTO evento ( " .
                "nombre," ."lugar,"  ."descripcion," ."precio," .
                "plazas," ."fecha," ."hora   )" . 
           " VALUES ( ?,?,?,?,?,?,?)";
    
answered by 06.06.2016 / 14:30
source
0

I think it may be because you do not enter the VALUES fields, the texts enclosed in quotes, the numbers you do not need.

It also checks the format of the date in the database, the minimum error of coincidence will make it fail.

If you could copy the code instead of the screenshots, you would make it easier for me to read your code. = D

Greetings.

    
answered by 06.06.2016 в 13:59