gpt4 book ai didi

java - 无法将数据从android插入到MySQL

转载 作者:太空宇宙 更新时间:2023-11-03 11:53:10 24 4
gpt4 key购买 nike

问题描述

Data not able insert into MySQL but the value get displayed using `print_r($listItems).

我想将 arraylist(SearchResults) 发送到 MySQL 从 android 到 Php MySQL。我现在面临的问题是数据没有插入到 MySQL 中。

WorkDetails.java

  public void addWorkDetails(ArrayList<SearchResults> listItems)
{
JSONArray jsonArray = new JSONArray();
try
{
for (SearchResults s : listItems)
{
JSONObject object= new JSONObject();
object.put("project", s.getProject());
String des=s.getDescription();
String [] description=des.split(":");
object.put("work_description", description[1]);
Toast.makeText(getApplicationContext(), description[1],Toast.LENGTH_LONG).show();
String per=s.getProgress();
String [] progress=per.split(":");
object.put("percentage", progress[1]);
String in=s.getTimeIn();
String []IN=in.split(":");
object.put("timeIn", IN[1]);
String out=s.getTimeOut();
String []OUT=out.split(":");
object.put("timeOut",OUT[1]);
jsonArray.put(object);
}
}catch(JSONException e)
{
e.printStackTrace();
}

AddWorkDetails ru = new AddWorkDetails(jsonArray);
ru.execute();
}

class AddWorkDetails extends AsyncTask<String, Void, String> {
ProgressDialog loading;

JSONArray jsonArray;
AddWorkDetails(JSONArray jsonArray){
this.jsonArray = jsonArray;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait",null, true, true);
}

@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String,String>();
data.put("listItems",jsonArray.toString());
RequestHandler rh=new RequestHandler();
String result = rh.sendPostRequest(Config.ADD_WORKDETAILS,data);
return result;
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
}

PHP

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$listItems = json_decode($_POST['listItems'], true);
$sql="INSERT INTO work_details
(project, work_description, percentage, timeIn, timeOut)
VALUES
(?, ?, ?, ?, ?)";

print_r($listItems);

if (!($stmt = $mysqli->prepare($sql))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
foreach($listItems as $item){
$stmt->bind_param("ssssss", $item['project'], $item['work_description'], $item['percentage'], $item['timeIn'], $item['timeOut']);
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
}
}
$mysqli->close();
}
?>

输出

enter image description here

使用

显示值
print_r($listItems);

配置

 public static final String ADD_WORKDETAILS="http://192.168.107.115:80/Android/CRUD/addWorkDetails.php";

最佳答案

你有

$sql="INSERT INTO work_details 
(project, work_description, percentage, timeIn, timeOut)
VALUES
(?, ?, ?, ?, ?)"; // 5 param

但同时绑定(bind)

$stmt->bind_param("ssssss", $item['project'], $item['work_description'], $item['percentage'], $item['timeIn'], $item['timeOut']); // 6 param

关于java - 无法将数据从android插入到MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34525922/

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