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

时间:2016-01-03 15:07:34

标签: php mysql

我有两个表,一个是Information,另一个是work_force

信息

enter image description here`

work_force

enter image description here

当调用addInformation()时,我希望将数据插入信息,并且自动增量的id将插入表workForce,列twf 即可。

这是我尝试过的

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

但是我将id插入twf

 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

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

1 个答案:

答案 0 :(得分:1)

你的代码中有一些问题,所有最后插入的id都不能插入,而不执行第一次查询。

您可以这样使用:

//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';
}