我正在尝试将数据从 android 应用程序插入到 mysql 数据库表中。但不知何故数据没有被插入..我认为android和mysql数据库没有连接。我没有从 android 端收到任何错误。但在 php 方面...如果我运行 url .. 我会收到通知。那个未定义的变量。但这些变量在android中。
这是我的 android 端代码:
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());
}
}
});
}
}
这是我的 php 端代码:
<?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);
?>