gpt4 book ai didi

php - 值总是返回 0,它应该返回插入的行数

转载 作者:IT老高 更新时间:2023-10-28 23:12:23 25 4
gpt4 key购买 nike

我有两张表,一张是Information,一张是WorkForce

信息

enter image description here

劳动力

enter image description here

WorkForce中的twf column用来获取Informationid,假设return为1 ,但返回值0。如果Information中的id 5,twf也应该是5。

首先我用a来表示选中的行数,然后将a的参数添加到addWorkForce中。 toast 总是显示 0 。

  a=addInformation(name,weather,date2,status,first1[1],last1[1]);
Toast.makeText(getApplicationContext(),a+"",Toast.LENGTH_LONG).show();
addWorkForce(Sub, NoP, NoH,a);

添加信息功能

  public long 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(),s,Toast.LENGTH_LONG).show();
}

@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);

return 0;

}

有人可以帮我解决问题吗?

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')";

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

这就是我使用代码将行插入到 sqlite 中的方式,它可以工作

 a = ts.insertTimeSheet(name, weather, date2, status, first1[1], last1[1]);

时间表

 public long insertTimeSheet(String name, String weather, String date, String status, String TimeIn, String TimeOut) {
database = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MyDatabaseHelper.Name, name);
values.put(MyDatabaseHelper.Weather, weather);
values.put(MyDatabaseHelper.Date, date);
values.put(MyDatabaseHelper.Status, status);
values.put(MyDatabaseHelper.TimeIn_Info, TimeIn);
values.put(MyDatabaseHelper.TimeOut_Info, TimeOut);
return database.insert(MyDatabaseHelper.TABLE_INFO, null, values); // if the id in Information is 5, twf display 5 too

最佳答案

根据您的代码添加信息:

a=addInformation(name,weather,date2,status,first1[1],last1[1]);

这里,您在主线程上调用 addInformation 并将返回值保存到变量 a。但问题是,您添加信息的实际实现是在 AsyncTaskdoInBackground 中完成的,它实际上在工作线程(异步调用)上完成了这项工作,所以你总是会得到a=0 的默认值(因为 a 很长)。

您需要做的是将您的代码移动到 AsyncTaskonPostExecute 函数,如下所示:

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(),s,Toast.LENGTH_LONG).show();
addWorkForce(Sub, NoP, NoH,a);
}

@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);

}

还有另一种使用回调的方法。实现一个接口(interface),在你的 AsyncTask 完成后,你可以发送回调来做另一个工作。

关于php - 值总是返回 0,它应该返回插入的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34534111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com