Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9086787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:34:21+00:00 2026-06-16T21:34:21+00:00

I am trying to insert data from android application to mysql database table. but

  • 0

I am trying to insert data from android application to mysql database table.
but somehow Data is not getting inserted.. I think android and mysql database is not connected. I am not getting any error from android side. but at php side… if i run url .. I get notices. that undefined variables. but those variables are in android.

This my android side code :

   package com.example.insertintophp;

import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText name,city,email,contact,msg;
    Button insert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        name =(EditText)findViewById(R.id.txtname);
        city =(EditText)findViewById(R.id.txtcity);
        email =(EditText)findViewById(R.id.txteid);
        contact =(EditText)findViewById(R.id.txtno);
        msg =(EditText)findViewById(R.id.txtmsg);
        insert=(Button)findViewById(R.id.btninsert);

        insert.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0)
            {
                // TODO Auto-generated method stub
                String nm = name.getText().toString();
                String ct = city.getText().toString();
                String emailid = email.getText().toString();
                String no = contact.getText().toString();
                String str = msg.getText().toString();
                insertRecords(nm, ct, emailid, no, str);

            }
            private void insertRecords(String nm,String ct,String emailid,String no,String str)
            {
                 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
                    nameValuePairs.add(new BasicNameValuePair("name", nm));
                    nameValuePairs.add(new BasicNameValuePair("city",ct));
                    nameValuePairs.add(new BasicNameValuePair("email",emailid));
                    nameValuePairs.add(new BasicNameValuePair("contact",no));
                    nameValuePairs.add(new BasicNameValuePair("msg",str));
                    sendData(nameValuePairs);
            }
            private void sendData(ArrayList<NameValuePair> data)
            {
                try 
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://10.0.2.2:8080/demo/insertData.php");
                    httppost.setEntity(new UrlEncodedFormEntity(data));
                    HttpResponse response = httpclient.execute(httppost);

                }
                catch (Exception e) {
                    // TODO: handle exception
                    Log.e("log_tag", "Error:  "+e.toString());
                }
            }
        });

    }


}

And this is my php side code :

  <?php 

$icon = mysql_connect("localhost","root",""); 
if(!$icon) 
{ 
die('Could not connect : ' . mysql_error()); 
} 
mysql_select_db("db_best_voyage", $icon)or die("database selection error"); 

echo json_encode($data); 
$name=$_POST['name']; 
$city=$_POST['city']; 
$email = $_POST['email']; 
$contact = $_POST['contact']; 
$msg = $_POST['msg']; 

mysql_query("INSERT INTO ctable (name,city,email,contact,msg) VALUES ('".$name."', '".$city."', '".$email."', '".$contact."', '".$msg."')"); 
mysql_close($icon); 

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:34:23+00:00Added an answer on June 16, 2026 at 9:34 pm

    It’s because

    1) You haven’t declared/initialized any $data variable, but you are encoding it to json and echoing.

    2) Moreover, you echo the jsonencoded data after accessing the post parameters and inserting them in the database like below.

        $name=$_POST['name']; 
        $city=$_POST['city']; 
        $email = $_POST['email']; 
        $contact = $_POST['contact']; 
        $msg = $_POST['msg']; 
    
        mysql_query("INSERT INTO ctable (name,city,email,contact,msg) VALUES ('".$name."', '".$city."', '".$email."', '".$contact."', '".$msg."')");
    
    echo json_encode($data); 
    

    Make sure to feed the $data with some data to ensure that you are successfully inserting the record.

    Edit:

    Test this first from your browser:

    <?php 
    
    $output = array();
    
    $icon = mysql_connect("localhost","root",""); 
    if(!$icon) 
    { 
    die('Could not connect : ' . mysql_error()); 
    } 
    mysql_select_db("db_best_voyage", $icon)or die("database selection error"); 
    
    echo json_encode($data); 
    $name= "name1";
    $city= "city1"; 
    $email = "email1"; 
    $contact = "contact1"; 
    $msg = "msg1"; 
    
    $insert1 = mysql_query("INSERT INTO ctable (name,city,email,contact,msg) VALUES ('".$name."', '".$city."', '".$email."', '".$contact."', '".$msg."')"); 
    mysql_close($icon); 
    
    if($insert1)
    {
            $output["success"] = 1;
        $output["message"] = "Successfully inserted";
    
        // echoing JSON response
        echo json_encode($output);
    }
    else
    {
         $output["success"] = 0;
        $output["message"] = "insertion failed......";
    
        // echoing JSON response
        echo json_encode($output);
    }
    
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to insert data in database from edittext in android but data is not
I am trying to take data from one mysql table and insert it in
I am trying to insert data from table a to table b (both are
I am trying to insert data from a database into a textarea. I can't
I am trying to insert data from one table that was imported from an
Language: OO PHP 5+ DB: MySQL I am trying to insert data from a
The data I am trying to insert is from a database, and while inserting
I am trying to insert data from one of my existing table into another
I am trying to insert data from a csv file into mysql using BigDump.
Im trying to insert data into a table in MySQL. I found/modified some code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.