php - 无法使用 php 将数据库从 android 插入本地主机

标签 php android mysql

我试图将数据存储在本地主机中存在的数据库中,我的 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 消息显示类似未定义索引的内容

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

最佳答案

尝试删除 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();
?>

关于php - 无法使用 php 将数据库从 android 插入本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45128606/

相关文章:

php - Wordpress 中的导航栏不显示 : Block

mysql - 无需root密码安装mysql 5.7

php - 如何在 php 文件上配置 grunt uncss?

php - CodeIgniter - 验证空值

android - 迁移到 Androidx 后设计 View 和导入问题

android - 将 Canvas 图像保存到 SQLite?

python - 在 Django 中过滤超过 0000-00-00 日期的对象

mysql - MySQL 中的 LEFT OUTER JOIN 与 SUBSELECT

由于...CSS,PHP 邮件表单文本框无法编辑?

android - 退出 Activity android时停止播放exo播放器视频