Izzo32
Izzo32

Reputation: 179

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 
$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 !

Upvotes: 0

Views: 111

Answers (2)

akki
akki

Reputation: 2312

Try

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

This should do the job.

Upvotes: 1

seven7
seven7

Reputation: 66

hI if you have to make a new entry can not use the WHERE:

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

Upvotes: 3

Related Questions