[英]Insert ID from one table to another in MySQL php
我有兩個表,一個是Information
,另一個是work_force
信息
work_force
當調用addInformation()
,我希望將數據插入到Information中 ,並且自動增量的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);
}
?>
你的代碼中有一些問題,所有最后插入的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';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.