简体   繁体   English

在MySQL php中将ID从一个表插入另一个表

[英]Insert ID from one table to another in MySQL php

I have two tables, one is Information and another is work_force 我有两个表,一个是Information ,另一个是work_force

Information 信息

在此输入图像描述 ` `

work_force work_force

在此输入图像描述

When the addInformation() get called, I want data insert into Information , and the id which is auto-increment will insert into table workForce, column twf . 当调用addInformation() ,我希望将数据插入到Information中 ,并且自动增量的id将插入到表workForce, 列twf中

This is what I've tried 这就是我尝试过的

addInformation.php addInformation.php

<?php 

    if($_SERVER['REQUEST_METHOD']=='POST'){

        //Getting values
        $name = $_POST['name'];
        $weather = $_POST['weather'];
        $date = $_POST['date'];
        $status = $_POST['status'];
        $timeIn = $_POST['timeIn'];
        $timeOut = $_POST['timeOut'];

        //Creating an sql query
        $sql = "INSERT INTO information(name, weather, date, status, time_in, time_out) VALUES ('$name','$weather','$date', '$status', '$timeIn', '$timeOut')";
        $sql="INSERT INTO work_force (twf) VALUES (LAST_INSERT_ID(), )"


        //Importing our db connection script
        require_once('dbConnect.php');

        //Executing query to database
        if(mysqli_query($con,$sql)){
            echo 'Information Added Successfully';
        }else{
            echo 'Could Not Add Information';
        }

        //Closing the database 
        mysqli_close($con);
    }
?>

But I get stuck in insert id into twf . 但我陷入插入idtwf

 addInformation(name, weather, date2, status, first1[1], last1[1]);
 addWorkForce(Sub, NoP, NoH, a);

 public void addInformation(final String name, final String weather, final String date2, final String status, final String timeIn, final String timeOut) {
        class AddInfo extends AsyncTask<String, Void, String> {
            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait", null, true, true);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(getApplicationContext(), "AAAA"+s, Toast.LENGTH_LONG).show();
                //addWorkForce(Sub, NoP, NoH, Long.parseLong(s));
               // addWorkDetails(results, Long.parseLong(s));
            }

            @Override
            protected String doInBackground(String... params) {

                HashMap<String, String> data = new HashMap<String, String>();
                data.put(Config.KEY_USER_NAME, name);
                data.put(Config.KEY_WEATHER, weather);
                data.put(Config.KEY_DATE, date2);
                data.put(Config.KEY_STATUS, status);
                data.put(Config.KEY_TIMEIN, timeIn);
                data.put(Config.KEY_TIMEOUT, timeOut);
                RequestHandler rh = new RequestHandler();
                String result = rh.sendPostRequest(Config.ADD_INFORMATION, data);
                return result;
            }
        }

        AddInfo ru = new AddInfo();
        ru.execute(name, weather, date2, status, timeIn, timeOut);
    }

addWorkForce.php addWorkForce.php

<?php 
    if($_SERVER['REQUEST_METHOD']=='POST'){

        //Getting values
        $subcontractors = $_POST['subcontractors'];
        $noPeople = $_POST['noPeople'];
        $noHours = $_POST['noHours'];
        $twf = $_POST['twf'];


        //Creating an sql query
        $sql = "INSERT INTO work_force(subcontractors, number_of_person, number_of_hours, twf) VALUES ('$subcontractors','$noPeople','$noHours','$twf')";

        //Importing our db connection script
        require_once('dbConnect.php');

        //Executing query to database
        if(mysqli_query($con,$sql)){
            echo 'Work Force Added Successfully';
        }else{
            echo 'Could Not Add Work Force';
        }

        //Closing the database 
        mysqli_close($con);
    }
?>

You have some issues in your code ist of all last inserted id cant insert at like that without executing first query. 你的代码中有一些问题,所有最后插入的id都不能插入,而不执行第一个查询。

You can use like that: 你可以这样使用:

//Creating an sql query
$sql = "INSERT INTO information(name, weather, date, status, time_in, time_out) VALUES ('$name','$weather','$date', '$status', '$timeIn', '$timeOut')";

//Importing our db connection script
require_once('dbConnect.php');

//Executing query to database
if(mysqli_query($con,$sql)){
    echo 'Information Added Successfully';
    $lastid = mysqli_insert_id();       
    $sql = "INSERT INTO work_force (subcontractors, number_of_person, number_of_hours, twf) VALUES ('$subcontractors','$noPeople','$noHours',$lastid)";
    mysqli_query($con,$sql);
}else{
echo 'Could Not Add Information';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM