尝试在数据库GET方法上插入PHP时出错

时间:2017-07-03 07:48:08

标签: php android mysql get

当我尝试在数据库中插入两个值时,我的PHP出现问题。

我正在使用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';
}
}
?>

我的结果是:&#34;无法注册&#34;。即使我尝试使用Android代码或访问资源管理器上的URL。

但是,当我在phpmyadmin上执行插入时,我没有错误,并且它可以正常工作。

我不知道为什么它没有插入,有人可以帮助我吗?

非常感谢!

3 个答案:

答案 0 :(得分:2)

您在检查POST时发出GET请求,将php的代码更改为此

from PIL import Image
import os, sys
import requests
from io import BytesIO

url = "https://cdn.discordapp.com/avatars/247096918923149313/34a66572b9339acdaa1dedbcb63bc90a.png?size=256"
response = requests.get(url)
pp = Image.open(BytesIO(response.content))
pp.save("image1.png")

答案 1 :(得分:0)

    <ScrollView
    android:layout_width="match_parent"
    android:fillViewport="true"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/li1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:text="Hellofgbjfdkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkhbhbghbuuuuuuuuuuuuuuuubfiiiiiiiiiibigyygfjjjjjjjjjjjjjjjjjjjjjjjjjj\nfjgggggggggggggggg\ngfggggggggggggggggggg\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\njgjijhijghijgfhjgfhjigfjhijgfhjgi
            jfdjgkfjd\nkkgkfgklfkgfg"
            android:layout_height="match_parent" />
    </LinearLayout>

</ScrollView>

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

答案 2 :(得分:0)

更改此

$ id = $ _POST [&#39; id&#39;]; $ points = $ _POST [&#39; points&#39;];

到此,

$ id = $ _GET [&#39; id&#39;]; $ points = $ _GET [&#39; points&#39;];