我正在開發Android的MySQL項目,並在WAMP server2.0上創建了我的數據庫。數據庫名稱是「測試」。我使用PHP進行插入操作。無法將數據插入到Android應用程序的mysql數據庫中
這是我的代碼。
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);
當我點擊按鈕時,我的數據沒有插入到我的數據庫表中。
這是我的mycon.php文件
<?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);
?>
我找不到什麼是我在這段代碼中的錯誤。我只是想從android應用程序插入數據到MySQL數據庫。請幫幫我。
哪裏是你的$連接變量sourund
INT
值? –我也建議使用PDO而不是mysql_query。 –
你解決了你的問題嗎? –