简体   繁体   English

将数据插入Mysql数据库

[英]Inserting Data Into Mysql Database

im having the following code: 我有以下代码:

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 file: 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);
?>

the application is running well and there's no error in the logcat, but when i check the database there's no data inserted, please help with that, knowing that i used almost the same code to select values from database and it worked, but it's not working with the insert ! 该应用程序运行良好,并且logcat中没有错误,但是当我检查数据库时,没有插入任何数据,请提供帮助,因为我知道我使用几乎相同的代码从数据库中选择值并且可以正常工作,但是仍然无法正常工作与插入!

如果您必须重新输入,则无法使用WHERE:

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

Try 尝试

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

This should do the job. 这应该做的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM