使用PHP从Android应用程序中的MySQL数据库中插入值不起作用?

时间:2021-10-25 07:28:42

I am trying to insert some values into a MySQL-database using PHP. I am fairly certain that my android code is correct, but I have very limited understanding of PHP. Am I doing something obvious wrong?

我试图使用PHP将一些值插入MySQL数据库。我相当肯定我的android代码是正确的,但我对PHP的理解非常有限。我做错了吗?

<?

$databasehost = "MyIPAdress:port";
$databasename = "databaseIWant";
$databaseusername = "UserWithPerms";
$databasepassword = "CorrectPassword";

$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());

$email = $_POST['email'];
$password = $_POST['password'];
$school = $_POST['school'];

$sql = "INSERT INTO users(username, password, school) VALUES ('$email', '$password', '$school');

$query_exec = mysql_query($sql) or die(mysql_error());
mysql_close();

?>

Here is the android code as well: (Java)

这里是android代码:( Java)

When a user registers this method gets called:

当用户注册此方法时会调用:

private void RegisterNewUser(String username, String password) {
        RegisterUserOnNetwork regUser = new RegisterUserOnNetwork();
        regUser.execute(username, password);
    }

class RegisterUserOnNetwork extends AsyncTask<String, String, String> {
    private Exception exception;

protected String doInBackground(String... strings) {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("email", strings[0]));
    nameValuePairs.add(new BasicNameValuePair("password", strings[1]));

    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new     HttpPost("http://IpAdress/RegisterUser.php");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            Log.e("RESULT", EntityUtils.toString(httpEntity) + "l");

            return "Gebruiker suksesvol geskep!";
        } catch (Exception e) {
            e.printStackTrace();
            return "Bediener kon nie bereik word nie.";
        }
    }
}

3 个解决方案

#1


1  

No school is send to PHP. Also PHP error will be helpful in debuging.

没有学校发送给PHP。此外,PHP错误将有助于debuging。

#2


0  

You are not posting data for "School" you should add another line of code in ur android app

你没有发布“学​​校”的数据,你应该在你的Android应用程序中添加另一行代码

 nameValuePairs.add(new BasicNameValuePair("school", YOUR_DATA));

#3


0  

Add these lines before DeafultHttpClient

在DeafultHttpClient之前添加这些行

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

Add internet permission to your Manifest file.

为您的清单文件添加Internet权限。

<uses-permission android:name="android.permission.INTERNET" />

PHP code.

PHP代码。

<?php
$databasehost = "MyIPAdress:port";
$databasename = "databaseIWant";
$databaseusername = "UserWithPerms";
$databasepassword = "CorrectPassword";

$con = mysqli_connect($databasehost, $databaseusername, $databasepassword, $databasename) or die(mysqli_error());

$email = $_POST['email'];
$password = $_POST['password'];
$school = $_POST['school'];

$sql = "INSERT INTO users(username, password, school) VALUES ('$email', '$password', '$school')";

$query_exec = mysqli_query($con, $sql) or die(mysql_error());
mysqli_close();
?>

#1


1  

No school is send to PHP. Also PHP error will be helpful in debuging.

没有学校发送给PHP。此外,PHP错误将有助于debuging。

#2


0  

You are not posting data for "School" you should add another line of code in ur android app

你没有发布“学​​校”的数据,你应该在你的Android应用程序中添加另一行代码

 nameValuePairs.add(new BasicNameValuePair("school", YOUR_DATA));

#3


0  

Add these lines before DeafultHttpClient

在DeafultHttpClient之前添加这些行

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

Add internet permission to your Manifest file.

为您的清单文件添加Internet权限。

<uses-permission android:name="android.permission.INTERNET" />

PHP code.

PHP代码。

<?php
$databasehost = "MyIPAdress:port";
$databasename = "databaseIWant";
$databaseusername = "UserWithPerms";
$databasepassword = "CorrectPassword";

$con = mysqli_connect($databasehost, $databaseusername, $databasepassword, $databasename) or die(mysqli_error());

$email = $_POST['email'];
$password = $_POST['password'];
$school = $_POST['school'];

$sql = "INSERT INTO users(username, password, school) VALUES ('$email', '$password', '$school')";

$query_exec = mysqli_query($con, $sql) or die(mysql_error());
mysqli_close();
?>