3

i asked this same question yesterday but i was not too clear about what i wanted the function to do, so i thought that i'd might give it another try!

i want to send data from a java program to a mysql server that i'm currently hosting via a local xampp server. Since this java class is will be used by a code written in android (also swift) it wont be possibe (atleast without a headache) to use a jdbc driver, therefor i'm trying to pass data to a php server with the help of java POST.

The problem is that literally nothing happens when i run the code. no errors, no data is being added to the sql, nothing is happening. So maybe a better programmer than be can have a look at my code and maybe see an issue that i currently can't!

Java code:

public static String main(String x, String y){

    try{

        String username = x;
        String password = y;

        URL url = new URL(login_url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);

        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

        String post_data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
                +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");

        bufferedWriter.write(post_data);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();

        // LÄSER FRÅN HEMSIDAN

        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
        String result="";
        String line="";
        while((line = bufferedReader.readLine()) != null){

            result += line;
        }




        bufferedReader.close();
        inputStream.close();
        httpURLConnection.disconnect();

        return result;

    }catch(Exception e){
        e.printStackTrace();
    }

    return null;

}

PHP

<body>


<?php
$db_name = "smoothie";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";

$usr = $_POST["username"];
$pass = $_POST["password"];

$conn = mysql_connect($server_name,$mysql_username,$mysql_password,$db_name);

$query = "INSERT INTO members (username, password)
VALUES ('$usr','$pass')";


?>

I could really use some help here!! :)

Janono
  • 161
  • 6
  • 2
    Well, for one, you never actually run your INSERT query. For a second, if you do continue with this query, expect your database to be destroyed (i.e. look up SQL injection, PDO and prepared statements ;) ). – Jonnix Sep 22 '16 at 21:53
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Sep 22 '16 at 22:01
  • Apart from the, I assume accidental, returning of `` you dont send anything back to the java program – RiggsFolly Sep 22 '16 at 22:02
  • 1
    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) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Sep 22 '16 at 22:02
  • @JonStirling well this is only for a school assignment so i dont think anybody will kill me if there is weakness in the security heheh :P but thanks for the info, gonna check if there still no errors after running the query :) – Janono Sep 22 '16 at 22:04
  • @RiggsFolly which one would you prefer then if you had to chose between pdo and myqli? :) – Janono Sep 22 '16 at 22:05
  • Personally I would pick PDO purely because I prefer the API – RiggsFolly Sep 22 '16 at 22:06
  • @RiggsFolly could you give me an example on how to send something back to the java program? – Janono Sep 22 '16 at 22:06
  • @RiggsFolly Hilarious picture of the kitten by the way hahahah – Janono Sep 22 '16 at 22:07
  • Well the simplest way is `echo 'OK';` – RiggsFolly Sep 22 '16 at 22:07
  • A better way is to use JSON i.e. `echo json_encode( array('status=> 'OK') );` – RiggsFolly Sep 22 '16 at 22:08
  • @RiggsFolly Okay gonna try and see what happens :) – Janono Sep 22 '16 at 22:08
  • But first remove the blank lines and `` from that script. Its just a piece of code and needs NO HTML – RiggsFolly Sep 22 '16 at 22:09
  • will do captain!! @RiggsFolly – Janono Sep 22 '16 at 22:10
  • @RiggsFolly i ran the query with mysqli_query($query,$conn) and still nothing happens except getting an error which says " Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\index.php on line 15 ok!". Any idea what's wrong? – Janono Sep 22 '16 at 22:17
  • okay found out the error hehe it's working now!! Thank u so much for the help! :) @RiggsFolly – Janono Sep 22 '16 at 22:20
  • Please [edit](http://stackoverflow.com/posts/39617564/edit) your previous question instead of reposting it. – The SE I loved is dead Sep 23 '16 at 00:01

0 Answers0