使用PHP从android插入数据库

时间:2015-08-03 06:44:20

标签: php android mysql

我尝试使用androiddatabase应用程序中的一些数据插入我的php,我尝试了下面的脚本,但是我收到了此错误

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

PHP脚本

 <?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部分

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

1 个答案:

答案 0 :(得分:1)

PHP脚本中出现小错误

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