简体   繁体   中英

insert data to mysql database from android app

xxxI am trying to insert data in mysql database from my Android app using php file. I don´t know what I am doing wrong, here is my android code:

public void insert()
    {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


        nameValuePairs.add(new BasicNameValuePair("nick",nick));
        nameValuePairs.add(new BasicNameValuePair("minuto",minuto));
        nameValuePairs.add(new  BasicNameValuePair("id_pelicula",String.valueOf(id_pelicula)));

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://xxxx.xxxxx.com/xxxx/insertar.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
        }
        catch(Exception e)
        {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
            Toast.LENGTH_LONG).show();
        }     

        try
        {
            BufferedReader reader = new BufferedReader
            (new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
        {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        Log.e("pass 2", "connection success ");
    }
        catch(Exception e)
    {
            Log.e("Fail 2", e.toString());
    }     

    try
    {
            JSONObject json_data = new JSONObject(result);
            code=(json_data.getInt("code"));

            if(code==1)
            {
            Toast.makeText(getBaseContext(), "Opinión enviada correctamente",
            Toast.LENGTH_SHORT).show();
            }
            else
            {
            Toast.makeText(getBaseContext(), "Disculpe, inténtelo de nuevo",
            Toast.LENGTH_LONG).show();
            }
    }
    catch(Exception e)
    {
            Log.e("Fail 3", e.toString());
    }
    }

and here is my php file code:

<?php

    // Connection data
        $host = 'xxx.xxx.com';;
        $uname = 'uxxxx';
        $pwd = 'xxxxx';
        $db = 'xxxxx';


        $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
        mysql_select_db($db,$con) or die("db selection failed");

        $nick=$_REQUEST['nick'];
        $minuto=$_REQUEST['minuto'];
        $id_pelicula=$_REQUEST['id_pelicula'];

        $flag['code']=0;

        if($r=mysql_query("insert into opiniones values('$nick','$minuto','$id_pelicula') ",$con))
        {
            $flag['code']=1;
            echo"hi";
        }

        print(json_encode($flag));
        mysql_close($con);
    ?>

Perhaps is because in my mysql database the "minuto" variable is integer and I use like String from android? Please somebody can help me? Thank you so much

我已解决,这句话的php文件中有一个错误:

if($r=mysql_query("insert into opiniones (nick, minuto, id_pelicula) values('$nick','$minuto','$id_pelicula') ",$con))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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