0

Data is not inserting in MySql from android there is no error in the code, i have defined them using AsyncThread - doInBackground and onPostExecute..Its showing the toast what i have defined in Json Object Try Catch Method 'Sorry Try Again' in else condition if data is not inserted.

InputStream is = null;
String res = null;
String line = null;
int code;

onClick Code

            AsyncTaskRunner runner = new AsyncTaskRunner();
            runner.execute();

            question = addque.getText().toString();
            choice1 = addc1.getText().toString();
            choice2 = addc2.getText().toString();
            choice3 = addc3.getText().toString();
            answer = addanswer.getText().toString();
            Explanation = addexplan.getText().toString();

AsyncTaskRunner Class

 private class AsyncTaskRunner extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        ArrayList<NameValuePair> nameValuePairs = new 
 ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("question", question));
        nameValuePairs.add(new BasicNameValuePair("choice1", choice1));
        nameValuePairs.add(new BasicNameValuePair("choice2", choice2));
        nameValuePairs.add(new BasicNameValuePair("choice3", choice3));
        nameValuePairs.add(new BasicNameValuePair("answer", answer));
        nameValuePairs.add(new BasicNameValuePair("Explanation", 
 Explanation));

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new 
HttpPost("http://192.168.43.4/insert/insert.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 1", e.toString());

        }

        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            res = sb.toString();
            Log.e("pass 2", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 2", e.toString());
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        try
        {
                JSONObject json_data;
                json_data = new JSONObject(res);
                code = json_data.getInt("code");

                if(code==1)
                {
            Toast.makeText(getActivity(), "Inserted Successfully",
                Toast.LENGTH_SHORT).show();
                }
                else
                {
             Toast.makeText(getActivity(), "Sorry, Try Again",
                Toast.LENGTH_LONG).show();
                }
        }
        catch(Exception e)
        {
                Log.e("Fail 3", e.toString());
        }
        }

    }

PHP Code

<?php
$host='localhost';
$uname='root';
$pwd='';
$db="quizDB";

$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");

$question=$_REQUEST['question'];
$choice1=$_REQUEST['choice1'];
$choice2=$_REQUEST['choice2'];
$choice3=$_REQUEST['choice3'];
$answer=$_REQUEST['answer'];
$Explanation=$_REQUEST['Explanation'];

$flag['code']=0;

 if($r=mysql_query("insert into quizquestion values('$question','$choice1', 
 $choice2, '$choice3', '$answer', '$Explanation') ",$con))
{
    $flag['code']=1;
    echo"hi";
}

print(json_encode($flag));
mysql_close($con);
?>

Correct me if i am wrong.

1 Answer 1

1

first get all string you want to insert and then call AsyncTask. may be this solve .

  question = addque.getText().toString();
  choice1 = addc1.getText().toString();
  choice2 = addc2.getText().toString();
  choice3 = addc3.getText().toString();
  answer = addanswer.getText().toString();
  Explanation = addexplan.getText().toString();

  AsyncTaskRunner runner = new AsyncTaskRunner();
  runner.execute();

and also change this 2 line . pass UTF-8 to UrlEncodeFormEntity

  UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
  httppost.setEntity(formEntity);

Edited

I think you forget to add concatination onto $choice2 , and change query like this. pass first fieldName and then Values.

$sql = "INSERT INTO MyGuests (fieldName1, fieldName2, fieldName3,fieldName4,fieldName5,fieldName6)
VALUES ('$question','$choice1','$choice2', '$choice3', '$answer', '$Explanation')";

if (mysqli_query($conn, $sql)) {
   echo "New record created successfully";
   $flag['code']=1;
   echo"hi";
} else {
   echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Sign up to request clarification or add additional context in comments.

8 Comments

from where and how to change this one UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8"); httppost.setEntity(formEntity);
in your asyncTask .
not working showing same toast..which is defined in else condition of jSON
which Toast it display?
I have edited my answer. plz check it and let me know if it is working or not.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.