I have my application in php with the model view controller design pattern but I don't know how to make a multiple insertion of several data.
When adding a single subject it is saved in the database but when I select some subjects it no longer saves me. Please help:
VIEW
This is the visual part which appears in the image where the teacher and all the subjects in the database are shown:
<label for="agregarAsignacionMateria">Nombre de la Materia</label>
<select multiple="multiple"
class="multiselect-dropdown form-control"
id="agregarAsignacionMateria"
name="agregarAsignacionMateria">
<?php
$item = null;
$valor = null;
$materias = ControladorAsignatura::ctrMostrarAsignatura($item, $valor);
foreach ($materias as $key => $value) {
echo '<option value="' . $value['ID_Asignatura'] . '">' . $value['Nombre_Asignatura'] . '</option>';
}
?>
</select>
Controller
Here I call the name of the input to later send it to the model and have it execute the respective statement:
public static function ctrCrearAsignacionMateria()
{
if (isset($_POST['agregarAsignacionMateriaDocente'])) {
$tabla = 'asignatura_docente';
$datos = array('asignatura' => $_POST['agregarAsignacionMateria'],
'docente' => $_POST['agregarAsignacionMateriaDocente']);
$respuesta = ModeloAsignacionMateria::mdlCrearAsignacionMateria($tabla, $datos);
if ($respuesta == 'ok') {
echo "<script>
Swal.fire({
type: 'success',
title: 'La materia ha sido creada correctamente',
confirmButtonText: 'Cerrar',
confirmButtonColor: '#3F6AD8',
allowOutsideClick: false,
allowEscapeKey: false
}).then((result)=>{
if (result.value){
window.location = 'asignacion_materias';
}
});
</script>";
} else {
echo "<script>
Swal.fire({
type: 'error',
title: 'Error en la base de datos, vuelva a intentarlo en un momento',
confirmButtonText: 'Cerrar',
confirmButtonColor: '#3F6AD8',
allowOutsideClick: false,
allowEscapeKey: false
}).then((result)=>{
if (result.value){
window.location = 'asignacion_materias';
}
});
</script>";
}
}
}
Model
Here in theory I create the sql sentence but if in the input I select a single subject it is saved but when selecting more subjects it is not saved.
public static function mdlCrearAsignacionMateria($tabla, $datos)
{
$stmt = Conexion::conectar()->prepare("INSERT INTO $tabla (ID_Asignatura, ID_Docente) VALUES (:asignatura, :docente)");
$stmt->bindParam(':asignatura', $datos['asignatura'], PDO::PARAM_INT);
$stmt->bindParam(':docente', $datos['docente'], PDO::PARAM_INT);
if ($stmt->execute()) {
return 'ok';
}
return 'error';
}
DATABASE
Hello, I am not sure how you are sending your data from the view to the controller since you do not put the complete form but I imagine that it must be that in your model you are only executing an insert so when you only select a subject it inserts it, but they are several is already an array and the value of the parameter :subject is an array and your insert expects a number so it should not throw an exception, what you should do is go through that array and execute as many inserts as the subjects. more or less as follows
It depends on the way you are sending your data since according to that the structure of $data['subject'] can vary in this case it is considered to be an array of integers.
On the other hand I recommend you to use transactions.
I hope it is what you are looking for, if not you can write to me to help you solve your doubts.
, Cheers.