2017-07-03 50 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