【问题标题】:PHP error trying to insert on database GET Method尝试在数据库 GET 方法上插入的 PHP 错误
【发布时间】:2017-07-03 07:48:07
【问题描述】:

当我尝试在数据库中插入两个值时,我的 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 上进行插入时,我没有任何错误,并且可以正常工作。

我不知道为什么它不能插入,请任何人帮助我吗?

非常感谢!

【问题讨论】:

  • 使用$_GET而不是$_POST

标签: php android mysql get


【解决方案1】:

您在检查 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';
}
}
?>

【讨论】:

  • 好的,我会试试的。你觉得还有什么问题可以解决吗?
  • 直到现在这就是我看到的错误,希望这能解决所有问题
【解决方案2】:
 if($_SERVER['REQUEST_METHOD']=='GET'){  //that's get method
  $id = $_POST['id']; // here you are getting value from $_POST

应该是$_GET

【讨论】:

  • 好的,我会试试的。你觉得还有什么问题可以解决吗?
【解决方案3】:

改一下

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

到这里,

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

【讨论】:

  • 好的,我会试试的。你觉得还有什么问题可以解决吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 2011-03-20
相关资源
最近更新 更多