0

I've been trying to use a simple POST request to add some basic information to a MySQL database, but I don't seem to be having much luck - it won't add the tuple to the database. I know this code runs the PHP file it requests OK as if I tell the PHP file to add dummy info to the database it works OK, but the information in the POST string does not get added.

Here is my Java file:

public class Test {

public static void main(String[] args) {

    int id = 3696;
    String name = "David";
    int steps = 6989;

    String strData = encode(id, name, steps);
    submit(strData);
}

public static String encode(int id, String name, int steps) {

    String strToSend;

    try
    {
        strToSend = URLEncoder.encode("id", "UTF-8") + "=";
        strToSend += URLEncoder.encode("" + id, "UTF-8");
        strToSend += "&" + URLEncoder.encode("name", "UTF-8") + "=";
        strToSend += URLEncoder.encode(name, "UTF-8");
        strToSend += "&" + URLEncoder.encode("steps", "UTF-8") + "=";
        strToSend += URLEncoder.encode("" + steps, "UTF-8");
    }

    catch (Exception e) {
        return "Error encoding data for submission";
    }

    return strToSend;
}

public static String submit(String strToSend) {

    URL urlDest;
    String strData = strToSend;
    System.out.println(strData);
    HttpURLConnection ucConnect;

    try 
    {
        urlDest = new URL("http://localhost/~David/posttest.php");
    }
    catch (Exception e)
    {
        return "Error in submitting entry";
    }


    try
    {
        ucConnect = (HttpURLConnection) urlDest.openConnection();
        ucConnect.setRequestProperty("Content-type","application/x-www-form-urlencoded");
        ucConnect.setRequestMethod("POST");
        ucConnect.setDoOutput(true);

    }

    catch (Exception e)
    {
        return "Error in connecting to URL";
    }

    try
    {
        OutputStreamWriter streamWrite = new OutputStreamWriter(ucConnect.getOutputStream());
        streamWrite.write(strData);
        System.out.println(streamWrite.toString());
        streamWrite.flush();

        //Get the response

        ucConnect.getInputStream();
        System.out.println(ucConnect);
        streamWrite.close();
    }

    catch (Exception e) 
    {
        return "Problem";
    }

    System.out.println("SUCCESS");
    return "Yay!";
}

}

And here's posttest.php

<?php

$con=mysqli_connect(removed);

//check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$id = $_POST["id"];
$strName = $_POST["name"];
$noSteps = $_POST["steps"];

mysqli_query ($con, "INSERT INTO People VALUES ($id, $strName, $noSteps)" || die();

mysqli_close($con);
echo json_encode($arr);

?>

The printed format of the string I'm sending is:

id=3696&name=David&steps=6989

Thanks for the help.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • 3
    This type of request `id=3696&name=David&steps=6989` is a `GET` and not a `POST` – Funk Forty Niner Oct 30 '13 at 23:11
  • Do a `var_dump($_POST);` to see whether you are really sending the data. Also if the data is going to be user supplied later you have SQL injection issues to worry about. – PeeHaa Oct 30 '13 at 23:14
  • 1
    @SteAp - it's not up to you to modify OP's code. Please revert. – Funk Forty Niner Oct 30 '13 at 23:14
  • @SteAp As previously stated, it's not your/our job to modify a question's "code". Spelling mistakes fine, but if it so happens that the OP were to re-modify, then what? You could have just pointed out the mistake. – Funk Forty Niner Oct 30 '13 at 23:17
  • This line `|| die();` is missing an ending `)` which should read as `|| die());` unless it was a typo. – Funk Forty Niner Oct 30 '13 at 23:19
  • 1
    @SteAp Another reason as to why not to modify an OP's code is; say for instance the error was in fact what you modified, then someone else comes along, looks at the code and says, "There's nothing wrong with your code". Then possibly downvotes the question or votes to close it. Now do you understand why? – Funk Forty Niner Oct 30 '13 at 23:21
  • 1
    @SteAp When editing code in questions you have to be careful you don't edit the problem out and leave everyone in a WTF state. – PeeHaa Oct 30 '13 at 23:22
  • Yes, that was an error. I fixed the dodgy syntax. How do I go about finding out the results of a var_dump($_POST);? I run the PHP file when the Java file executes. sorry if that's a stupid question, I'm new to all of this. – David Mcinnes Oct 30 '13 at 23:22
  • You can log it or return it to the client – PeeHaa Oct 30 '13 at 23:23
  • @DavidMcinnes I'm curious. How is `$arr` defined? You have it in `echo json_encode($arr);` but I don't see any other mention of it anywhere else in your (posted) code. Plus [have a look at this answer on SO](http://stackoverflow.com/a/2525686/1415724) might be useful. – Funk Forty Niner Oct 31 '13 at 00:00

1 Answers1

1

The line

mysqli_query ($con, "INSERT INTO People VALUES ($id, $strName, $noSteps)" 
  || die();

is syntactically incorrect. Please include a ) at the end of mysqli_query:

mysqli_query ($con, "INSERT INTO People VALUES ($id, $strName, $noSteps)" ) 
  || die();
SteAp
  • 11,853
  • 10
  • 53
  • 88