如何将java中的值添加到php中?

时间:2011-05-19 14:09:30

标签: java php android

我的java函数

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 BasicNameValuePair("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());
    }
}

我的PHP代码

<?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();
?>

当我执行它时,我去那里的数据库看看。

已添加,但为空值。

所以,我的问题是如何将java中的edittext获取的值传递给php

2 个答案:

答案 0 :(得分:0)

您正在从Java中执行POST请求,但请阅读PHP代码中的GET请求。发布数据时,它不会显示在URL中,其中数据应该符合GET动词。

答案 1 :(得分:0)

请记住在SQL查询中转义所有字符串以增强安全性:

$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) . "')");