将数据插入Mysql数据库

时间:2013-12-08 20:56:55

标签: java php android mysql

我有以下代码:

private void setCounter(String counter,String stName){


        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("XXX");


        try{
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("vCounter",counter));
            nameValuePairs.add(new BasicNameValuePair("StName",stName));

           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           HttpResponse response = httpclient.execute(httppost);

        }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }
    }


class setCountertoDB extends AsyncTask<Void, Void, Boolean> {//kept


        @Override
        protected Boolean doInBackground(Void... params) {

            setCounter(StringCounter,streetName);
            return null;
        }
    }

PHP文件:

<?php 
$con = mysql_connect("XXX","XXX","XXX");
if (!$con)
    {
    die('Could not Connect:'. mysql_error());
    }
mysql_select_db("a6241050_Stpeeds",$con);


mysql_query ("INSERT INTO DBNAME (vCounter) VALUES('".$_REQUEST['vCounter']."') WHERE StName='".$_REQUEST['StName']."'");

mysql_close($con);
?>

应用程序运行良好并且logcat中没有错误,但是当我检查数据库时没有插入数据,请帮助,知道我使用几乎相同的代码从数据库中选择值并且它有效但是它不适用于插入!

2 个答案:

答案 0 :(得分:3)

hI如果你必须创建一个新条目,则不能使用WHERE:

  mysql_query ("INSERT INTO DBNAME (vCounter) VALUES('".$_REQUEST['vCounter']."')); 

答案 1 :(得分:1)

尝试

mysql_query ("UPDATE table_name SET vcounter='".$_REQUEST['vcounter']."'WHERE StName='".$_REQUEST['StName']."'");

这应该可以胜任。