How can I update data from a multiple input select in mysql

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 19.03.2017 в 15:12
source

1 answer

1

The first thing you have to do is to receive the contents of the select and treat it in your PHP code as an arrangement, that is to say something like the following:

<form action="edit_student.php" method="post">
  <select name="selectC[]" multiple="multiple">
    <option value="7" >Photoshop</option>
    <option value="8">Cinema 3D</option>
    <option value="9">iDesign</option>
    <option value="10">Illustrator</option>
  </select>
  <input type="submit">
</form>

In your PHP:

<?php
  //primero tendrias que borrar los cursos que actualmente tenia el alumno
  $statment = $connect->prepare('DELETE FROM students_courses WHERE student_id = :student_id');
  $statment->bindParam(':student_id',$student_id);
  //Una vez que ya los borraste, agregarías los cursos que se enviaron.
  $values = $_POST['selectC'];
  foreach ($values as $course_id){
    $statment = $connect->prepare( 'INSERT INTO students_courses (course_id,student_id) VALUES (:course_id,:student_id) ;
    $statment->bindParam(':course_id',$course_id);
    $statment->bindParam(':student_id',$student_id);
    $statment->execute();
  }
?>
    
answered by 19.03.2017 / 16:46
source