2

I am trying to make a HttpPost from my app to a php script, which will then insert data to a remote database. I'm not getting any runtime errors, but the data never gets inserted into my remote database. Code:

public class InsertResult extends AsyncTask<String, String, String>
    {
        @Override
        protected String doInBackground(String... arg0)
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.<myDomain>.co.za/insertTeamResultwithCode.php");

            try
            {
                ArrayList<NameValuePair> nameValues = new ArrayList<NameValuePair>(10);
                nameValues.add(new BasicNameValuePair("Code", password));
                nameValues.add(new BasicNameValuePair("Section", section));
                nameValues.add(new BasicNameValuePair("Gender", gender));
                nameValues.add(new BasicNameValuePair("WinningTeam", newResult.getWinningTeam()));
                nameValues.add(new BasicNameValuePair("LosingTeam", newResult.getLosingTeam()));
                nameValues.add(new BasicNameValuePair("FixtureD", newResult.getDate()));
                nameValues.add(new BasicNameValuePair("FixtureT", newResult.getTime()));
                nameValues.add(new BasicNameValuePair("Venue", newResult.getVenue()));
                nameValues.add(new BasicNameValuePair("Court", newResult.getCourt()));
                nameValues.add(new BasicNameValuePair("Texts", newResult.getText()));

                httppost.setEntity(new UrlEncodedFormEntity(nameValues));

                HttpResponse response = httpclient.execute(httppost);
            }
            catch (ClientProtocolException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result)
        {
            GoToJarvisDashboard();
        }
    }

And PhP script:

<?php
//Code verifications
$Code = $_GET['Code'];

//Variables for SQL INSERT
$Section = $_GET['Section'];
$Gender = $_GET['Gender'];
$WinningTeam = $_GET['WinningTeam'];
$LosingTeam = $_GET['LosingTeam'];
$FixtureD = $_GET['FixtureD'];
$FixtureT = $_GET['FixtureT'];
$Venue = $_GET['Venue'];
$Court = $_GET['Court'];
$Texts = $_GET['Texts'];
$SetsWon = $_GET['SetsWon'];
$SetsLost = $_GET['SetsLost'];
$Winner_Score = $_GET['Winner_Score'];
$Loser_Score = $_GET['Loser_Score'];

$dbname = '<dbName>';
$dbuser = '<user>';
$dbpass = '<password>';
$dbhost = '<host>t';
$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die ("Unable to Connect to '$dbhost'");

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$SQL = "SELECT * FROM squasdlw_db1.validationCodes WHERE Code = '$Code'";
$codeValid = mysqli_query($connect, $SQL) or die (mysqli_error($connect));

$response["no"] = array();

if (mysqli_num_rows($codeValid) > 0) 
{
    echo "Code is valid\n\n";

    //Insert Result
    $query = "INSERT INTO squasdlw_db1.jarvisTeamResults (Section,Gender,WinningTeam,LosingTeam,FixtureD,FixtureT,Venue,Court,Texts,SetsWon,SetsLost,Winner_Score,Loser_Score) VALUES('$Section','$Gender','$WinningTeam','$LosingTeam','$FixtureD','$FixtureT','$Venue','$Court','$Texts','$SetsWon','$SetsLost','$Winner_Score','$Loser_Score')";             
    $result = mysqli_query($connect, $query) or die (mysqli_error($connect));
    echo "Result successfully submitted to Jarvis Remote Database: Table -> jarvisTeamResults\n\n"  ;   

    //Update Winner on jarvisLogs
    $query = "UPDATE squasdlw_db1.jarvisLogs SET Points = Points + '$Winner_Score', Played = Played + 1, Won = Won + 1 WHERE Gender = '$Gender' AND Section = '$Section' AND Team = '$WinningTeam'";    
    $result = mysqli_query($connect, $query) or die (mysqli_error($connect));
    echo "Log update for '$WinningTeam': Table -> jarvisLogs\n\n";

    //Update Winner on jarvisLogs
    $query = "UPDATE squasdlw_db1.jarvisLogs SET Points = Points + '$Loser_Score', Played = Played + 1, Lost = Lost + 1 WHERE Gender = '$Gender' AND Section = '$Section' AND Team = '$LosingTeam'";    
    $result = mysqli_query($connect, $query) or die (mysqli_error($connect));
    echo "Log update for '$LosingTeam': Table -> jarvisLogs\n\n";

    $response["isValid"] = true;
    echo json_encode($response);
}
else
{
    $response["isValid"] = false;
    echo "The code is not valid\n";
    echo json_encode($response);
}

mysqli_close($connect);

?>

Whats even weirder is that if I try this in a web browser, it works fine and the data does get added to the remote database-

http://www.<myDomain>.co.za/insertTeamResultwithCode.php?Code=<password>&Section=A&Gender=Men&WinningTeam=NW&LosingTeam=KZN&FixtureD=2015-05-25&FixtureT=09:00:00&Venue=Puk&Court=6&Texts=Andrew%20(NW)%20beat%20Kevin%20(WP)%203-1&SetsWon=15&SetsLost=5&Winner_Score=15&Loser_Score=5

Please help out, I have no idea what is going wrong because it does not create runtime errors? I am about to go crazy...

Code Vader
  • 649
  • 3
  • 8
  • 22

2 Answers2

1

You are doing an HttpPost so you should get your data via POST and not GET

//Code verifications
$Code = $_POST['Code'];

//Variables for SQL INSERT
$Section = $_POST['Section'];
$Gender = $_POST['Gender'];
$WinningTeam = $_POST['WinningTeam'];
$LosingTeam = $_POST['LosingTeam'];
$FixtureD = $_POST['FixtureD'];
$FixtureT = $_POST['FixtureT'];
$Venue = $_POST['Venue'];
$Court = $_POST['Court'];
$Texts = $_POST['Texts'];
$SetsWon = $_POST['SetsWon'];
$SetsLost = $_POST['SetsLost'];
$Winner_Score = $_POST['Winner_Score'];
$Loser_Score = $_POST['Loser_Score'];

Or change your android code to make GET requests :

HttpGet httpget = new HttpGet("http://www.example.com/insertTeamResultwithCode.php");
β.εηοιτ.βε
  • 16,236
  • 11
  • 41
  • 53
0

You are sending POST in android BUT you are expecting GET in php which are not the same.

Change the $_GET to $_POST and it will work.

Here a post that will tell you difference between then

Here another with some more details

Community
  • 1
  • 1
Misters
  • 1,281
  • 2
  • 16
  • 28