1

Most of the times I don't post anything because I can find everything I need in others posts, but this one has me for a few days now, how do you guys store anything in your databases? Here is my Java code

@Override
protected Object doInBackground(Object[] params) {
   try {
            URL url = null;
            url = new URL("http://www.myserver.com/Register.php");
            HttpURLConnection conn = null;
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(CONNECTION_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            DataOutputStream os = new DataOutputStream(conn.getOutputStream());

            Uri.Builder builder = new Uri.Builder().appendQueryParameter("name", user.Name)
                                                    .appendQueryParameter("age", user.Age + "")
                                                    .appendQueryParameter("username", user.Username)
                                                    .appendQueryParameter("password", user.Password);

            String query = builder.build().getEncodedQuery();

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

            writer.write(query);
            writer.flush();
            writer.close();
            os.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
  return null;
  }

I tried a lot of solutions but nothing works, I can't put anything in the database. Every time I find something that could help, it is outdated. Here is the php file. Thank you.

<?php


$con = mysql_connect("hostname", "user", "passwd") or die (mysql_error());
mysql_select_db("database", $con) or die (mysql_error());

echo $name = $_POST["name"];
echo $age = $_POST["age"];
echo $username = $_POST["username"];
echo $password = $_POST["password"];


$sql_query = "insert into UserInfo values('', '$name','$age','$username','$password');";

$req = mysql_query($sql_query);
?>

I used mysqli but it didn't work so I tried with mysql_ here it was

$statement = mysqli_prepare($con, "INSERT INTO UserInfo (name, age, username, password) VALUE (?, ?, ?, ?);");
mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement); 
mysqli_stmt_close($statement);
mysqli_close($con);

I haven't any "exeption" or "failed to" just that and i don't inderstand what they are

       E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab759330

there is my android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app_invaders.depaname" >

<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Login"
        android:label="@string/title_activity_login" >
    </activity>
    <activity
        android:name=".Register"
        android:label="@string/title_activity_register" >
    </activity>
</application>

</manifest>
Lip
  • 91
  • 1
  • 9
  • Hi - it looks like the code you are having an issue with is actually the php code inserting into the database and that the android side of things isn't involved at all in storing the data (other than making a web call). I'd suggest you edit your question and add some clarification of where the database sits and add the `php` tag to your question. Your title also explicitly references android, but again, android doesn't seem to be the culprit if your issue is with the database aving. – jyanks Sep 30 '15 at 04:24
  • `mysql_*` is outdated. Try to use `mysqli*` – NewUser Sep 30 '15 at 05:30
  • What error message do you get? Have you checked your server logs? – Tash Pemhiwa Sep 30 '15 at 16:12
  • i m sur it comes from the java code beacause it works from an html formulare, I use 000webhost and i don't find the serveur log, I edit post about mysqli and error message. (sorry for bad english) – Lip Oct 02 '15 at 16:44
  • @philLip Try to define where the system is failing. Start by requesting the URL+URI in a browser and ensure that it works (since its a POST, [check this first](http://stackoverflow.com/questions/4797534/how-do-i-manually-fire-http-post-requests-with-firefox-or-chrome)), then check the network if you are sending the correct packages (I suggest [wireshark](https://www.wireshark.org/)). – Bonatti Oct 02 '15 at 18:32
  • With Postman, data add on databases, now i go try to use wireshark for first time when i succeded, i come back.. Thanks @Bonatti. – Lip Oct 02 '15 at 18:56
  • When i use wireshark for methode post with postman, wireshark write somthing, but when i use for my app, wireshark write nothing.(sorry for bad english) – Lip Oct 02 '15 at 19:42
  • @philLip Edit your question, writte your AndroidManifest.xml. This could be a permission issue, a "no netwwork availabe" issue, or even a tool that is misconfigured.... Also, the LogCat you posted are not important so far. Try to emulate the operation you need, then copy/paste anything with "exception" or "failed to" etc...the `W/OpenGLRenderer` are not problems, just a warning that its lacking ram/processor – Bonatti Oct 05 '15 at 11:28
  • Possible duplicate of [Restful webservice in Android](https://stackoverflow.com/questions/35948522/restful-webservice-in-android) – Martin Zeitler Sep 19 '18 at 10:34

2 Answers2

0

I guess, you're trying to add ID value in,

$sql_query = "insert into UserInfo values('', '$name','$age','$username','$password');";

instead of this. Try using this,

$sql_query = "insert into UserInfo values('$name','$age','$username','$password')";
geekido
  • 171
  • 5
0

Instead of writing

$sql_query = "insert into UserInfo values('', '$name','$age','$username','$password');";

Try this code

$Sql_Query = "INSERT INTO UserInfo (name,age,username,password) values
('".$name."','".$age."','".$username."','".$password."')";
Roy Scheffers
  • 3,832
  • 11
  • 31
  • 36