Skip to content
Advertisement

Insert into database from android using PHP

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

Advertisement

Answer

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();
    ?> 
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement