无法从Android应用程序向mysql数据库插入数据

Hey I am trying to insert data to my sql data base and I cant figure out why it isn't working, when I run the php script on the host alone its working but when I try to do it from the android app it just doesn't do nothing I couldn't find the problem so I thought you guys might help me

here it the android code:

  @Override
    protected String doInBackground(String... params) {

        String insertUrl = "http://localhost/insert.php";

        club_name = params[1];
        String publisher_name = params[2];
       String club_contact = params[3];
       String club_location = params[4];
       String club_hours = params[5];
       String club_age = params[6];
       String club_description = params[7];

        try {
            URL url = new URL(insertUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String data = URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(club_name,"UTF-8")+"&"+
                    URLEncoder.encode("number","UTF-8")+"="+URLEncoder.encode(publisher_name,"UTF-8");
                  //  URLEncoder.encode("publisher_phone","UTF-8")+"="+URLEncoder.encode(club_contact,"UTF-8")+"&"+
                   // URLEncoder.encode("location","UTF-8")+"="+URLEncoder.encode(club_location,"UTF-8")+"&"+
                   // URLEncoder.encode("hour_open","UTF-8")+"="+URLEncoder.encode(club_hours,"UTF-8")+"&"+
                   // URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(club_age,"UTF-8")+"&"+
                   // URLEncoder.encode("description","UTF-8")+"="+URLEncoder.encode(club_description,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            inputStream.close();



        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "Party has been posted!";
    }

and the php file:

<?php
require "connection.php";

$club_name = $_POST["club_name"];
$publisher_name = $_POST["publisher_name"];
$publisher_phone = $_POST["publisher_phone"];
$location = $_POST["location"];
$hour_open = $_POST["hour_open"];
$age = $_POST["age"];
$description = $_POST["description"];

$sql = "insert into apps(club_name,publisher_name,publisher_phone,location,hour_open,age,description) values ('$club_name','$publisher_name','$publisher_phone','$location','$hour_open','$age','$description')";
$query = mysqli_query($conn,$sql);

?>

connection php:

<?php
$db_name = "ori";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";
$conn = mysqli_connect($server_name,$mysql_username,$mysql_password,$db_name);

if($conn)
{
    //echo "connects";
}
?>
douyingmou1389
douyingmou1389 好文章,我为它添加了书签。
接近 4 年之前 回复
dongwen7730
dongwen7730 “看着每一条线”都像看着它一样?虽然这是一件好事,但它并没有使用调试器。您是否阅读过JoeC的链接并完成该文章中的步骤?
接近 4 年之前 回复
doutuo7609
doutuo7609 我真的经历了每一行搜索错误,我只是找不到代码有什么问题。我的意思是php很好我确定但是android代码中的东西不起作用我无法想象出什么..
接近 4 年之前 回复
dtrnish3637
dtrnish3637 欢迎来到StackOverflow!看起来您需要学习使用调试器。请帮助自己一些补充调试技巧。如果您之后仍有问题,请随时回来查看更多详情。
接近 4 年之前 回复
dongwen750492
dongwen750492 使用PDO或清理这些字符串。您的应用程序是开放的SQL注入。你确认$conn实际连接到数据库吗?你的connection.php是什么样的?
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐