php – Insert form data on database table depends on the value selected from dropdown list-ThrowExceptions

Exception or error:

I have 3 tables in my database tbl1, tbl2 and tbl3.

My HTML form

<form action="" method="post">
<input type="text" class="form-control" id="department" name="department" placeholder="Enter Name" required>
<input type="text" class="form-control" id="cost" name="cost" placeholder="Enter Name" required>
   <select class="custom-select" name="roles" id="inputGroupSelect01 status">
                    <option selected>Choose...</option>
                    <option value="Available">Available</option>
                    <option value="Repair">Repair</option>
                    <option value="Checked Out">Checked Out</option>

                  </select>

ADD USER

My question is if the user chose Available then the entire form data should go to tbl1, if the value is disposed then tbl2, if its checked out then tbl3.

How can i do it?

How to solve:

First of all your from need to have a submit button to send data to server side

<form action="" method="post">
    <input type="text" class="form-control" id="department" name="department" 
     placeholder="Enter Name" required>
    <input type="text" class="form-control" id="cost" name="cost" 
     placeholder="Enter Name" required>
    <select class="custom-select" name="roles" id="inputGroupSelect01 status">
        <option selected>Choose...</option>
        <option value="Available">Available</option>
        <option value="Repair">Repair</option>
        <option value="Checked Out">Checked Out</option>                     
    </select>
    <button type="submit">
        save data
    </button>
</form>

Assume that your form submitted correctly to server side, you can use switch case for this case to check which option is selected and then save form data to related table:

<?php 
switch($_POST['roles']) {
    case 'Available':
         // insert form data in tbl1
        break;
    case 'Repair':
         // insert form data in tbl2
        break;
    case 'Checked Out':
         // insert form data in tbl3
        break; 
    default:
        echo 'no role selected';
        break;
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *