dqhgjay5753 2015-12-13 17:51
浏览 77

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

I have a form which contains details of employee, including the basic details and educational details. How to save basic details in one table and educational details in another table?

The form is as below.

 <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>

I want to save the basic details in employee table and the educational details in educational_details table. For which I have done the below coding using angular js and php

This is my ajax.php code in which I have saved the basic details

  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();
  }

I have done saving of basic details successfully. But I am not getting idea how to save the educational details in another table with empid which contains in employee table.

If the user enters 3 educational details then 3 the data must be saved in 3 records i.e, 3 rows in database. How do I do this using angularjs and php?

This is the jsFiddle link http://jsfiddle.net/b0jtqzpj/

Please help me

  • 写回答

1条回答 默认 最新

  • douhan1860 2015-12-13 18:14
    关注

    If angular is already posting all of the details that you need for all of the inserts, you can add another function to set up the different inserts and call each one when that post type is received. This should all be done on the server unless angular isn't sending all the required data, in which case the post request will need adjusting.

    On the server the code will be similar to this:

    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();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?