用PHP脚本将数据插入MySQL数据库不起作用 [英] Insert data into mysql db with php script doesnt work

查看:140
本文介绍了用PHP脚本将数据插入MySQL数据库不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,开发人员,

我正在尝试使用php脚本将Android应用程序中的数据插入MySql Server(在沼泽中运行).

I am trying to insert data from my Android Application into my MySql Server (running on wamp) with php script.

这些是我的php脚本:

Thhose are my php scripts:

<?php

define('hostname','localhost');
define('user','root');
define('password','');
define('databaseName','tutorial');

$connect = mysqli_connect(hostname,user,password,databaseName);

?>

AND

<?php

if($_SERVER["REQUEST_METHOD"]=="POST") {

    require 'connection.php';
    createStudent();

}

function createstudent(){

    global $connect;

    $firstname = $_POST["firstname"];
    $lastname = $_POST["lastname"];
    $age = $_POST["age"];

    $query = "insert into student(firstname,lastname,age) values ('$firstname','$lastname','$age');";

    mysqli_query($connect,$query) or die (mysqli_error($connect));
    mysqli_close($connect);

}

?>

这是我的android代码:

and this is my android code:

requestQueue = Volley.newRequestQueue(getApplicationContext());

                StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> parameters = new HashMap<String,String>();
                        parameters.put("firstname",et_firstname.getText().toString());
                        parameters.put("lastname",et_lastname.getText().toString());
                        parameters.put("age",et_age.getText().toString());

                        return parameters;

                    }
                };

                requestQueue.add(request);

让我说:我知道这是一个很简单的问题,但是我几乎是在我的想法的尽头,几周以来一直在寻找解决方案,如果有人可以帮助我,我会非常高兴,这也是因为我没有非常喜欢php,我帮不上忙.

Let me say: I know this is quiet a complex question but I am pretty much at the end of my ideas, searching for the solution since weeks and I would be so glad if someone could help me, also because I am not very much into php I can't help myself too much.

谢谢 你好 亚历克斯

编辑

当我在邮递员上运行脚本以填充数据时,出现这些错误:

When I run the script on Postman filling the data, those errors appear:

邮递员错误

推荐答案

解决方案

如果其他人有这个问题,我终于明白了问题所在:

I finally got what the problem was, if somebody else has this problem:

我正在运行前一段时间安装的另一台MySql Server,却忘了它.因此,这是在我的默认端口上运行的.我卸载了该服务器,并将真实"服务器的端口更改为默认端口.

I was running another MySql Server I installed some time ago and forgot about it. So this one was running on my default port. I deinstalled this server and changed the port of my "real" server to the default port.

无论如何,都要感谢所有试图帮助我的人,这帮助我找到了解决方案.

Anyways big thanks to everybody who tried to help me, which helped me get to the solution.

这篇关于用PHP脚本将数据插入MySQL数据库不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆