-1

I have an application that insert certain parameters in a MySQL table stored in a hosting.

When i start the app for first time and send the information the register is succesfully added to the DB. Then i continue using the application. If later i try to add another registry, nothing happen, the insert is not realized. If i close (kill) the application and start it again and send the insert again it works perfectly. So in resume i can only send the insert when i first start the app.

I checked the debug and seems to be ok, if the same log in both cases.

My Android code is the following: public class AsyncCall extends AsyncTask {

This is the button that call the AsyncTask:

sentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            parametros.add(new BasicNameValuePair("Categoria",intent.getStringExtra("Categoria")));
            parametros.add(new BasicNameValuePair("Titulo",intent.getStringExtra("Titulo")));
            parametros.add(new BasicNameValuePair("Latitud",Double.toString(latLng.latitude)));
            parametros.add(new BasicNameValuePair("Longitud",Double.toString(latLng.longitude)));
            parametros.add(new BasicNameValuePair("Cuerpo",descripcion.getText().toString()));
            parametros.add(new BasicNameValuePair("User_Id",String.valueOf(User_Id)));
            parametros.add(new BasicNameValuePair("Username",username));
            parametros.add(new BasicNameValuePair("Direccion",autoCompleteUbicacion.getText().toString()));
            parametros.add(new BasicNameValuePair("Tipo",tipo));
            parametros.add(new BasicNameValuePair("FechaInicio", strFechaInicio));
            parametros.add(new BasicNameValuePair("FechaFin", strFechaFin));

            AsyncCall task=new AsyncCall();
            task.execute();

        }
    });

And here is the doInBackground

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

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://xxxxx/insert.php");
            httppost.setEntity(new UrlEncodedFormEntity(parametros));
            HttpResponse response = httpclient.execute(httppost);

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

        return null;
    }

My question: Why the insert only works in the first execution? I checked and its running and the parameters are ok in the second execution, but nothing is inserted in the DB

My PHP file:

<?php

    mysql_connect("localhost","xxxx","xxxx");
    mysql_select_db("xxxxx");

    $q=mysql_query("INSERT INTO xxxx_table 
              (xxxx_category, xxxx_title, xxxx_description, xxxx_type, 
               xxxx_location_lat, xxxx_location_lon, xxxx_direccion, 
               username,id_user, xxxx_start_date,xxxx_end_date) 
        VALUES ('".$_REQUEST['Categoria']."','".$_REQUEST['Titulo'].
                "','".$_REQUEST['Cuerpo']."','".$_REQUEST['Tipo'].
                "','".$_REQUEST['Latitud']."','".$_REQUEST['Longitud'].
                "','".$_REQUEST['Direccion']."','".$_REQUEST['Username'].
                "','".$_REQUEST['User_Id']."','".$_REQUEST['FechaInicio'].
                "','".$_REQUEST['FechaFin']."')");

    mysql_close();
   ?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
user3515652
  • 69
  • 2
  • 8
  • Why the insert only works in the first execution? I checked and its running and the parameters are ok in the second execution, but nothing is inserted in the DB – user3515652 Apr 21 '16 at 23:40
  • You had better show the code that adds data to the database then, or we could all sit here and just imagine what it might be doing – RiggsFolly Apr 21 '16 at 23:42
  • sure, but the code for the insert is working because the first run is working, also if i execute the query from a browser works normally: – user3515652 Apr 21 '16 at 23:43
  • wel you say that the java is also working, so lets have the option of seeing all the code – RiggsFolly Apr 21 '16 at 23:44
  • I just added the PHP and the button that calls the AsyncTask :) – user3515652 Apr 21 '16 at 23:52
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) – RiggsFolly Apr 22 '16 at 00:02
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Apr 22 '16 at 00:03
  • Thanks a lot for your comments @RiggsFolly, i will look at those, actually i'm just building a prototype and focusing in the Android part, so the PHP scripts that i'm using are for internet examples and didn't check them very deep. I will take your advice :) – user3515652 Apr 22 '16 at 04:05

1 Answers1

-1

I found the error, i was sending one of the parameters in blank that is related to another table in the BD.

user3515652
  • 69
  • 2
  • 8