Php 未使用JSON更新mysql数据
我的问题困扰了我将近三天。我一直在互联网上寻找解决方案 我正在开发一个android程序,使用JSON和php mysql进行数据管理。我的主要修订是从这里开始的 以下是我的JSON解析器代码:`Php 未使用JSON更新mysql数据,php,android,mysql,json,Php,Android,Mysql,Json,我的问题困扰了我将近三天。我一直在互联网上寻找解决方案 我正在开发一个android程序,使用JSON和php mysql进行数据管理。我的主要修订是从这里开始的 以下是我的JSON解析器代码:` /* * a JSON Parser class to get JSON from URL. This class supports two http request methods GET and POST to get json from url. * */ public class
/*
* a JSON Parser class to get JSON from URL. This class supports two http request methods GET and POST to get json from url.
*
*/
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.i("postData", httpResponse.getStatusLine().toString());
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.i("value is for POST", is.toString());
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.i("value is for GET", is.toString());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
下面是我的PHP脚本,用于更新SaveProductDetails类假定发布的数据
<?php
/*
* Following code will update a product information
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['pid']) && isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'] )&& isset($_POST['img'] )&& isset($_POST['qty'] ) ) {
$pid = $_POST['pid'];
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
$base=$_POST['img'];
$qty = $_POST['qty'];
// include db connect class
//require_once __DIR__ . '/db_connect.php';
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql update row with matched pid
$result = mysql_query("UPDATE products SET name = '$name', price = '$price', description = '$description', image='$base', qty='$qty' WHERE pid = $pid" );
// check if row inserted or not
if ($result) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Product successfully updated.";
// echoing JSON response
echo json_encode($response);
} else {
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
注意!!
编辑产品代码
private static final String TAG_IMAGE="image";
...
params.add(new BasicNameValuePair(TAG_IMAGE,img));
//TAG_IMAGE="image";
PHP脚本
if (isset($_POST['pid']) && isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'] )&& isset($_POST['img'] )&& isset($_POST['qty'] ) ) {
//but $_POST['img'] is not existing
你很容易受到@Fred ii的攻击-由于需要保存字符限制,我正在移植一些代码..我已经编辑了你刚才提到的位置..这不是代码问题..将代码粘贴到询问框是我的错误here@MarcB我知道这一点,因为这是我了解php、json和mysql如何工作的尝试先生/女士,你真的救了我的命!这有助于解决我的问题…非常感谢…非常感谢…哦!我不知道,我是拯救生命的英雄。有时候只是用不同的眼睛看。因此这里是堆栈溢出。我很乐意帮忙。
<?php
/*
* Following code will delete a product from table
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql update row with matched pid
$result = mysql_query("DELETE FROM products WHERE pid = $pid");
// check if row deleted or not
if (mysql_affected_rows() > 0) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Product successfully deleted";
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
/**
* Background Async Task to Save product Details
* */
class SaveProductDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditProduct.this);
pDialog.setMessage("Saving product ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Saving product
* */
10-17 09:46:59.760: D/Nilai String name lepas Saving(7728): shiyh update
10-17 09:46:59.760: D/Nilai String price lepas Saving(7728): 3.00
10-17 09:46:59.760: D/Nilai String desc lepas Saving(7728): afagfeag
10-17 09:46:59.760: D/Nilai String img lepas Saving(7728): android.widget.ImageView{137dff73 V.ED.... ......I. 10,60-170,180 #7f08005a app:id/imagePreview}
10-17 09:46:59.760: D/Nilai String qty lepas Saving(7728): 3.00
10-17 09:46:59.760: D/Nilai String pid lepas Saving(7728): 11
10-17 09:46:59.760: I/System.out(7728): [pid=11, name=shiyh update, price=3.00, description=afagfeag, image=android.widget.ImageView{137dff73 V.ED.... ......I. 10,60-170,180 #7f08005a app:id/imagePreview}, qty=3.00]
10-17 09:46:59.863: I/Choreographer(7728): Skipped 61 frames! The application may be doing too much work on its main thread.
10-17 09:46:59.969: I/art(7728): Background sticky concurrent mark sweep GC freed 2613(116KB) AllocSpace objects, 0(0B) LOS objects, 689% free, 3MB/6MB, paused 3.561ms total 119.584ms
10-17 09:46:59.991: I/postData(7728): HTTP/1.1 200 OK
10-17 09:46:59.991: I/value is for POST(7728): org.apache.http.conn.EofSensorInputStream@1527e9eb
10-17 09:46:59.993: D/Saving Response(7728): {"success":0,"message":"Required field(s) is missing"}
10-17 09:46:59.994: I/System.out(7728): Fuck not updated shit!
<?php
/*
* Following code will update a product information
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['pid']) && isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'] )&& isset($_POST['img'] )&& isset($_POST['qty'] ) ) {
$pid = $_POST['pid'];
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
$base=$_POST['img'];
$qty = $_POST['qty'];
// include db connect class
//require_once __DIR__ . '/db_connect.php';
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql update row with matched pid
$result = mysql_query("UPDATE products SET name = '$name', price = '$price', description = '$description', image='$base', qty='$qty' WHERE pid = $pid" );
// check if row inserted or not
if ($result) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Product successfully updated.";
// echoing JSON response
echo json_encode($response);
} else {
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
private static final String TAG_IMAGE="image";
...
params.add(new BasicNameValuePair(TAG_IMAGE,img));
//TAG_IMAGE="image";
if (isset($_POST['pid']) && isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'] )&& isset($_POST['img'] )&& isset($_POST['qty'] ) ) {
//but $_POST['img'] is not existing