1

I am inserting json data from my android app in my device into xampp server in my computer using POST. It simply inserts row with empty column. When I enter the column from my browser using GET it converts it to numbers only eventhough the data is mix of numbers and letters. The type of the table column is varbinary. I entered 'crap' from my browser and it was inserted as '63726170' in the table. I am puzzled by this. Here is the PHP code that inserts the data.

if (isset($_POST)){
    //sanitize the input
    filter_var_array($_POST);
    $ri=$_POST['regID'];
    $id=json_decode($ri);
    //$decoid= $id->regID;
    if(is_array($id)){
    foreach ($id as $key=>$value){
        $fu=$id[$key];
        };
    }
    try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO regids (regID)
    VALUES ('$fu')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
        }
    catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }
   }else{
        echo "Error executing query!!!";
    }    
    $conn = null;

I am adding my android code that is sending the data to the server

URL url;
                    HttpURLConnection urlConn;
                    DataOutputStream printout;
                    url = new URL ("myurlhere");
                    urlConn = (HttpURLConnection)url.openConnection();
                    urlConn.setDoInput (true);
                    urlConn.setDoOutput (true);
                    urlConn.setUseCaches (false);
                    urlConn.setRequestProperty("Content-Type","application/json");   
                    urlConn.setRequestProperty("Accept", "application/json");
                    urlConn.setRequestMethod("POST");
                    urlConn.connect();

                    /*List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("regID", result));*/
                    String result = args.toString();
                    try{
                    //Create JSONObject here
                    JSONObject jsonParam = new JSONObject();
                    jsonParam.put("regID", result);
                    String postData="json="+jsonParam.toString();


                    // Send POST output.
                    printout = new DataOutputStream(urlConn.getOutputStream ());
                    printout.writeUTF(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
                    Log.i("NOTIFICATION", "Data Sent");
                    printout.flush ();
                    printout.close ();

                    OutputStreamWriter os = new OutputStreamWriter(urlConn.getOutputStream(), "UTF-8");
                    os.write(postData);
                    Log.i("NOTIFICATION", "Data Sent");



                    BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 
                    String msg=""; 
                    String line = ""; 
                    while ((line = reader.readLine()) != null) {
                        msg += line; } 
                    Log.i("msg=",""+msg);

                    os.close();
The_Martian
  • 3,684
  • 5
  • 33
  • 61
  • Where's the android code ? – Pedro Lobito Apr 22 '15 at 22:22
  • Use print debugging, add an `echo $fu` and `echo $sql` before the query, and see what the output is. – Daniel Nugent Apr 22 '15 at 22:23
  • @Daniel I got 'undefined variable' thrown at me at both lines and undefined index at regID when I try from my browser. – The_Martian Apr 22 '15 at 22:33
  • 3
    63726170 is the binary/hex of "crap". When you save as varbinary, you'll get binary back. You have to convert it back into your string, or save as a varchar or nvarchar instead. – Chris Apr 22 '15 at 22:33
  • @Chris do I convert that in mysql? – The_Martian Apr 22 '15 at 23:13
  • Is there a specific reason you're saving it as binary? If not then I think you should save as varchar. Otherwise, try looking [here](http://stackoverflow.com/questions/1873085/how-to-convert-from-varbinary-to-char-varchar-in-mysql). – Chris Apr 22 '15 at 23:16
  • @Chris I want it to search entries by. I read it is preferable to use binary. – The_Martian Apr 22 '15 at 23:25

0 Answers0