SQL 更新方法 returns JSONException
SQL update method returns JSONException
我的服务器上有一个简单的 MySQL table,有 4 个字段。 pid
、aid
、uname
和 active
。我正在通过我的 android 应用程序将 uname
变量发送到我的 PHP 代码,我想将布尔值 active
列从 1 更改为 0,其中 uname
字段等于我通过应用程序发送的值。这是我的代码:
我从 android 应用程序发送的值在 index.php 中像这样接收并传递到 DB_Functions.php
中的另一个函数
else if ($tag == 'notpart'){
$uname = $_POST['uname'];
$notpart = $db->notpart($uname);
if (!empty($notpart)) {
// stored successfully
$response["success"] = 1;
//$response["notpart"]["aid"] = $notpart["aid"];
$response["notpart"]["uname"] = $notpart["uname"];
echo json_encode($response);
}
else {
// failed to store
$response["error"] = 1;
//$response["error_msg"] = "JSON Error occured";
$response["error_msg"] = mysql_error();
echo json_encode($response);
}
}
DB_Functions.php中的函数:
更新: 将 $uname
更改为 '$uname'
。现在 IllegalStringOffset 警告消失了,但存在 JSONException
public function notpart($uname) {
$uuid = uniqid('', true);
echo $uname;
$result = mysql_query("UPDATE part SET active='0' WHERE uname = $uname");
// check for successful store
if ($result) {
//get event details
$pid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM part WHERE pid = $pid");
//return event details
return mysql_fetch_array($result);
} else {
return mysql_error();
}
}
但我收到非法字符串偏移警告,并且 MySQL table 没有任何更改。
这是 logcat:
05-20 21:57:26.958: E/JSON(17919): wwwww<br />n<b>Warning</b>: Illegal string offset 'uname' in <b>C:\xampp\htdocs\shareity\shareity\index.php</b> on line <b>445</b><br />n{"tag":"notpart","success":1,"error":0,"notpart":{"uname":"U"}}n
05-20 21:57:26.958: E/JSON Parser(17919): Error parsing data org.json.JSONException: Value wwwww<br of type java.lang.String cannot be converted to JSONObject
我尝试了 isset
和 !empty
方法来尝试发送非法偏移错误,但没有任何效果。有人可以告诉我怎么了吗?谢谢
$result = mysql_query("UPDATE part SET active='0' WHERE uname = $uname");
试试这个:
$result = mysql_query("UPDATE part SET active='0' WHERE `uname` = '$uname'");
另请注意,您将活动设置为 '0'
,而不是 0
。如果活动字段类型为 INT,则将其更改为 0。
此外,您还要求自己进行 SQL 注入。使用 mysql_real_escape_string()
此外,不要使用 mysql
(已弃用)扩展名。切换到 mysqli
或 PDO
.
最后要修复的是删除 echo $uname;
它破坏 json 响应添加 uname 到输出。这可能会破坏您的 json.
我的服务器上有一个简单的 MySQL table,有 4 个字段。 pid
、aid
、uname
和 active
。我正在通过我的 android 应用程序将 uname
变量发送到我的 PHP 代码,我想将布尔值 active
列从 1 更改为 0,其中 uname
字段等于我通过应用程序发送的值。这是我的代码:
我从 android 应用程序发送的值在 index.php 中像这样接收并传递到 DB_Functions.php
else if ($tag == 'notpart'){
$uname = $_POST['uname'];
$notpart = $db->notpart($uname);
if (!empty($notpart)) {
// stored successfully
$response["success"] = 1;
//$response["notpart"]["aid"] = $notpart["aid"];
$response["notpart"]["uname"] = $notpart["uname"];
echo json_encode($response);
}
else {
// failed to store
$response["error"] = 1;
//$response["error_msg"] = "JSON Error occured";
$response["error_msg"] = mysql_error();
echo json_encode($response);
}
}
DB_Functions.php中的函数:
更新: 将 $uname
更改为 '$uname'
。现在 IllegalStringOffset 警告消失了,但存在 JSONException
public function notpart($uname) {
$uuid = uniqid('', true);
echo $uname;
$result = mysql_query("UPDATE part SET active='0' WHERE uname = $uname");
// check for successful store
if ($result) {
//get event details
$pid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM part WHERE pid = $pid");
//return event details
return mysql_fetch_array($result);
} else {
return mysql_error();
}
}
但我收到非法字符串偏移警告,并且 MySQL table 没有任何更改。
这是 logcat:
05-20 21:57:26.958: E/JSON(17919): wwwww<br />n<b>Warning</b>: Illegal string offset 'uname' in <b>C:\xampp\htdocs\shareity\shareity\index.php</b> on line <b>445</b><br />n{"tag":"notpart","success":1,"error":0,"notpart":{"uname":"U"}}n
05-20 21:57:26.958: E/JSON Parser(17919): Error parsing data org.json.JSONException: Value wwwww<br of type java.lang.String cannot be converted to JSONObject
我尝试了 isset
和 !empty
方法来尝试发送非法偏移错误,但没有任何效果。有人可以告诉我怎么了吗?谢谢
$result = mysql_query("UPDATE part SET active='0' WHERE uname = $uname");
试试这个:
$result = mysql_query("UPDATE part SET active='0' WHERE `uname` = '$uname'");
另请注意,您将活动设置为 '0'
,而不是 0
。如果活动字段类型为 INT,则将其更改为 0。
此外,您还要求自己进行 SQL 注入。使用 mysql_real_escape_string()
此外,不要使用 mysql
(已弃用)扩展名。切换到 mysqli
或 PDO
.
最后要修复的是删除 echo $uname;
它破坏 json 响应添加 uname 到输出。这可能会破坏您的 json.