lundi 30 mars 2020

How to insert into one-to-many relationship in MySQL using PHP form?

I want to add more than one student to a specific department (in my case I chose software engineering department, id = 1)

The "stage" must also be chosen for each student.

My SQL tables:

students                             departments                         stages
==================                ==================                ================              
id_student(PK)                        id_dep(PK)                      id_stage(PK)
student_name                       department_name                    stage_number
student_lname
student_mail
student_phone
student_user
student_pass
student_inreg_date
id_dep(FK)
id_stage(FK)

And the HTML FORM:

<form action="add_student.php" method="POST">
   <div class="form-row">
      <div class="form-group col-md-6">
         <label for="inputName">Name</label>
         <input type="text" class="form-control" name="std_name">
      </div>
      <div class="form-group col-md-6">
         <label for="inputLname">Last Name</label>
         <input type="text" class="form-control" name="std_lname">
      </div>
   </div>
   <div class="form-row">
      <div class="form-group col-md-6">
         <label for="inputDepartment">Department</label>
         <input type="text" class="form-control" name="department" value="Software engineering" readonly>
      </div>
      <div class="form-group col-md-6">
         <label for="inputStage">Stage</label>
         <select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="std_stage">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
         </select>
      </div>
   </div>
   <div class="form-row">
      <div class="form-group col-md-6">
         <label for="inputEmail">E-mail</label>
         <input type="email" class="form-control" name="std_email">
      </div>
      <div class="form-group col-md-6">
         <label for="inputPhone">Phone Nr.</label>
         <input type="tel" class="form-control" name="std_phone">
      </div>
   </div>
   <div class="form-row">
      <div class="form-group col-md-6">
         <label for="inputUser">Username</label>
         <input type="text" class="form-control" name="std_user">
      </div>
      <div class="form-group col-md-6">
         <label for="inputPassword">Password</label>
         <input type="password" id="password" class="form-control" name="std_pass">
         <input type="checkbox" onclick="myFunction()">Show Password
      </div>
   </div>
   <button type="submit" class="btn btn-primary" name="submit">Add</button>
</form> 

How must be the PHP code to insert into these tables?




Aucun commentaire:

Enregistrer un commentaire