2016-02-06 103 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

我添加了一張圖片:( –

相關問題