angularjs - 如何同时将数据保存到两个表中

时间:2015-12-13 17:51:19

标签: php jquery mysql angularjs

我有一份表格,其中包含员工的详细信息,包括基本详细信息和教育详细信息。如何在一个表中保存基本详细信息,在另一个表中保存教育详细信息?

表格如下。

 <form name="empdetailsform">
  <h3>BASIC DETAILS</h3>

   <label>First Name</label>
   <input type="text" name="firstname" data-ng-model="empData.firstname"/></br>

   <label>Last Name</label>
   <input type="text" name="lastname" data-ng-model="empData.lastname" /></br>

   <label>Email</label>
   <input type="email" name="email" data-ng-model="empData.email"/></br>

    <label>Mobile</label>
    <input type="text" name="mobile" data-ng-model="empData.mobile"/></br>

     <label>City</label>
     <select name="city" data-ng-model="empData.city" >
       <option value="Hyderabad">Hyderabad</option>
       <option value="Bangalore">Bangalore</option>
       <option value="Chennai">Chennai</option>
      </select><br>

    <h3>Educational Details</h3>
      <button ng-click="addfield()">Add Education</button>
      <div ng-repeat="item in items">
      <input type="text" name="empData.qualification[]" placeholder="Qualification">
      <input type="text" name="empData.year[]" placeholder="Year of Passing">
      <input type="text" name="empData.percentage[]" placeholder="Percentage">
      </div> 
     <input type="submit" name="submit" ng-click="saveEmp()" />
      </form>

我想在employee表中保存基本详细信息,在educational_details表中保存教育详细信息。为此我使用angular js和php

完成了以下编码

这是我保存基本详细信息的ajax.php代码

  if( isset($_POST['type']) && !empty($_POST['type'] ) ){
   $type = $_POST['type'];

   switch ($type) {
       case "save_user":
        save_user($mysqli);
        break;
    default:
        invalidRequest();
     }
    }else{
      invalidRequest();
     }

     function save_user($mysqli){
         $firstname=$_POST['user']['firstname'];
         $lastname=$_POST['user']['lastname'];
         $email=$_POST['user']['email'];
         $mobile=$_POST['user']['mobile'];
         $city=$_POST['user']['city'];
        $query = "INSERT INTO employees ( firstname, lastname, email, mobile, city) VALUES ('$firstname', '$lastname', '$email', '$mobile', '$city')";
    $mysqli->query( $query );
    $mysqli->close();
  }

我已成功保存基本详细信息。但我不知道如何将教育细节保存在另一个包含在员工表中的empid的表中。

如果用户输入3个教育细节,那么3个数据必须保存在3个记录中,即数据库中的3行。我如何使用angularjs和php做到这一点?

这是jsFiddle链接 http://jsfiddle.net/b0jtqzpj/

请帮帮我

1 个答案:

答案 0 :(得分:0)

如果angular已经发布了所有插入所需的所有详细信息,则可以添加另一个函数来设置不同的插入,并在收到该帖子类型时调用每个插入。这应该都是在服务器上完成的,除非角度没有发送所有必需的数据,在这种情况下,邮政请求将需要调整。

在服务器上,代码与此类似:

if( isset($_POST['type']) && !empty($_POST['type'] ) ){
   $type = $_POST['type'];

   switch ($type) {
       case "save_user":
        save_user($mysqli);
        save_details($mysqli);
        break;
    default:
        invalidRequest();
     }
    }else{
      invalidRequest();
     }

function save_user($mysqli){

    $insert_sql = $mysqli->prepare("INSERT INTO employees ( firstname, lastname, email, mobile, city) VALUES (?,?,?,?,?)";
    $query->bind_param($firstname, $lastname, $email, $mobile, $city);

    $firstname=$_POST['user']['firstname'];
    $lastname=$_POST['user']['lastname'];
    $email=$_POST['user']['email'];
    $mobile=$_POST['user']['mobile'];
    $city=$_POST['user']['city'];

    $mysqli->query( $insert_sql );
    $mysqli->close();
}

function save_details($mysqli){

    $select_sql = $mysqli->prepare("SELECT id FROM employees WHERE email = ?");
    $select_sql->bind_param($email);

    $email=$_POST['user']['email'];

    $insert_sql = $mysqli->prepare("INSERT INTO educational_details ( employee_id, qualification, year, percentage) VALUES (?,?,?,?)";
    $query->bind_param($employee_id, $qualification, $year, $percentage);

    $employee_id = $mysqli->query( $select_sql );
    $qualification=$_POST['user']['qualification'];
    $year=$_POST['user']['year'];
    $percentage=$_POST['user']['percentage'];

    $mysqli->query( $query );
    $mysqli->close();
}