2017-07-03 48 views
0

當我嘗試在數據庫中插入兩個值時,我遇到了PHP的問題。試圖插入數據庫的PHP錯誤GET方法

我正在使用Android,我打電話給php做插入。我張貼我的Android代碼:

private void loadPhase() { 
    String url = "http://www.myurl.com/scriptsfolder/myphptoinsertranking.php?id=prueba0101&points=200"; 

    if (numQuestion < 11) { 
     header4questions.setText(listQuestions.get(questionPosition)); 

     answer1.setText(listAnswers.get(questionPosition * 4)); 
     answer2.setText(listAnswers.get((questionPosition * 4) + 1)); 
     answer3.setText(listAnswers.get((questionPosition * 4) + 2)); 
     answer4.setText(listAnswers.get((questionPosition * 4) + 3)); 

     if(numQuestion % 5 == 0 && numQuestion > 0) 
     { 
      MediaPlayer mp = MediaPlayer.create(this, R.raw.aplausos); 
      mp.start(); 
     } 

     numQuestion++; 
    } 
    else { 
     Toast.makeText(getApplicationContext(), "You have finished the quiz!", Toast.LENGTH_SHORT).show(); 

     StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         Toast.makeText(QuizActivity.this,response,Toast.LENGTH_LONG).show(); 

         Intent inn1 = getIntent(); 
         inn1 = new Intent(QuizActivity.this, MainActivity.class); 
         startActivity(inn1); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(QuizActivity.this, "INSERT ERROR", Toast.LENGTH_LONG).show(); 

         Intent inn1 = getIntent(); 
         inn1 = new Intent(QuizActivity.this, MainActivity.class); 
         startActivity(inn1); 
        } 
       }) { 

     }; 

     //Adding the string request to the queue 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 
    } 
} 

我還張貼我的PHP代碼: 「無法註冊」:

<?php 
if($_SERVER['REQUEST_METHOD']=='GET'){ 
$id = $_POST['id']; 
$points = $_POST['points']; 

$con = mysqli_connect("server.com:1111","username","userpassword","databasename"); 
if (!$con->connect_error) 
{ 
    $sql= "INSERT INTO table (IdUser, Points) values ('$id','$points')"; 

    if(mysqli_query($con,$sql)){ 
     echo "Successfully Registered"; 
    } 
    else{ 
     echo "Could not register"; 
    } 
} 
else{ 
    echo 'error de conexion'; 
} 
} 
?> 

我已經是結果。即使我嘗試使用Android代碼或轉到資源管理器上的URL。

但是,當我在phpmyadmin上插入時,我沒有任何錯誤,它能正常工作。

我不知道爲什麼它不能插入,誰能幫助我嗎?

非常感謝!

+1

使用'$ _GET'而不是'$ _POST' –

回答

2

你是一個GET請求,同時檢查POST,PHP的代碼更改此

<?php 
if($_SERVER['REQUEST_METHOD']=='GET'){ 
$id = $_GET['id']; 
$points = $_GET['points']; 

$con = mysqli_connect("server.com:1111","username","userpassword","databasename"); 
if (!$con->connect_error) 
{ 
    $sql= "INSERT INTO table (IdUser, Points) values ('$id','$points')"; 

    if(mysqli_query($con,$sql)){ 
     echo "Successfully Registered"; 
    } 
    else{ 
     echo "Could not register"; 
    } 
} 
else{ 
    echo 'error de conexion'; 
} 
} 
?> 
+0

好吧,我會嘗試。你有沒有看到更多的錯誤o這可以解決它? – Imrik

+0

直到現在多數民衆贊成在我能看到的錯誤,希望這將工作一切 – Exprator

+0

謝謝!我試着告訴你是否解決了這個問題。 – Imrik

0
if($_SERVER['REQUEST_METHOD']=='GET'){ //that's get method 
    $id = $_POST['id']; // here you are getting value from $_POST 

應該$_GET

+0

好的,我會盡力的。你有沒有看到更多的錯誤o這可以解決它? – Imrik

+0

試一下,讓我知道是否仍然有錯誤。 –

0

改變這種

的$ id = $ _POST [ 'ID']; $ points = $ _POST ['points'];

於此,

$ ID = $ _GET [ 'ID']; $ points = $ _GET ['points'];

+0

好的,我會嘗試。你有沒有看到更多的錯誤o這可以解決它? – Imrik