1

I am working on my Android MySQL project and I have made my database on WAMP server2.0. Database name is "test". I used PHP for insert operation.

this is my code.

b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0)
{
    // TODO Auto-generated method stub
setContentView(R.layout.activity_main);
findViewById(R.id.newslabel);
findViewById(R.id.timetable);
try{

    HttpClient httpClient =new DefaultHttpClient();
    HttpPost httpPost=new HttpPost("http://127.0.0.1/swapnil/mycon.php");       
    if(httpPost != null)
        {
            Context context = getApplicationContext();
            CharSequence text = "Connected";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

        }

    try
    {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("name", name1.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("id", id1.getText().toString()));


         httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         httpClient.execute(httpPost);

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    httpClient.execute(httpPost);

when i click on button my data does not insert into my database table.

this is my mycon.php file

<?php
// array for JSON response
$response = array();



// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();
$name=$_POST['name'];
$id=$_POST['id'];
// mysql update row with matched pid
$result =mysql_query("insert into info(name, id) values ('$name', '$id')", $connect);
$response["success"] = 1;
$response["message"] = "Product successfully updated.";

// echoing JSON response
echo json_encode($response);
?>

i could not find what was my mistake in this code. i just wnat to insert data from android applicaton to mysql database. please help me.

4
  • 1
    where is your $connect variable? Commented Mar 31, 2014 at 19:02
  • I would also suggest using PDO instead of mysql_query. Commented Mar 31, 2014 at 19:03
  • Have you solved your issue? Commented Apr 1, 2014 at 17:32
  • Yes, i have solved my issue. LINK check step 3 and 6. Commented Apr 3, 2014 at 11:35

1 Answer 1

1

Your id column is an INT.

Change

INSERT INTO info(name, id) VALUES ('$name', '$id')

to

INSERT INTO info(name, id) VALUES ('$name', $id)

You must not sourund INT values with ''

Sign up to request clarification or add additional context in comments.

Comments

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.