将 php 值存储到 java [英] Store php value to java
问题描述
我想将数据从 Android 插入 MySQL 数据库.插入记录后,我想获取最后一个插入 id 并检索 lastID 值.是否可以?我怎样才能做到这一点?任何帮助将不胜感激.
addInformation(status, timeIn);最后ID =?//为了获得最后一个 ID 我应该写什么?public void addInformation(final String name, final String weather, final String date2, final String status, final String timeIn, final String timeOut) {类 AddInfo 扩展了 AsyncTask{ProgressDialog 加载;@覆盖protected void onPreExecute() {super.onPreExecute();loading = ProgressDialog.show(WorkDetailsTable.this, "请稍候", null, true, true);}@覆盖protected void onPostExecute(String s) {super.onPostExecute(s);load.dismiss();Toast.makeText(getApplicationContext(),s, Toast.LENGTH_LONG).show();//addWorkForce(Sub, NoP, NoH, Long.parseLong(s));//addWorkDetails(results, Long.parseLong(s));}@覆盖受保护的字符串 doInBackground(String... params) {HashMap<字符串,字符串>data = new HashMap();data.put(Config.KEY_USER_NAME, name);data.put(Config.KEY_WEATHER, 天气);data.put(Config.KEY_DATE, date2);data.put(Config.KEY_STATUS, status);data.put(Config.KEY_TIMEIN, timeIn);data.put(Config.KEY_TIMEOUT, 超时);RequestHandler rh = new RequestHandler();String result = rh.sendPostRequest(Config.ADD_INFORMATION, data);返回结果;}}AddInfo ru = new AddInfo();ru.execute(name,weather,date2,status,timeIn,timeOut);}
ADD_INFORMATION.php
<块引用>
我想获取 $insertId 并放入 lastID
根据评论,我正在创建新答案,因为我的两个答案在基础上都是正确的.我举了一个例子,使用 android (apache commons 4.5.1) 和 php 5.6.两个版本(4.5.1、5.6)都不是必需的,只是我现在使用的.
示例假设您有一个名为 information 的 mysql 表,其中包含字段 status、time_in 和另一个标记为 AUTO_INCREMENT的字段强>.
<小时>Java 部分
在原来的 ::doInBackground(String ...params) 函数中你可以拥有
HttpClient 客户端 = HttpClients.createDefault();HttpPost httpPost = new HttpPost("http://localhost/so/sendrequest/addInformation.php");尝试{列表数据 = 新的 ArrayList(2);data.add(new BasicNameValuePair("status", "ok"));data.add(new BasicNameValuePair("timein", "12:55"));httpPost.setEntity(new UrlEncodedFormEntity(data));字符串响应 = EntityUtils.toString(client.execute(httpPost).getEntity());System.out.println(响应);//这里有你的insertid}catch(ClientProtocolException e){//TODO 自动生成的 catch 块}catch(IOException e){//TODO 自动生成的 catch 块}
基于原生Java
import java.io.IOException;导入 java.util.ArrayList;导入 java.util.List;
和 apache commons,应该包含在 android 库中(下载链接如果没有)
import org.apache.http.NameValuePair;导入 org.apache.http.client.ClientProtocolException;导入 org.apache.http.client.HttpClient;导入 org.apache.http.client.entity.UrlEncodedFormEntity;导入 org.apache.http.client.methods.HttpPost;导入 org.apache.http.impl.client.HttpClients;导入 org.apache.http.message.BasicNameValuePair;导入 org.apache.http.util.EntityUtils;
<小时>
PHP 部分
addInformation.php
real_escape_string($_POST['status']);$timein = $con->real_escape_string($_POST['timein']);$con->query("INSERT INTO information (status, time_in) VALUES ('$status', '$timein')");echo $con->insert_id;}}$ai = new AddInformation();$ai->response();
I wanted to insert data from an Android into a MySQL database. After inserting a record, I want to get the last insert id and retrieve the lastID value. Is it possible? How can I achieve this ? Any help would be appreciated.
addInformation(status, timeIn);
lastID =? // what should I write in order to get the last ID ?
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, 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);
}
ADD_INFORMATION.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$status = $_POST['status'];
$timeIn = $_POST['timeIn'];
//Creating an sql query
$sql = "INSERT INTO information(status, time_in) VALUES ('$status', '$timeIn')";
//Importing our db connection script
require_once('dbConnect.php');
//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Information Added Successfully';
$insertId=mysql_insert_id();
echo json_encode($insertId);
}else{
echo 'Could Not Add Information';
}
//Closing the database
mysqli_close($con);
}
?>
I want to get the $insertId and put into lastID
Based on the comments, I am creating new answer, as both my answers are correct in the basics. I made example, working with android (apache commons 4.5.1) and php 5.6. Both version (4.5.1, 5.6) are not requirements, just what I am using right now.
Example assume you have a mysql table called information with fields status, time_in and with another field marked as AUTO_INCREMENT.
Java Part
in original ::doInBackground(String ...params) function you could have
HttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost/so/sendrequest/addInformation.php");
try{
List<NameValuePair> data = new ArrayList<NameValuePair>(2);
data.add(new BasicNameValuePair("status", "ok"));
data.add(new BasicNameValuePair("timein", "12:55"));
httpPost.setEntity(new UrlEncodedFormEntity(data));
String response = EntityUtils.toString(client.execute(httpPost).getEntity());
System.out.println(response); //here you have your insertid
}catch(ClientProtocolException e){
// TODO Auto-generated catch block
}catch(IOException e){
// TODO Auto-generated catch block
}
based on native Java
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
and apache commons, which should be included with android library (download link if not)
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
PHP Part
addInformation.php
<?php
class AddInformation
{
function response(){
/** @var mysqli $con */
require_once('dbConnect.php'); //$con = new mysqli('127.0.0.1', 'root', '', 'so');
$status = $con->real_escape_string($_POST['status']);
$timein = $con->real_escape_string($_POST['timein']);
$con->query("INSERT INTO information (status, time_in) VALUES ('$status', '$timein')");
echo $con->insert_id;
}
}
$ai = new AddInformation();
$ai->response();
这篇关于将 php 值存储到 java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!