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

标签 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';
}
}
?>

我得到的结果是:“无法注册”。即使我尝试使用 Android 代码或访问资源管理器上的 URL 来执行此操作。

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

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

非常感谢!

最佳答案

您在检查 POST 时发出 GET 请求,请将 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';
}
}
?>

关于PHP 尝试插入数据库 GET 方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44880281/

相关文章:

php - Wordpress/Woocommerce将帖子ID和订单ID偏移10000

php - JOIN 和 AND 后的订单查询

php - 如何通过 Graph API 指定 FB 帖子的位置定位

php - 使用 eloquent ORM 在 Laravel 中进行批量插入

android - 您应该使用数据库来存储视频还是视频 url?

android - 如何获取在 jni 中返回泛型类型的函数的方法 ID?

android - ORMLite 多对多关系

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows等…期望参数1为资源或结果

java - JDBC 准备语句 SQLSyntaxErrorException

php - MySQL - 复杂查询问题,多个表