Mastering Development

Dropdown list insert ID instead of Name to mysql database in PHP

I have am an issue with my application where I have my drop down lists (for Departments and Users) while selecting department the users of related department appears in uesr dropdown list which is working perfectly fine except one issue, it inserts ID into my database instead of department name and user name.

Here is my code

  1. Insert Code:

            // connect to mysql
            $pdoConnect = new PDO("mysql:host=localhost;dbname=trms","root","");
        } catch (PDOException $exc) {
            echo $exc->getMessage();
        // get values form input text and number
        $idcard = $_POST['idcard'];
        $vdate = $_POST['vdate'];
        $name = $_POST['name'];
        $company = $_POST['company'];
        $sel_depart = $_POST['sel_depart'];
        $sel_user = $_POST['sel_user'];
        $contact = $_POST['contact'];
        $confirm = $_POST['confirm'];
        $floors = $_POST['floors'];
        $registerby = $_POST['registerby'];
        $purpose = $_POST['purpose'];
        $arrival = $_POST['arrival'];
        $departure = $_POST['departure'];
        //$timediff = $_POST['timediff'];
        $indexday = $_POST['indexday'];
        $status = $_POST['status'];
            $pdoQuery = "INSERT INTO `master`(`idcard`,`vdate`,`name`,`company`,`sel_depart`,`sel_user`,`contact`,`confirm`,`floors`, `registerby`,`purpose`,`arrival`,`departure`,`indexday`, `status`) VALUES (:idcard,:vdate,:name,:company,:sel_depart,:sel_user,:contact,:confirm,:floors,:registerby,:purpose,:arrival,:departure,:indexday,:status)";
        $pdoResult = $pdoConnect->prepare($pdoQuery);
            $pdoExec = $pdoResult->execute(array(
            // check if mysql insert query successful
            //echo 'Data Inserted';
            $message = "Data Inserted";
            echo "<script type='text/javascript'>alert('$message');</script>";
            //echo 'Data Not Inserted';
            $message = "Data Not Inserted";
            echo "<script type='text/javascript'>alert('$message');</script>";

2: Department dropdown list:

<div class="form-group col-sm-6">
                    <div class="row">
                        <div class="col-sm-3">
                            <label for="sel_depart"><strong>Department</strong></label>
                        <div class="col-sm-8">
                            <div class="row">
                                <div class="col-sm-12">
                                    <select id="sel_depart" name="sel_depart" class="form-control" required>
                                       <option value="0">Please Select</option>
                                        $sql_department = "SELECT * FROM department";
                                        $department_data = mysqli_query($con,$sql_department);
                                        while($row = mysqli_fetch_assoc($department_data) ){
                                            $departid = $row['id'];
                                            $depart_name = $row['depart_name'];
                                            echo "<option value='".$departid."' >".$depart_name."</option>";
  1. User dropdownlist

     <div class="row">
          <div class="col-sm-3">
                 <label for="sel_user"><strong>Employee</strong></label>
               <div class="col-sm-8">
                  <div class="row">
                       <div class="col-sm-12">
          <select class="form-control" name="sel_user" id="sel_user" >
           <option value="">Please Select</option>                                                      

  2. user code (getUser.php)

    $departid = $_POST['depart'];   // department id
    $sql = "SELECT id,name FROM users WHERE department=".$departid;
    $result = mysqli_query($con,$sql);
    $users_arr = array();
    while( $row = mysqli_fetch_array($result) ){
        $userid = $row['id'];
        $name = $row['name'];
        $users_arr[] = array("id" => $userid, "name" => $name);
    // encoding array to json format
    echo json_encode($users_arr);
  3. Ajax code

                var deptid = $(this).val();
                    url: 'getUsers.php',
                    type: 'post',
                    data: {depart:deptid},
                    dataType: 'json',
                        var len = response.length;
                        for( var i = 0; i<len; i++){
                            var id = response[i]['id'];
                            var name = response[i]['name'];
                            $("#sel_user").append("<option value='"+id+"'>"+name+"</option>");

Leave a Reply

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