0

I am trying to insert data to my mysql database from my android using below code.But it is not inserting any data.Both mysql table column are of Int data type.I need to insert data only.Don't require any jason response. My php page is inserting data as i browse it using hardcoded value.In android it is showing the value in Toast message properly.It seems url is not posting from android.What's wrong I am doing.

This is my Php Code

<?php 

 //database constants
define('DB_HOST', 'localhost');
define('DB_USER', '****_*ry');
define('DB_PASS', '**IB**R');
define('DB_NAME', '**ry');

//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    die();
}

$mobile = $_POST['mobile'];
$usertype = $_POST['usertype'];


//creating a query
$stmt = $conn->prepare("INSERT INTO user_preference(mobile_id, car_id) VALUES('$mobile', '$usertype')");



//executing the query 
$stmt->execute();

 ?>

And this is my post method in android and I am calling this method in onCreate method

 private void postText() {



    /*getting car id and user mobile to insert in database*/
    ContactsDB db = new ContactsDB(this);
    db.open();
    String mobile=db.getDataOnlyMobile().toString().trim();
    String car_id=getIntent().getStringExtra("car_id");

    Toast.makeText(this, mobile, Toast.LENGTH_SHORT).show();
    Toast.makeText(this, car_id, Toast.LENGTH_SHORT).show();

    HttpClient Client = new DefaultHttpClient();

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    //below two variables need to be called in driver_details.php file posting url file
    nameValuePairs.add(new BasicNameValuePair("mobile", mobile));
    nameValuePairs.add(new BasicNameValuePair("usertype", car_id));

    try {

        String SetServerString = "";

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://mywebsiteurl.com/user_preference.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        SetServerString = httpclient.execute(httppost, responseHandler);

    } catch (Exception ex) {
        // failed
    }

}
Mithu
  • 665
  • 1
  • 8
  • 38
  • suggest starting with reading [how to avoid sql injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php/14569797#14569797) – danblack Dec 11 '19 at 11:33
  • Is the web request hitting the server? Is the URL and POST data correct? – danblack Dec 11 '19 at 11:48
  • yes url and post data is correct.To check the post data I have added toast and it is showing in toast perfectly – Mithu Dec 11 '19 at 11:51
  • i think from android url is not posting but don't know how to check and diagnose this – Mithu Dec 11 '19 at 11:51

0 Answers0