Reputation: 600
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.
Upvotes: 1
Views: 680
Reputation: 7862
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 ''
Upvotes: 1