Android应用无法将条目插入数据库

时间:2016-06-28 12:21:12

标签: java php android mysql database

我试图从android app和php mysql插入数据库。我尝试了教程,谷歌和堆栈溢出,但它仍然无法正常工作。首先,我从Android应用程序中的表单用户填写表单中获取数据。

   public class Sending_Data_To_Server extends AsyncTask<String,Void,String>{
        String First_Name,Last_Name,Phone,Mail_ID,Password;

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

            First_Name=params[0];
            Last_Name=params[1];
            Phone=params[2];
            Mail_ID=params[3];
            Password=params[4];
            Log.d("SEND DATA VALUES",First_Name+Last_Name+Phone);
            //LOG SHOWS VALES COMING AS FILLED IN ABOVE CODE


            String Link="http://www.boysjoys.com/test/Android/Data/showData.php?";
            try {

                String data= URLEncoder.encode("First_Name","UTF-8")+ "=" +URLEncoder.encode(First_Name,"UTF-8");        
                data+= "&" +URLEncoder.encode("Last_Name","UTF-8")+ "=" +URLEncoder.encode(Last_Name,"UTF-8");
                data+= "&" + URLEncoder.encode("Phone","UTF-8")+ "=" +URLEncoder.encode(Phone,"UTF-8");
                data+= "&" + URLEncoder.encode("Mail_ID","UTF-8")+ "=" +URLEncoder.encode(Mail_ID,"UTF-8");
                data+= "&" + URLEncoder.encode("Password","UTF-8")+ "=" +URLEncoder.encode(Password,"UTF-8");

                Log.d("SEND CLASS---------",data);
                //DATA VARIABLE IN LOG SHOWS IT GETTING ALL VALUES AS FILLED


                URL url=new URL(Link+data);
                HttpURLConnection con= (HttpURLConnection) url.openConnection();



                /***************This Block I Also Tried to send data to server
    URLConnection connection=url.openConnection();
                connection.setDoOutput(true);
                OutputStreamWriter writer= new OutputStreamWriter(connection.getOutputStream());
                writer.write(data);
                Log.d("SEND DATA","----WRITING DATA TO SERVER");
                writer.flush();
    **************************/

                BufferedReader reader=new BufferedReader((new InputStreamReader(con.getInputStream())));
                String result=reader.readLine();
                return result;


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

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

        }

    }

然后我的PHP MYSQl代码(我认为这有一些问题),我试图将条目插入数据库,由app发送。

<?php
    $host='localhost';
$uname='root';
$pass='pass';
$db='App';

$con=mysqli_connect($host,$uname,$pass,$db);

$First_Name=$_POST['First_Name'];
$Last_Name=$_POST['Last_Name'];
$Phone=$_POST['Phone'];
$Mail_ID=$_POST['Mail_ID'];
$Password =$_POST['Password'];
$La=$_POST['La'];
$Lo=$_POST['Lo'];
$Ac=$_POST['Ac'];
$Pro=$_POST['Pro'];

$sql="INSERT INTO mobile_App(First_Name,Last_Name,Phone,Mail_ID,Password,La,Lo,Ac,Pro)
VALUES ('$First_Name,$Last_Name,$Phone,$Mail_ID,$Password,$La,$Lo,$Ac,$Pro')";

if(mysqli_query($con,$sql)){
echo 'Data Inserted Successfully';
}
else{
echo 'Try Again';

}
mysqli_close($con);
?>

这是我的数据库表结构Table Structure

1 个答案:

答案 0 :(得分:0)

如果我没错_这就是链接......

  

String Link =“http://www.boysjoys.com/test/Android/Data/showData.php?”;

如何将数据库连接为.. localhost。(我的意思是它的实时数据库链接http://www

 $con=mysqli_connect("localhost","User","Pass","FinalApp");

检查您的数据库凭据。