I am developing a CRUD using the PDO extension, I just need to debug the following error that appears in the MODIFY part, I made several tests and found that when I presented the user with the modified form the input The ID must be hidden because we use it as a reference more than anything, right? Well, if I do not fill it manually (which is not the duty to be) and leave it hidden, I skip the error mentioned which is this:
Fatal error: Uncaught PDOException: SQLSTATE [22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "" in /var/www/html/Modules/Fermary/modelo/update-triunfador-modelo.php : 148 Stack trace: # 0 /var/www/html/Modulos/Fermary/modelo/actualizar-triunfador-modelo.php(148): PDOStatement-> execute () # 1 / var / www / html / Modules / Fermary /controller/form-actualize-trunner-controller.php(21): UpdateTrunner-> ModifyTrunner () # 2 {main} thrown in /var/www/html/Modulos/Fermary/modelo/actualizar-triunfador-modelo.php online 148
I am working with the MVC pattern I will leave you here pieces of code to see if we can solve this, anyway I will continue working and if I find the solution I share it with you:
Model (here I show the set method, the get method with which I print a record according to the id in the placeholder of the input that I will show later):
public function setIdT($id){
$this->id_triun = $id;
}
public function getIdT(){
$PDOStatement = Conexion::query("SELECT id_triun FROM triunfador WHERE id_triun = {$this->id_triun}");
$PDOStatement -> execute();
$idTriunfador = $PDOStatement -> fetchAll(PDO::FETCH_ASSOC);
return $idTriunfador;
}
public function ModificarTriunfador(){
$consultaSQL = 'UPDATE triunfador SET nombre_triun = :nombreT,apellido_triun = :apellidoT,cedula_triun = :cedulaT,seccion_triun = :seccionT,trayecto_triun = :trayectoT,condicion_triun = :condicionT WHERE id_triun = :idT';
$PDOStatement = Conexion::prepare($consultaSQL);
$PDOStatement -> bindParam(':idT', $this->id_triun, PDO::PARAM_INT);
$PDOStatement -> bindParam(':nombreT',$this->nombre_triun,PDO::PARAM_STR);
$PDOStatement -> bindParam(':apellidoT',$this->apellido_triun,PDO::PARAM_STR);
$PDOStatement -> bindParam(':cedulaT',$this->cedula_triun,PDO::PARAM_INT);
$PDOStatement -> bindParam(':seccionT',$this->seccion_triun,PDO::PARAM_STR);
$PDOStatement -> bindParam(':trayectoT',$this->trayecto_triun,PDO::PARAM_INT);
$PDOStatement -> bindParam(':condicionT',$this->condicion_triun,PDO::PARAM_STR);
$PDOStatement -> execute();
}
Controller:
<?php
require_once '../modelo/actualizar-triunfador-modelo.php';
$id = $_POST["Id"];
$nombre_triun = $_POST["Nombre"];
$apellido_triun = $_POST["Apellido"];
$cedula_triun = $_POST["Cedula"];
$seccion_triun = $_POST["Seccion"];
$trayecto_triun = $_POST["Trayecto"];
$condicion_triun = $_POST["Condicion"];
$obj = new ActualizarTriunfador();
$obj -> setIdT($id);
$obj -> setNombreT($nombre_triun);
$obj -> setApellidoT($apellido_triun);
$obj -> setCedulaT($cedula_triun);
$obj -> setTrayectoT($trayecto_triun);
$obj -> setSeccionT($seccion_triun);
$obj -> setCondicionT($condicion_triun);
$obj -> ModificarTriunfador();
header('location:../controlador/gestionar-triunfador-controlador.php');
? >
View (Here I have the input that I mentioned):
<div class="form-group bounceInLeft animated">
<label for="Id" class="Nombre hidden">ID</label>
<input type="text" name="Id" placeholder="<?php foreach($Id as $Triunfador){echo intval($Triunfador['id_triun']);} ?>" class="center-block form-control" >
</div>