doudengjin8251 2013-11-21 17:04
浏览 67

Android,PHP和MySQL

i'm trying to develop an android application that connects to a MySQL database.

I'm using PHP as a web Service parsing the data from a JSON array.

But when i click to do the process nothing happens, the data is not added to the database. First i change the code and i'm using AsynTask<> for avoiding the RunningOnMainThread Exception, but y can't make it work.

Any suggestions?

Here's the code:

PHP:

<?php
$hostname ="localhost";
$database ="traffic_qro";
$username ="root";
$password ="";

$localhost = mysql_connect($hostname,$username,$password);

mysql_select_db("traffic_qro");
$name = $_REQUEST['driver_name'];
$number = $_REQUEST['driver_number'];
$plate = $_REQUEST['driver_plate'];
$brand = $_REQUEST['driver_brand'];
$model = $_REQUEST['driver_model'];

$sql = "INSERT INTO users VALUES('".$name."', '".$number."', '".$plate."', '".$brand."', '".$model."'');";

$res = mysql_query($sql);

if(!$res) {
    echo "Error in query: ".mysql_error();
}
mysql_close();
?>

DBTask.java

class DBTask extends AsyncTask<String, Void, Void> {

    protected Void doInBackground(String... args) {
        InputStream is = null;
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("driver_name", args[0]));
        nameValuePairs.add(new BasicNameValuePair("driver_number", args[1]));
        nameValuePairs.add(new BasicNameValuePair("driver_plate", args[2]));
        nameValuePairs.add(new BasicNameValuePair("driver_brand", args[3]));
        nameValuePairs.add(new BasicNameValuePair("driver_model", args[4]));
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://192.168.1.85/traffic_qro/add_client_doc.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("log_tag", is.toString());
        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection" + e.toString());
        }
        return null;
    }

    protected void onPostExecute(String feed) {
        // TODO: check this.exception
        // TODO: do something with the feed
    }
}

Implementation od DBTask.java

new DBTask().execute(driver_name, drvr_num, driver_car_plate,
                    driver_car_brand, driver_car_model);
  • 写回答

1条回答 默认 最新

  • dpj96988 2013-11-21 17:06
    关注

    You've got an error in your SQL statement, specifically around '".$model."''); (two single quotes after $model):

    Change:

    $sql = "INSERT INTO users 
            VALUES('".$name."', '".$number."', '".$plate."', '".$brand."', '".$model."'');";
    

    to

    $sql = "INSERT INTO users 
            VALUES('".$name."', '".$number."', '".$plate."', '".$brand."', '".$model."');";
    

    To check for errors like this, use:

    $res = mysql_query($sql, $localhost) or trigger_error(mysql_error($localhost));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)