Insert data into mysql db with php script doesnt w

2019-08-31 09:15发布

Hello developers out there,

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

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);

}

?>

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);

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.

Thank you greetings Alex

EDIT

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

Postman error

4条回答
Anthone
2楼-- · 2019-08-31 09:37

Can you access PhpMyAdmin using a browser? The error points out that the database was not accessible using your PHP application.

If you can't, make sure it is properly installed and is working.

If you can, make sure mysqli driver is installed. See this question to see if you have mysqli driver was installed and configured.

查看更多
The star\"
3楼-- · 2019-08-31 09:40

SOLUTION

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

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.

查看更多
对你真心纯属浪费
4楼-- · 2019-08-31 09:42

Check your api first by running it on some online website and check if your db name is correct by simply inserting some data through a php script running on web and if problem not solve it might be problem in your connection of db.

查看更多
叼着烟拽天下
5楼-- · 2019-08-31 09:50

The reason is the connection.

The correct syntax is

$con = mysqli_connect("localhost","my_user","my_password","my_db");

So, it must be

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

remove the empty parenthesis

查看更多
登录 后发表回答