narahari_arjun narahari_arjun - 3 months ago 9
MySQL Question

insert values to mysql using php

I am inserting some values from my android login page to mysql database using php .

login.php

<?php

$con = mysql_connect("localhost","abc","xyz") or die(mysql_error());
mysql_select_db("sync",$con) or die(mysql_error());

$user_name = $_POST['user_name'];
$user_mobile_no = $_POST['user_mobile_no'];
$user_email_id = $_POST['user_email_id'];
$imei_no = $_POST['imei_no'];
$login_date = $_POST['login_date'];
$time = $_POST['time'];

$user_name = 'Navdeep';
$user_mobile_no = '12345678990';
$user_email_id = 'nav@gmail.com';
$imei_no = '1234567890';
$login_date = '24-12-2012';
$time = '01:01:01';

$result = mysql_query("INSERT INTO
login_details(user_name,user_mobile_no,user_email_id,
imei_no,login_date,time)
VALUES('$user_name','$user_mobile_no','$user_email_id',' $imei_no','$login_date','$time')");

if($result)
{
$response["success"] = 1;
$response["message"] = "User Details inserted successfully";

echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "There was an error inserting user details";
echo json_encode($response);
}
mysql_close($con);

?>


When i use
$_POST['']
values are getting inserted as blank , but when i hardcode the values it is getting inserted , need some help

Android code :

try{
JSONObject jsonObject = new JSONObject();
jsonObject.put("user_name",userName);
jsonObject.put("user_mobile_no",mobNo);
jsonObject.put("user_email_id",email);
jsonObject.put("imei_no",imeino);
jsonObject.put("login_date",date);
jsonObject.put("time",time);
JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonObject);
String basicAuth = "Device " + new
String(Base64.encode((imeino).getBytes(), Base64.NO_WRAP));
RequestBody body =
RequestBody.create(JSON,String.valueOf(jsonArray));
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().header("Authorization",
basicAuth).url(url).post(body).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
Login.this.runOnUiThread(new Runnable() {

@Override
public void run() {
Toast.makeText(getBaseContext(), "Request to the
server failed", Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void onResponse(Call call, Response response) throws
IOException {
Log.i("response ", "onResponse(): " + response );
StatusLine statusLine = null;
String result = response.body().string();
if(result.equals("") || result.equals(null)){
Log.i("No response", "No response");
}else{
Log.i("Response","Response "+result);
statusLine = StatusLine.get(response);
final int responseCode = statusLine.code;
Log.d("Code:", String.valueOf(responseCode));
}
}
});
}catch (Exception e){
e.printStackTrace();
}

Answer

You should send form data request from android. Replace this

JSONObject jsonObject = new JSONObject();
        jsonObject.put("user_name",userName);
        jsonObject.put("user_mobile_no",mobNo);
        jsonObject.put("user_email_id",email);
        jsonObject.put("imei_no",imeino);
        jsonObject.put("login_date",date);
        jsonObject.put("time",time);
        JSONArray jsonArray = new JSONArray();
        jsonArray.put(jsonObject);
        String basicAuth = "Device " + new 
        String(Base64.encode((imeino).getBytes(), Base64.NO_WRAP));
        RequestBody body = 
        RequestBody.create(JSON,String.valueOf(jsonArray));
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().header("Authorization", 
        basicAuth).url(url).post(body).build();

With

    RequestBody formBody = new FormBody.Builder()
            .add("user_name",userName)
            .add("user_mobile_no",mobNo)
            .add("user_email_id",email)
            .add("imei_no",imeino)
            .add("login_date",date)
            .add("time",time)
            .build();

String basicAuth = "Device " + new 
        String(Base64.encode((imeino).getBytes(), Base64.NO_WRAP));
    Request request = new Request.Builder().header("Authorization", 
            basicAuth).url(url).post(formBody).build();