how to after android connect php,insert value to sql table ?
I wanna use android post value to php
than php can receive value and insert to.
but now I have problem
I don't know why can't insert to.
this is my android code
public class MainActivity extends Activity {
TextView t;
//
String url = "http://ift.tt/1OwdThR";
//thread
Handler mHandle; //manager
HandlerThread mThread;//worker
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create table using CreateTable.php
mThread = new HandlerThread("CreateTable");//
mThread.start();//worker stand by
mHandle = new Handler (mThread.getLooper());//look for worker using Handle manager
mHandle.post(CreateTable);//give work for worker
}
//
private Runnable CreateTable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//connect php
HttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
//send value to
List< NameValuePair> vars=new ArrayList< NameValuePair>();
vars.add(new BasicNameValuePair("number","abc"));
try {
method.setEntity(new UrlEncodedFormEntity(vars,HTTP.UTF_8));
Log.e("TAG","Send to php");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
and this is my php code
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'androidhive';
$number = $_POST['number'];//$number from android
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error with MySQL connection');
mysql_query("SET NAMES 'utf8'");
mysql_select_db($dbname);
$result = mysql_query("INSERT INTO table1(Username,Password,Role) VALUES('number', 'number', 'number')");
?>
Aucun commentaire:
Enregistrer un commentaire