douyamitong57935
2015-08-03 06:44
浏览 55

使用PHP从android插入数据库

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 + "
");
        }
        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条回答 默认 最新

  • dprq18175 2015-08-03 08:26
    已采纳

    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();
        ?> 
    
    点赞 评论

相关推荐 更多相似问题