require "findDoctorConnect.php";
$type = $_POST["type"];
$yourName = $_POST["YourName"];
$RegNum = $_POST["regNum"];
$FatherName = $_POST["fatherName"];
$Gender = $_POST["gender"];
$MobileNumber = $_POST["mobileNumber"];
$Password = $_POST["password"];
$sql_query = "select * from doctorregistration where MobileNumber='$MobileNumber';";
$result = mysqli_query($con, $sql_query);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$temp = $row["MobileNumber"];
if ($temp == $MobileNumber) {
$row = mysqli_fetch_assoc($result);
echo "already found";
}
} else if (mysqli_query($con, $sql_query)) {
$sql_query = "insert into doctorregistration values('$type','$yourName','$RegNum','$FatherName','$Gender','$MobileNumber','$Password');";
$result = mysqli_query($con, $sql_query);
echo "successfull";
}
当我从我的应用程序的数据库,第一时间再次发送相同的数据的数据它保存在数据库中,并成功返回的注册,但是当像我的主键是手机号码,当我再次将数据发送到数据库,它说我注册成功,但实际上这次并没有保存数据。我想返回注册失败,那么该怎么办?为什么返回注册成功?
这里是我的代码:
public class BackgroundTask extends AsyncTask<String, Void, String> {
Context ctx;
AlertDialog alertDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
alertDialog=new AlertDialog.Builder(ctx).create();
}
public BackgroundTask(Context ctx) {
// TODO Auto-generated constructor stub
this.ctx=ctx;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String reg_url="http://10.0.2.2/findDoctor/register.php";
String method=params[0];
if(method.equals("register")) {
String type=params[1];
String YourName=params[2];
String regNum=params[3];
String fatherName=params[4];
String gender=params[5];
String mobileNumber=params[6];
String password=params[7];
try {
URL url=new URL(reg_url);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStream OS=connection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(OS));
String Data=URLEncoder.encode("type","UTF-8")+"="+URLEncoder.encode(type,"UTF-8")+"&"+
URLEncoder.encode("YourName","UTF-8")+"="+URLEncoder.encode(YourName,"UTF-8")+"&"+
URLEncoder.encode("regNum","UTF-8")+"="+URLEncoder.encode(regNum,"UTF-8")+"&"+
URLEncoder.encode("fatherName","UTF-8")+"="+URLEncoder.encode(fatherName,"UTF-8")+"&"+
URLEncoder.encode("gender","UTF-8")+"="+URLEncoder.encode(gender,"UTF-8")+"&"+
URLEncoder.encode("mobileNumber","UTF-8")+"="+URLEncoder.encode(mobileNumber,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(Data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS=connection.getInputStream();
IS.close();
return "registration successful";
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
alertDialog.setMessage(result);
alertDialog.show();
if (result.endsWith("Registration seccussfull")) {
Toast.makeText(ctx, result,Toast.LENGTH_LONG).show();
} else if(result.endsWith("already found")){
alertDialog.setMessage("its works");
alertDialog.show();
}
}
}
错误inPostExecute? ''注册seccussfull“' –
你可以看到我在这里写的关于使用AsyncTask的一个很好的答案。 https://stackoverflow.com/questions/35209769/using-asycntask-with-passing-a-value/35210468#35210468 –