Question

I have a signup form in a android app which consist of a username and a password. I want that when a user click the submit button of the signup form then the username and password will be saved into a web-database(mySql database).

When a user press submit button then sendPostRequest(givenUsername, givenPassword); is executed.

and sendPostRequest() function is like that

private void sendPostRequest(String givenUsername, String givenPassword) {

    class SendPostReqAsyncTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {

            String paramUsername = params[0];
            String paramPassword = params[1];

            System.out.println("*** doInBackground ** paramUsername " + paramUsername + " paramPassword :" + paramPassword);

            HttpClient httpClient = new DefaultHttpClient();


            HttpPost httpPost = new HttpPost("http://10.0.2.2/imon.php");



            BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("paramUsername", paramUsername);
            BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("paramPassword", paramPassword);


            List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
            nameValuePairList.add(usernameBasicNameValuePair);
            nameValuePairList.add(passwordBasicNameValuePAir);

            try {

                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);

                httpPost.setEntity(urlEncodedFormEntity);

                try {

                    HttpResponse httpResponse = httpClient.execute(httpPost);


                    InputStream inputStream = httpResponse.getEntity().getContent();

                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                    StringBuilder stringBuilder = new StringBuilder();

                    String bufferedStrChunk = null;

                    while((bufferedStrChunk = bufferedReader.readLine()) != null){
                        stringBuilder.append(bufferedStrChunk);
                    }

                    return stringBuilder.toString();

                } catch (ClientProtocolException cpe) {
                    System.out.println("First Exception caz of HttpResponese :" + cpe);
                    cpe.printStackTrace();
                } catch (IOException ioe) {
                    System.out.println("Second Exception caz of HttpResponse :" + ioe);
                    ioe.printStackTrace();
                }

            } catch (UnsupportedEncodingException uee) {
                System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
                uee.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            if(result.equals("working")){
                Toast.makeText(getApplicationContext(), "HTTP POST is working...", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(), "Invalid POST req...", Toast.LENGTH_LONG).show();
            }
        }           
    }

    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute(givenUsername, givenPassword);     
}

And my imon.php is like that:

 <?php

 $varUsername = $_POST['paramUsername'];
 $varPassword = $_POST['paramPassword'];

 if($varUsername != "" && $varPassword != ""){

$con = mysql_connect('localhost', 'root', '');
 if (!$con) {
die('Not connected : ' . mysql_error());
 }

  // make foo the current db
  $db_selected = mysql_select_db('post_db', $con);
  if (!$db_selected) {
   die ('Can\'t use foo : ' . mysql_error());
  }

  $sql="INSERT INTO post_table (username,password)
 VALUES
  ('$varUsername ','$varPassword')";

   if (!mysql_query($con,$sql))
    {
    die('Error: ' . mysql_error($con));
     }

echo 'working';
  }else
       {
   echo 'invalid';
       }
   ?>

But data is not inserted into my-sql database.

What am i wrong here?? What should i do to insert the data of signup form into web database??

Was it helpful?

Solution

You can't do this

mysql_connect('localhost', 'root', '');

and then do this

if (!mysqli_query($con,$sql))

mysql and mysqli are separate extensions. you can't share resources from one with another.

mysqli_query() and mysql_query() doesn't take the same param order. the first param of mysql_query() is the query. where in mysqli_query first parameter is the connection resource. You were also intermixing the connections earlier as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top