J. Doe
J. Doe

Reputation: 483

Communication between android, php and mysql fails

I have successfully created a mysql database and trying to fill it with a php file.

the code of the php looks like this:

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $userid = $_POST['userid'];
    $time = $_POST['time'];
    $button = $_POST['button'];
    $weight = $_POST['weight'];
    $sex = $_POST['sex'];

    $sql = "INSERT INTO beerconsumption (userid, time, button, weight, sex)
    VALUES ('userid', 'time', 'button', 'weight', 'sex')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
    catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?> 

if i mannualy call the php file it does make entrys but just 0. if i change the Line

VALUES ('1', 'time', 'button', 'weight', 'sex')";

it puts a one in the first field of the table. but if i use postman or my android app to post different values it still just post 0.

at the moment i just don't get the fault.

Android Script:

 public void sendData(){

    StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

        }
    }, new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError error){

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("verify", "******");
            parameters.put("userid",userID);
            parameters.put("time",Long.toString(java.lang.System.currentTimeMillis()));
            parameters.put("button", Integer.toString(button));
            parameters.put("weight",Integer.toString(calc.person.getWeight()));
            parameters.put("sex",Integer.toString(calc.person.getSex()));

            return parameters;
        }
    };
    requestQueue.add(request);
}

Upvotes: 0

Views: 47

Answers (2)

J. Doe
J. Doe

Reputation: 483

I did some research and changed my php code to:

<?php
define('HOST','localhost');
define('USER','***');
define('PASS','***');
define('DB','***');
$con = mysqli_connect(HOST,USER,PASS,DB);

$userid = $_POST['userid'];
$time = $_POST['time'];
$button = $_POST['button'];
$weight = $_POST['weight'];
$sex = $_POST['sex'];

$sql = "insert into beerconsumption (userid,time,button,weight,sex) values ('$userid','$time','$button','$weight','$sex')";
if(mysqli_query($con,$sql)){
    echo 'success';
}
else{
  echo 'failure';
}
mysqli_close($con);
?>

this seems to work well, but i now have some security doubts. this is all transfered in plain text isn't it?

Upvotes: 0

Karthi
Karthi

Reputation: 528

I test with your code its working perfectly, may you check your form submitting and POST data.

<?php
$servername="localhost";
$dbname="android";
$username="root";
$password="root";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "INSERT INTO so(a,b,c,d,e) VALUES ('1', 'time', 'button', 'weight', 'sex')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
    catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?> 

Upvotes: 1

Related Questions