2015-05-29 86 views
2

我想從我的應用程序到一個PHP腳本,然後將數據插入到遠程數據庫的HttpPost。我沒有收到任何運行時錯誤,但數據永遠不會被插入到我的遠程數據庫中。 代碼:HttpPost不工作​​在Android

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(); 
     } 
    } 

和PHP腳本:

<?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); 

?> 

請告訴我更加古怪的是,如果我嘗試這在web瀏覽器,它工作正常,數據不會被添加到遠程數據庫 -

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 

請幫幫忙,我不知道什麼問題,因爲它不會創建運行時錯誤?我即將發瘋...

回答

1

你正在做一個HttpPost所以你應該通過POST讓您的數據,而不是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']; 

或更改你的Android的代碼,使GET請求:

HttpGet httpget = new HttpGet("http://www.example.com/insertTeamResultwithCode.php"); 
+0

我愛你!如果我可以給你50 ++的選票,我會發誓 –