本文介绍了在MySQL php中将ID从一张表插入到另一张表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两张表,一张是Information
,一张是work_force
I have two tables, one is Information
and another is work_force
信息
`
work_force
当 addInformation()
被调用时,我想将数据插入到 Information 中,并且自动递增的 id
将插入到表workForce,列twf.
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.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
的问题.
But I get stuck in insert id
into 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 都无法在不执行第一个查询的情况下插入.
You have some issues in your code ist of all last inserted id cant insert at like that without executing first query.
你可以这样使用:
//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';
}
这篇关于在MySQL php中将ID从一张表插入到另一张表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!