这是用于连接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();
}
}
}
答案 0 :(得分:0)
在这里,您缺少执行插入查询。您可以使用mysqli_query
来执行此操作。
接下来处理大多数条件的代码,以便在您的应用中获得更好的响应。
注意:查询的响应在JSON中给出,因为它将传递给Android应用程序。
$db_name = "testing";
$mysql_user = "root";
$mysql_pass = "squidoo";
$server_name = "localhost";
// Create connection
$con = mysqli_connect($server_name, $mysql_user, $mysql_pass, $db_name);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
// check all parameters are with values
$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')";
// execute the query
if (mysqli_query($con, $sql_query))
{
$data = array('result' => 'success', 'message' => 'Successfully user added!');
}
else
{
$data = array('result' => 'error', 'message' => 'Error while registration!');
}
// close the connection
mysqli_close($con);
/* JSON Response */
header("Content-type: application/json");
echo json_encode($data);
?>
答案 1 :(得分:0)
这是因为您从未执行过SQL查询,因此无法将数据插入数据库。执行SQL,我认为它肯定会有用。
mysqli_query($con, $sql_query)
注意:您需要使用mysqli预处理语句,因为您目前容易受到SQL注入攻击。