开发者

how to add the value from java into php?

my java function

private void adddataintophp(String title, String date, String time, String channel){
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    //http post
    try{
        nameValuePairs.add(new BasicNameValuePair("Title", title));
        nameValuePairs.add(new BasicNameValuePair("Date", date));
        nameValuePairs.add(new BasicNameValuePai开发者_如何转开发r("Time", time));
        nameValuePairs.add(new BasicNameValuePair("Channel", channel));
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/insertprogram.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        Log.e("log_tag", "Error in http connection"+e.toString());
    }
}

my php code

<?php
$title = $_POST['Title'];
$date = $_POST['Date'];
$time = $_POST['Time'];
$channel = $_POST['Channel'];
mysql_connect("localhost","root","");
mysql_select_db("imammuda");
$sql=mysql_query("insert into Program (ID, Title, Date, Time, Channel) values ('NULL', '$title', '$date', '$time', '$channel')");
mysql_close();
?>

when i execute it, then i go database there and see.

It was added but with null value.

So, my question is how to pass the value that get by edittext in java into php


You're doing a POST request from within Java, but read the GET request inside your PHP code. When posting your data, it won't show up in the URL, where the data should be according to the GET verb.


Remember to escape all strings in the SQL query for enhanced security:

$sql=mysql_query("insert into Program (ID, Title, Date, Time, Channel) values ('NULL', '" . mysql_real_escape_string($title) . "', '" . mysql_real_escape_string($date) . "', '" . mysql_real_escape_string($time) . "', '" . mysql_real_escape_string($channel) . "')");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜