0

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);
}
8
  • You're trying to insert the string 'userid' into the userid field? Commented Aug 18, 2016 at 9:58
  • i'm trying to insert the userid given from the android application in to the mysql database trough the php script. Commented Aug 18, 2016 at 9:59
  • try this query INSERT INTO beerconsumption (userid, time, button, weight, sex) VALUES ('".$userid."', ''.$time."', '".$button."', '".$weight."', '".$sex."') Commented Aug 18, 2016 at 10:00
  • and echo your query Commented Aug 18, 2016 at 10:01
  • @KarthiVenture unfortunately it doesn't work like this. now nothing happens when i run the php file Commented Aug 18, 2016 at 10:04

2 Answers 2

1

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;
?> 
Sign up to request clarification or add additional context in comments.

4 Comments

if i run the script like you posted it, then it just make a new entry with 1,0,0,0,0 every time how can i get it to post the values from the android app?
you can get data from android app so u just using JSON Parsing or use some Network Lib. so problem is android side
are you sure? because if i test with postman addon for chrome it doens't work as well.
alright i've added the method which i use to send the data to the php file
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.