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

时间:2018-03-18 07:15:53

标签: php android mysql

我是初学者。我试图使用php将数据从android插入MySQL。但它不会向数据库中添加行。

u_data_insert.php:

<?php
$servername = "localhost";
$username = "zprestau01u";
$password = "ZPrestau#01U$100";
$dbname = "zprestau01";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$username=$_POST['Name'];
$email = $_POST['Email'];
$password = $_POST['Password'];


$sql = "INSERT INTO demotable(Name,Email,Password) VALUES ('$username','$email','$password')";


if(mysqli_query($conn,$sql)){

 echo 'Data Submit Successfully';

 }
 else{

 echo "Error:".mysqli_error($conn);

 }
 mysqli_close($conn);
?>

BackgroundTask.java

public class BackgroundTask extends AsyncTask<String,Void,String> {

    Context ctx;
    BackgroundTask(Context ctx)
    {
        this.ctx=ctx;
    }


    protected String doInBackground(String... params) {

        String reg_url="http://restaurant.zpcoder.mobi/u_data_insert.php";
        String method=params[0];
        String text = "";
        if(method.equals("register"))
        {
            String name=params[1];
            String email = params[2];
            String password=params[3];

            try {
                URL url=new URL(reg_url);
                HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream os=httpURLConnection.getOutputStream();

                String data= URLEncoder.encode("Name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
                        URLEncoder.encode("Email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
                        URLEncoder.encode("Password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
                bufferedWriter.write(data);
                bufferedWriter.flush();
                int statusCode = httpURLConnection.getResponseCode();
                if (statusCode == 200) {

                    BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line;

                    while ((line = reader.readLine()) != null)
                        sb.append(line).append("\n");

                    text = sb.toString();
                    bufferedWriter.close();
                }


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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

RegActivity.java:

regRegister.setOnClickListener(new View.OnClickListener()

                {


                    @Override
                    public void onClick(View view) {


                        String method="register";
                        BackgroundTask backgroundTask=new BackgroundTask(RegActivity.this);
                        backgroundTask.execute(method,Name,Email,Password);
                        finish();
                    }


                });


            }

        });

它应该与“#34;数据提交成功”#34;一起举杯,但它显示&#34;再试一次&#34;并且不会在MySQL数据库中添加新行。你能告诉我哪里做错了吗?

1 个答案:

答案 0 :(得分:1)

当你通过字段时 - 我认为它们被称为名称,密码和&amp;联系...

String data= URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
    URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
    URLEncoder.encode("contact","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");

但是当你用PHP阅读它们时,它们被称为 - 姓名,电子邮件和密码

$username=$_POST['Name'];
$email = $_POST['Email'];
$password = $_POST['Password'];

他们应该在代码的两个部分使用相同的名称。

您还需要确保将变量名称检查为define $ sql,然后使用$ Sql。

特别是当您开始工作时,如果您的SQL失败,您将受益于输出错误。所以不是......

 echo 'Try Again';

有类似......

echo "Error:".mysqli_error($conn);

更新: 为了帮助调试...

echo "SQL=".$sql. " Error:".mysqli_error($conn);