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

时间:2016-11-17 22:12:26

标签: php android mysql sql database

嘿我正在尝试将数据插入到我的sql数据库中,我无法弄清楚为什么它不起作用,当我在主机上单独运行php脚本时它工作但是当我尝试从Android应用程序它不会做什么我无法找到问题所以我认为你们可能会帮助我

这里是android代码:

  @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!";
    }

和php文件:

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

?>

连接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";
}
?>

0 个答案:

没有答案