【发布时间】:2015-10-02 04:15:52
【问题描述】:
这里是连接到 MySQL 数据库测试的 PHP 文件代码和将数据传递给 PHP 的 Java 代码。请帮我找出错误。当我运行它时它不会更新数据库。但是,我可以使用 PHP 来更新数据库,它会显示出来。
PHP 文件:
<?php
$db_name = "testing";
$mysql_user = "root";
$mysql_pass = "squidoo";
$server_name = "localhost";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name)
?>
<?php
require "dbconfigure.php";
$firstname = isset($_POST["Firstname"])? $_POST["Firstname"]:null;
$lastname = isset($_POST["Lastname"])? $_POST["Lastname"]:null;
$birthdate = isset ($_POST["Birthdate"])? $_POST["Birthdate"]:null;
$email = isset($_POST["email"])? $_POST["email"]: null;
$username = isset($_POST['username'])? $_POST['username'] : null;
$password = isset ($_POST["password"])? $_POST['username'] : null;
$sql_query = "INSERT INTO register (firstname, lastname, birthdate,
email,username,password)
VALUES('$firstname','$lastname','$birthdate','$email','$username','$password');";
?>
JAVA 文件:
package com.example.sony.testing;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class RegisterActivity extends Activity {
String firstname, lastname, birthdate, email, username, password;
EditText FirstName, LastName, Email, BirthDate, NewUsername, NewPassword;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_activity);
FirstName = (EditText) findViewById(R.id.FirstName);
LastName = (EditText) findViewById(R.id.LastName);
BirthDate = (EditText) findViewById(R.id.BirthDate);
Email = (EditText) findViewById(R.id.Email);
NewUsername = (EditText) findViewById(R.id.NewUsername);
NewPassword = (EditText) findViewById(R.id.NewPassword);
}
public void userReg(View view) {
firstname = FirstName.getText().toString();
lastname = LastName.getText().toString();
birthdate = BirthDate.getText().toString();
email = Email.getText().toString();
username = NewUsername.getText().toString();
password = NewPassword.getText().toString();
String method = "register";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method, firstname, lastname, birthdate, email, username, password);
finish();
}
}
package com.example.sony.testing;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class BackgroundTask extends AsyncTask <String,Void,String> {
AlertDialog alertDialog;
Context ctx;
public BackgroundTask(Context ctx)
{
this.ctx =ctx;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Information");
}
@Override
protected String doInBackground(String... params) {
String reg_url = "http://10.0.2.2/webapp/register.php";
String login_url = "http://10.0.2.2/webapp/login.php";
String method = params[0];
if (method.equals("register")) {
String Firstname = params[1];
String Lastname = params[2];
String Birthdate = params[3];
String email= params[4];
String username = params[5];
String password = params[6];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
//httpURLConnection.setDoInput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("Firstname","UTF-8")+"="+URLEncoder.encode(Firstname,"UTF-8")+"&"+
URLEncoder.encode("Lastname","UTF-8")+"="+URLEncoder.encode(Lastname,"UTF-8")+"&"+
URLEncoder.encode("Birthdate","UTF-8")+"="+URLEncoder.encode(Birthdate,"UTF-8")+"&"+
URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
httpURLConnection.connect();
httpURLConnection.disconnect();
return "Registration Success...";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("login"))
{
String login_name = params[1];
String login_pass = params[2];
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Registration Success..."))
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
else
{
alertDialog.setMessage(result);
alertDialog.show();
}
}
}
【问题讨论】:
-
在php端你缺少执行查询
-
添加此行
mysql_query($sql_query,$con) -
谢谢!我会尝试并告诉你。我不太了解php,只想将我的数据库连接到应用程序:/
-
在这种情况下,你想做
mysqli_query($con, $sql_query)。 -
感谢@budwiser 的更正
标签: java php android mysql post