Greetings community. I am developing an application using PDO, I am starting and I have had a problem when inserting records in the database. Next I put the functions that I am using and the error that is giving me.
First my controller receives the data in the following way:
public function agregar(){
if(!$_POST){
$datos = $this->seccion->listar();
return $datos;
}else{
$permitidos = array("image/jpeg", "image/png", "image/gif", "image/jpg"); // valido los formatos de imagen permitidos
$limite = 700;
if(in_array($_FILES['imagen']['type'], $permitidos) && $_FILES['imagen']['size'] <= $limite * 1024){ //valido si el formato y el peso son aceptados
$nombre = $_FILES['imagen']['name'] . date('is'); //creo el nombre de la imagen
$ruta = "Views" . DS . "templates" . DS . "imagenes" . DS . $nombre; //asigno la ruta de la imagen
move_uploaded_file($_FILES['imagen']['tmp_name'], $ruta);
$this->estudiante->set("nombre" , $_POST['nombre']);
$this->estudiante->set("edad" , $_POST['edad']);
$this->estudiante->set("promedio" , $_POST['promedio']);
$this->estudiante->set("imagen" , $nombre);
$this->estudiante->set("id_seccion" , $_POST['id_seccion']);
$this->estudiante->add();
echo error_reporting();
}
}
}
Then that data is received by the set () function that is in my models
public function set($atributo, $contenido){
$this->$atributo = $contenido;
}
Which passes that data to my add () function in the model
public function add(){
$sql = "INSERT INTO estudiantes(nombres, edad, promedio, img, id_seccion)
VALUES(:nombre, :edad, :promedio, :imagen, :id_seccion)";
$params = [':nombre' => $this->nombre];
$params = [':edad' => $this->edad];
$params = [':promedio' => $this->promedio];
$params = [':imagen' => $this->imagen];
$params = [':id_seccion' => $this->id_seccion];
$this->con->consultaSimple($sql, $params);
}
Finally, I execute the query with this function
public function consultaSimple($sql, $binds = []){
$datos = $this->con->prepare($sql);
foreach ($binds as $key => $val) {
$datos->bindParam($key, $val);
}
$datos->execute();
}
Those are all the functions that I use to insert, the error that is generating me is the following:
Warning: PDOStatement :: execute (): SQLSTATE [HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C: \ xampp \ htdocs \ pdo \ Models \ Connection.php on line 25 22527
I clarify that the line where the error is thrown is in the function SimpleSpeed in the execute () The truth is that I have not been able to solve this error and I have given it many times already and I have not been successful.