It gives me an error when inserting some values in the database. I've been looking for a while now and I can't find 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
one found 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 DB, is in events.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 it 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/events.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
The problem is how you generate the insert statement: you are missing a closing parenthesis at the end of the field list . Right now you have (I split it into different lines to make it look good):
You just have to close the parentheses after the field
hora
:I think it may be because you don't enter in the VALUES fields, the texts enclosed in quotes, the numbers you don't need.
It also checks the date format in the database, the slightest mismatch will cause it to fail.
If you could copy the code instead of the screenshots it would make it easier for me to read your code. =D
All the best.