Php JSONParser不更新MySQL数据库
我有一个包含MySQL数据库值的列表。值随列表一起传输,但当我查看变量时(在调试模式下)。我看到只将旧值发送到PHP文件,而不是“更新”的值 这里我得到了“喜欢”的旧价值观: 这就是我列表中的函数Php JSONParser不更新MySQL数据库,php,android,mysql,Php,Android,Mysql,我有一个包含MySQL数据库值的列表。值随列表一起传输,但当我查看变量时(在调试模式下)。我看到只将旧值发送到PHP文件,而不是“更新”的值 这里我得到了“喜欢”的旧价值观: 这就是我列表中的函数 protected String doInBackground(String... args) { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.ad
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", cid));
params.add(new BasicNameValuePair(TAG_LIKES, Integer.toString(likes)));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jParser.makeHttpRequest(url_like,"POST", params);
// check log cat for response
Log.d("Create Response", json.toString());
return null;
}
最后是我的PHP文件:
<?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['likes']) && isset($_POST['id'])) {
$id = $_POST['id'];
$likes = $_POST['likes'];
// include db connect class
require_once (connection);
// connecting to db
$db = new DB_CONNECT();
// mysql update row with matched pid
$result = mysql_query("UPDATE Charts SET likes = '$likes' WHERE id = '$id'");
// check if row inserted or not
if ($result) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Like erfolgreich";
// echoing JSON response
echo json_encode($response);
} else {
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Ein Fehler ist aufgetreten";
// echoing JSON response
echo json_encode($response);
}
?>
您很容易受到攻击,而且几乎可以保证JSON文本将包含两个以上的,
字符,“破坏”您的sql查询。本质上,您正在注入自己的sql查询
您对查询也没有错误处理,并且假设查询成功。这很糟糕。即使您的SQL语法是100%完美的,也有太多其他原因导致查询无法不检查错误:
至少,你应该有
$safe_json = mysql_real_escape_string($_POST['likes']);
$sql = "UPDATE .... WHERE likes='$safe_json' ...";
$result = mysql_query($sql) or die(mysql_error());
作为一般提示,mysql_*()
函数从PHP5.4开始就被弃用了。你应该考虑切换,或者最好是.< /p> < p>首先,你的if条件在<强> JSONParser <强>中没有执行,因为
如果你的情况是
if(method.equals("POST"))
不是,
if(method == "POST")
第二个,
另外,不要在doInBackground()中使用runOnUiThread
,而是将UI更新代码放在AsyncTask的onPostExecute()
方法中 好的,我修正了你的一些建议。我将每个sql查询放在一个变量中,并用“或死…”来“保存”它们。此外,我对每个POST变量都使用“real\u escpe\u string”,但我仍然没有解决问题的方法。你指的是哪一行?例如,这一行这是我的JSONParser:if(method==“POST”){
啊,好的,我改变了它,但是为什么我要这么做呢?你不能使用=
操作符来比较字符串。第二个:但是如果我从runOnUiThread中获取整个代码并将其放在onPostExecute中,doInBackground()中就没有代码了。好的,现在它可以工作了。我只是将likes=(likes+1);从OnCreate方法放入doInBackground中。
$safe_json = mysql_real_escape_string($_POST['likes']);
$sql = "UPDATE .... WHERE likes='$safe_json' ...";
$result = mysql_query($sql) or die(mysql_error());
if(method.equals("POST"))
if(method == "POST")