0

I'm trying to insert some data from my android application into my database using php, I tried below script but I get this error

Fail 3﹕ org.json.JSONException: End of input at character 0 of

PHP script

<?php
    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";


    // create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // check connection
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    // insert values into table
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    

    $sql = "INSERT INTO user_message (`NM_id_mes`, `NM_mail`, `NM_content`, `NM_datetime`, `NM_name`)
            VALUES (Null,$email,$message,NOW()+ INTERVAL 7 HOUR,$name)";
            
    $flag['code']=0;

    if ($conn->query($sql) === TRUE) {
        //echo "New record created successfully";
        $flag['code']=1;
    } else {
        $flag[code']=0;
    }
    echo json_encode($flag);
    $conn->close();
?> 

Android Part

@Override
protected Void doInBackground(Void... params) {
    nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("name", ContactUsFragment.name));
    nameValuePairs.add(new BasicNameValuePair("email", ContactUsFragment.email));
    nameValuePairs.add(new BasicNameValuePair("message", ContactUsFragment.message));

    Log.d("name , email , message ",
            ContactUsFragment.name+"--- "+ContactUsFragment.email+"--- "+ContactUsFragment.message );
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://example.com/Message.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("pass 1", "connection success ");
    } catch (Exception e) {
        Log.e("Fail 1", e.toString());

    }

    try {
        BufferedReader reader = new BufferedReader
                (new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        Log.e("pass 2", "connection success ");
    } catch (Exception e) {
        Log.e("Fail 2", e.toString());
    }

    try {
        JSONObject json_data = new JSONObject(result);
        code = (json_data.getInt("code"));

        if (code == 1) {
            Log.d("Inserted Successfully", "");
        } else {
            Log.d("Sorry Try Again", "");
        }
    } catch (Exception e) {
        Log.e("Fail 3", e.toString());
    }

    return null;
}
4
  • 1
    Try to change query like: $sql = "INSERT INTO user_message (NM_id_mes, NM_mail, NM_content, NM_datetime, NM_name) VALUES (Null, '$email', '$message' ,NOW()+ INTERVAL 7 HOUR, '$name')"; Commented Aug 3, 2015 at 6:48
  • did you change url while running example.com/Message.php Commented Aug 3, 2015 at 6:51
  • the same error the confusing thing here that it working but for other table .i don't know what's wrong this time.. Commented Aug 3, 2015 at 6:53
  • @user2217535 yes bro , it's fake data for posting here only Commented Aug 3, 2015 at 6:54

1 Answer 1

1

You have got small error in your PHP script

<?php
    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";


    // create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // check connection

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // insert values into table
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];


    $sql = "INSERT INTO user_message (`NM_id_mes`, `NM_mail`, `NM_content`, `NM_datetime`, `NM_name`)
            VALUES (Null,$email,$message,NOW()+ INTERVAL 7 HOUR,$name)";

    $flag['code']=0;

    if ($conn->query($sql) === TRUE) {
        //echo "New record created successfully";
        $flag['code']=1;
    } else {
        //$flag[code']=0; !!!!!!!!!! ERROR
        $flag['code']=0;
    }
    echo json_encode($flag);
    $conn->close();
    ?> 
Sign up to request clarification or add additional context in comments.

1 Comment

Can you specify where it is exactly, im checking it from phone right now and i can't find the difference between the old script and ur answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.