无法使用php将数据库从android插入localhost

时间:2017-07-16 12:29:45

标签: php android mysql

我正在尝试将数据存储在localhost中的数据库中,我的android和php代码似乎工作正常,但它并没有将数据写入数据库。

这是我的android doInBackground

 @Override
    protected String doInBackground(String... params) {
        String type= params[0];
        String name;
        String user_name;
        String user_phone;
        String user_address;
        String password;
        String login_url = "http://192.168.1.5/Sandbox/signup.php";
        String register_url = "http://192.168.1.5/Sandbox/Register.php";

        if (type.equals("login")){
            try {
                Log.d("inside", "doInBackground: ");
                password = params[2];
                name= params[1];
                URL url = new URL(login_url);
                Log.d("middle", "doInBackground: ");
                HttpURLConnection http = (HttpURLConnection) url.openConnection();
                http.setRequestMethod("POST");
                http.setDoOutput(true);
                http.setDoInput(true);
                Log.d("middle", "doInBackground: ");
                OutputStream outputstream = http.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputstream, "UTF-8"));
                String post_data = URLEncoder.encode("name"+"UTF-8")+"="+URLEncoder.encode(name+"UTF-8")+"&"+URLEncoder.encode("password"+"UTF-8")+"="+URLEncoder.encode(password+"UTF-8");
                Log.d("middle", "doInBackground: ");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputstream.close();

                InputStream inputstream = http.getInputStream();
                BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream,"iso-8859-1"));
                result="";
                String line="";

                while ((line=bufferedreader.readLine())!=null){
                    result+=line;
                }
                bufferedreader.close();
                inputstream.close();
                http.disconnect();
                inputstream.close();

                Log.d("leaving","doInBackground: ");
                return "Registration Successful";


            } catch (Exception e) {
                e.printStackTrace();
            }
        }

这是我的php文件

<html lang="en">
<title>LOGIN</title>
<body>
<?php
require "Conn.php";



    $name= $_POST["name"];
    $password = $_POST["password"];

$mysql_qry="insert into user_table values('$name','$password')";


if (mysqli_query ($con,$mysql_qry)){
echo "Insert Success";

}
else{
    echo "ERROR!".mysqli_error($con);
    }

    $con->close();
?>

</body>
</html>

toast message表示未定义的索引

并且数据库更新为没有文本的空白框。

1 个答案:

答案 0 :(得分:0)

尝试删除html标记并仅保留php:

<?php
    require "Conn.php";

    $name= $_POST["name"];
    $password = $_POST["password"];

    $mysql_qry="insert into user_table values('$name','$password')"

    if (mysqli_query ($con,$mysql_qry)){
    echo "Insert Success";

    }
    else{
        echo "ERROR!".mysqli_error($con);
    }

 $con->close();
?>