2016-02-06 32 views
0
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(); 
     } 
    } 
} 

enter image description here

+0

错误inPostExecute? ''注册seccussfull“' –

+0

你可以看到我在这里写的关于使用AsyncTask的一个很好的答案。 https://stackoverflow.com/questions/35209769/using-asycntask-with-passing-a-value/35210468#35210468 –

回答

0

管理服务器端http://10.0.2.2/findDoctor/register.php这种情况下,有些数据返回到Android的代码。

并在android中使用InputStream从服务器读取数据,然后检查注册成功与否。

编辑:添加这些行,它会显示正确的输出

InputStream IS=connection.getInputStream(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(IS)); 
String line; 
StringBuilder builder = new StringBuilder(); 
while((line=reader.readLine()) != null) 
    builder.append(line); 
IS.close(); 
return builder.toString(); 

这些行说,你不关心服务器的响应。

+0

你能建议我该怎么做,我的最后一年的项目,我卡在这里 –

+0

提供给我你的PHP代码在'register.php' – ELITE

+0

我添加了一张图片:( –

相关问题