每当我尝试通过xampp服务器从Android应用程序插入数据时 它插入新的空行但没有插入数据
这是我的代码
注册Java文件
public class Register extends Activity {
EditText ET_fname,ET_lname,ET_mail,ET_phone,ET_subject,ET_pass;
String fname,lname,mail,phone,subject,pass;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
ET_fname=(EditText)findViewById(R.id.fname);
ET_lname=(EditText)findViewById(R.id.lname);
ET_mail=(EditText)findViewById(R.id.mail);
ET_phone=(EditText)findViewById(R.id.phone);
ET_subject=(EditText)findViewById(R.id.subject);
ET_pass=(EditText)findViewById(R.id.pass);
}
public void userReg(View view)
{
if (view.getId() == R.id.button) {
fname = ET_fname.getText().toString();
lname = ET_lname.getText().toString();
mail = ET_mail.getText().toString();
phone = ET_phone.getText().toString();
subject = ET_subject.getText().toString();
pass = ET_pass.getText().toString();
String method = "Register";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method, fname, lname, mail, phone, subject, pass);
finish();
}
}
}
背景活动
public class BackgroundTask extends AsyncTask<String,Void,String>{
Context ctx;
BackgroundTask(Context ctx){
this.ctx=ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String reg_url="http://10.0.2.2/staff/register.php";
String login_url="http://10.0.2.2/staff/login.php";
String method=params[0];
if(method.equals("Register"))
{
String fname=params[1];
String lname=params[2];
String mail=params[3];
String phone=params[4];
String subject=params[5];
String pass=params[6];
try {
URL url=new URL(reg_url);
HttpURLConnection httpURLConnection=
(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new
OutputStreamWriter(OS,"UTF-8"));
String data= URLEncoder.encode("fname","UTF-8")+" =
"+URLEncoder.encode(fname,"UTF-8")+" & "+
URLEncoder.encode("lname","UTF-8")+" =
"+URLEncoder.encode(lname,"UTF-8")+" & "+
URLEncoder.encode("mail","UTF-8")+" = "+URLEncoder.encode(mail,"UTF-8")+" & "+
URLEncoder.encode("phone","UTF-8")+" = "+URLEncoder.encode(phone,"UTF-8")+" & "+
URLEncoder.encode("subject","UTF-8")+" = "+URLEncoder.encode(subject,"UTF-8")+" & "+
URLEncoder.encode("pass","UTF-8")+" = "+URLEncoder.encode(pass,"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();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}
这是我的插入php文件
<?php
error_reporting(0);
require "init.php";
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$mail = $_POST["mail"];
$phone = $_POST["phone"];
$subject = $_POST["subject"];
$password = $_POST["password"];
//$name = "sdf";
//$password = "sdf";
//$email = "sdf@r54";
$sql = "INSERT INTO staff_db
VALUES('$fname','$lname','$mail','$phone','$subject', '$password');";
if(!mysqli_query($con, $sql)){
echo '{"message":"Unable to save the data to the database."}';
}
else
echo '{"message":"Injected Successfully."}';
?>