-1

I have android application that check if in DB exists already the phone number. but always it return "+" even there doesn't exists the phone number, this is my code:

  URL url = new URL(URL_CHECK);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("cphone", "UTF-8") + "=" + URLEncoder.encode(params[0], "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "UTF-8"));
                String response = "";
                String line ="";
                while ((line = bufferedReader.readLine()) != null) {
                    response += line;

                }
                bufferedReader.close();
                IS.close();
                info = response;
                return info; 

The variable "info" always equals "+". This is my PHP Script:

    <?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "registers";
$con = new mysqli($servername, $username, $password, $dbname);
 $user_name = $_POST["cphone"];  
 $sql_query = "select id from users where phone like '$user_name';";  
 $result = mysqli_query($con,$sql_query);  
 if(mysqli_num_rows($result) >0 )  
 {  
 $row = mysqli_fetch_assoc($result);  
 $name =$row["id"];   
 echo "$name";
 }  
 else  
 {   
 echo "+";  
 }  
 ?>  

The php script work when instead of $_POST["cphone"] I write example "+37455001100".

arm_1234
  • 1
  • 1

1 Answers1

-1

The way you POST the value might be wrong. You can refer an answer at https://stackoverflow.com/a/2938787/2435238 on correctly sending parameters.

Community
  • 1
  • 1
Sujay
  • 3,417
  • 1
  • 23
  • 25
  • This answer is using deprecated and not avaible(on android 6) Apache HttpClient – Selvin Jun 28 '16 at 08:32
  • I just answered with the url & didnt try to copy the code & paste here. Also I am just a beginner in stackoverflow & hence learning things which I can do & cannot. Thank you for your kind answer & letting me know when I get down votes. – Sujay Jun 28 '16 at 08:50
  • I copied the code from my other class. in the class the code works! – arm_1234 Jun 28 '16 at 09:37