Data not inserted with query prepared mysql-php

2

I am making a loan system in which a teacher will request a loan, which will record his identification number, the plate or serial number of the item he requested, the current date and the administrator who lent him the item at that moment, who it will be the user who has the session active.

The problem I have is when sending data, the browser does not give me any errors and when looking at the table in phpMyAdmin the data is not inserted into the database.

This would be the form that would receive the data and send it to processPrestamo.php The values of $ Admin Y $ Instructor are of numeric type and $ element are of type String and $ date type date although I am inserting it as a string because the prepared query only allows me to specify with "i" for integer "s" for string, and "d" for double, in the database, If I have it specified as a date type, I do not know if there is a problem with the data.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title></title>
	<link rel="stylesheet" href="">
</head>
<body>
	<form action="procesarPrestamo.php" method="post" accept-charset="utf-8">
		Elemento: <input type="text" name="elemento"/>
		Cedula:   <input type="text" name="cedula"/>	
		<input type="submit" name="enviar" value="Enviar"/>
	</form>
</body>
</html>
<?php 
include_once 'conexion.php';

session_start();    

date_default_timezone_set('America/Bogota');

$Admin = $_SESSION['user'];
$Instructor = $_POST['cedula'];
$Elemento = $_POST['elemento'];
$Fecha = date("d/m/Y");    

if ($stmt = $mysqli->prepare("INSERT INTO prestamo VALUES(?,?,?,?)") ){

    $stmt->bind_param("iiss",$Admin,$Instructor,$Elemento,$Fecha);     
    $stmt->execute();    
    $stmt->close(); 

} else{
   echo "Error al ejecutar la sentencia preparada".$mysqli->error;
}  
$mysqli = new Conexion();  
?>
    
asked by Andres David Rodriguez 06.03.2017 в 21:06
source

3 answers

0

In my opinion you can get to verify it in the following way, I do not know much about msqli since I like more PDO but try the following and show us the error message in which case this.

 $stmt = $mysqli->prepare("INSERT INTO prestamo (admin,instructor,elemento,fecha) VALUES (?,?,?,?)");

    $stmt->bind_param("iiss",$Admin,$Instructor,$Elemento,$Fecha);

    if ($stmt->execute()){  
       echo "Se ejecuto la sentencia ";
     } else{
       echo "Error al ejecutar la sentencia preparada".$mysqli->error;
     }

     $stmt->close(); 

With regard to the error may be due to the format of the date, in some cases may give problems with the parameterization of this.

As a suggestion:

I would recommend that you go to PDO link

    
answered by 07.03.2017 в 06:50
0

The error is that you forgot to select your columns in your table , you have only added their values.

INSERT INTO prestamo (faltan tus columnas)  VALUES (?,?,?,?)")
                      ^^^^^^^^^^^^^^^^^^

Updated example:

session_start();    

include_once 'conexion.php';                            

date_default_timezone_set('America/Bogota');

$Admin = $_SESSION['user'];
$Instructor = $_POST['cedula'];
$Elemento = $_POST['elemento'];
$Fecha = date("d/m/Y");    

$stmt = $mysqli->prepare("INSERT INTO prestamo (ccAdmin,ccInstructor,idElemento,fechaPrestamo) VALUES (?,?,?,?)");  
if (false===$stmt) {        
    exit('Sentencia prepare() fallo: ' . htmlspecialchars($stmt->error));
}            

$rc = $stmt->bind_param("iiss",$Admin,$Instructor,$Elemento,$Fecha);
if (false===$rc) {      
    exit('bind_param() fallo: ' . htmlspecialchars($stmt->error));
}


$rc = $stmt->execute();
if (false===$rc) {
    exit('execute() fallo: ' . htmlspecialchars($stmt->error));
}
//Cerramos sentencia
$stmt->close(); 

conexion.php

<?php
$mysqli = new mysqli("localhost", "root", "", "centrologistico");

/* verificar conexión */
if (mysqli_connect_errno()) {
    printf("Conexión fallo: %s\n", mysqli_connect_error());
    exit();
}

//UTF-8.
if (!$mysqli->set_charset("utf8")) {
    printf("Error cargando el conjunto de caracteres utf8: %s\n", $mysqli->error);
    exit();
    }
?>
    
answered by 06.03.2017 в 21:26
0

By removing the syntax corrections that have already been made, I would like to supplement a bit with my experience, in case someone is in the same situation.

I gave a similar error (did not insert and did not throw errors), and to stop passing the value of the time (in my case were 3 fields and one was the time), it worked correctly.

Thanks to a comment here, I checked that the error was because I was taking that data with the function time() of PHP, and then replace it with date("G:H:s") and yes inserted correctly.

    
answered by 28.05.2017 в 23:07