1

I have a simple MySQL table on my server with 4 fields. pid, aid, uname and active. I am sending the uname variable via my android app to my PHP code and I want to change the boolean active column from 1 to 0 where the uname field is equal to the value I send through the app. This is the code I have: The value I send from the android app is received like this in index.php and passed onto the other function in 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);
        }


}   

The function in DB_Functions.php: UPDATE: changed $uname to '$uname'. Now the IllegalStringOffset Warning is gone but JSONException exists

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();
    }
}

But I am getting an Illegal String offset warning and no changes are made to the MySQL table.

Here's the 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

I tried isset and !empty methods to try to send off the illegal offset error, but nothing worked. Can someone please tell me what's wrong? Thanks

1 Answer 1

1
$result = mysql_query("UPDATE part SET active='0' WHERE uname = $uname");

try this:

$result = mysql_query("UPDATE part SET active='0' WHERE `uname` = '$uname'");

Also note, you're setting active to '0', not to 0. Change it to 0 if active field type is INT.

Also, you're asking yourelf for SQLInjection. Use mysql_real_escape_string()

Additionally, dont use mysql (deprecated) extension. Switch to mysqli or PDO.

Last thing to fix is to remove echo $uname; It breaks json response adding uname to output. This probably breaks your json.

7
  • for boolean use TRUE or FALSE or 1 or 0 if you want o use numeric value but not '0'
    – Grzegorz
    May 20, 2015 at 22:09
  • Okay I changed the field type to INT. And used 0 instead of '0'. Now the String offset warning is gone, after using '$uname' instead of $uname but now I get a JSON error saying the value of uname (java.lang.String cannot be converted to JSONObject) May 20, 2015 at 22:16
  • can u attach var_dump of variable you pass to json?
    – Grzegorz
    May 20, 2015 at 22:20
  • sorry what is var_dump? May 20, 2015 at 22:21
  • it's a String. A String that has the value donor123 May 20, 2015 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.