【问题标题】:Communication between android, php and mysql failsandroid、php和mysql之间的通信失败
【发布时间】:2016-08-18 09:53:24
【问题描述】:

我已经成功创建了一个 mysql 数据库并尝试用一个 php 文件填充它。

php 的代码如下所示:

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;
?> 

如果我手动调用 php 文件,它确实会输入但只有 0。如果我更改线路

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

它在表格的第一个字段中放置一个。 但如果我使用邮递员或我的 android 应用发布不同的值,它仍然只是发布 0。

目前我只是不明白错误。

Android 脚本:

 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);
}

【问题讨论】:

  • 您正在尝试将 字符串 'userid' 插入到用户 ID 字段中?
  • 我正在尝试通过 php 脚本将 android 应用程序提供的用户 ID 插入到 mysql 数据库中。
  • 试试这个查询INSERT INTO beerconsumption (userid, time, button, weight, sex) VALUES ('".$userid."', ''.$time."', '".$button."', '".$weight."', '".$sex."')
  • 并回显您的查询
  • @KarthiVenture 不幸的是它不是这样工作的。现在当我运行 php 文件时没有任何反应

标签: php android mysql database post


【解决方案1】:

我用你的代码测试它工作正常,请检查你的表单提交和 POST 数据。

<?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;
?> 

【讨论】:

  • 如果我像你发布的那样运行脚本,那么它每次都会用 1,0,0,0,0 创建一个新条目,我怎样才能让它从 android 应用程序中发布值?
  • 您可以从 android 应用程序获取数据,因此您只需使用 JSON 解析或使用一些网络库。所以问题是android端
  • 你确定吗?因为如果我用邮递员插件测试 chrome 它也不能正常工作。
  • 好吧,我已经添加了用于将数据发送到 php 文件的方法
【解决方案2】:

我做了一些研究并将我的 php 代码更改为:

<?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);
?>

这似乎运作良好,但我现在有一些安全问题。这都是纯文本传输的不是吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2018-12-22
    • 2011-08-23
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多