使用MySQL和PHP注册活动

时间:2017-03-15 20:32:13

标签: php android mysql database

我正在尝试为我的应用创建注册活动。我正在使用此代码:

@Override
    protected String doInBackground(String... params) {
        String nome = params[0];
        String email = params[1];
        String pwd = params[2];
        String data="";
        int tmp;

        try {
            URL url = new URL("http://6senseofficial.it/api/login/register.php?");
            String urlParams = "nome=" + nome + "&email=" + email + "&pwd=" + pwd;

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(true);
            OutputStream os = httpURLConnection.getOutputStream();
            os.write(urlParams.getBytes());
            os.flush();
            os.close();
            InputStream is = httpURLConnection.getInputStream();
            while ((tmp = is.read()) != -1) {
                data += (char) tmp;
            }
            is.close();
            httpURLConnection.disconnect();

            return data;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "Exception 1: "+e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return "Exception 2: "+e.getMessage();
        }
    }

每当我控制我的数据库上的新阵营时,所有的参数都是空的。 有人知道为什么?感谢。

PHP文件寄存器的代码是:

<?php include('../../mysql.php'); ?>
<?php
error_reporting(0);

$nome = $_GET['nome'];
$email = $_GET['email'];
$pwd = $_GET['pwd'];

$data = date('Y-m-d H:i:s');
$sql = "INSERT INTO utenti (nome, email, pwd, datacreazione) VALUES ('$nome','$email','$pwd','$data')";
if(!mysqli_query($link, $sql)){
    echo '{"message":"Unable to save the data to the database."}';
}

?>

1 个答案:

答案 0 :(得分:0)

如果要发送GET请求,则应将查询字符串附加到URL,而不是附加到正文。

正确的代码应该是:

String urlParams = "nome=" + nome + "&email=" + email + "&pwd=" + pwd;
URL url = new URL("http://6senseofficial.it/api/login/register.php?" + urlParams);

使用OutputStream删除行。