How can I update data from a multiple input select

1

I have a problem (I do not know how to do an UPDATE) when it comes to updating the student's file since it only changes the name and image of the student, and what I want to do is add or remove the courses in which it is enrolled from a multiple select.

This is a capture of the edit file:

link

Table Students

---------------------------------------------
- student_id - student_name - student_image -
---------------------------------------------
-          1 - Ana          - avatar.jpg    -
-          2 - David        - avatar.jpg    -
-          3 - Jasmine      - avatar.jpg    -
---------------------------------------------

Table Courses

-------------------------------------------
- course_id - course_title - course_image -
-------------------------------------------
-         7 - Photoshop    - image.jpg    -
-         8 - Cinema 4D    - image.jpg    -
-         9 - Idesign      - image.jpg    -
-        10 - Illustrator  - image.jpg    -
-------------------------------------------

Table Students_Courses

----------------------------------------------
students_courses_id - course_id - student_id - 
----------------------------------------------
-         1         -      7    -       1   
-         2         -      8    -       2   
-         3         -      9    -       2   
-         4         -      10   -       3   
-         5         -      8    -       1   
----------------------------------------------

My code

$connect = connect($database);
if(!$connect){
    header ('Location: ' . SITE_URL . '/controller/error.php');
    }

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

$course_id = $_POST['course_id'];

$student_id = cleardata($_POST['student_id']);
$student_name = cleardata($_POST['student_name']);
$student_image_save = $_POST['student_image_save'];
$student_image = $_FILES['student_image'];

if (empty($student_image['name'])) {
    $student_image = $student_image_save;
} else{
    $student_image_upload = '../' . $items_config['images_folder'] . $_FILES['student_image']['name'];
    move_uploaded_file($_FILES['student_image']['tmp_name'], $student_image_upload);
    $student_image = $_FILES['student_image']['name'];
}

$ statment = $ connect-> prepare (     'UPDATE students SET student_name =: student_name, student_image =: student_image WHERE student_id =: student_id'     ); $ statment-> execute (array (         ': student_name' = > $ student_name,         ': student_image' = > $ student_image,         ': student_id' = > $ student_id         )); $ statment = $ connect-> prepare ('UPDATE students_courses SET course_id =: course_id WHERE student_id ='. $ student_id); $ statment-> execute ();
    
asked by anonfidusa 18.03.2017 в 23:36
source

1 answer

0

The first thing you have to do is delete all the courses that the student has with your id, your database will be empty, and then you will execute an insert of all the courses that you have registered, this can be done by sending a post with the data of the course id in a string separated by spaces or commas and with the explode method you take the array and run it with a foreach to insert as a guide I leave this pseudocode:

//Aqui tu consulta para borrar todos los cursos del alumno

//Usas el explode para que te de un array
$cursos = "curso1,curso2,curso3";
$lista_cursos = explode(",", $cursos);

    foreach ($lista_cursos as $idcurso) {
    //aqui preparas tu consulta para insertar por un curso a la vez
        $statment = $connect->prepare( 'INSERT') ;
        $statment->execute();
    }
    
answered by 31.03.2017 в 09:38