如何使用php从Android向mysql数据库插入数据?

时间:2022-09-25 19:01:31

I need to insert some data into my MySQL database but I don't understand why my code doesn't work.

我需要将一些数据插入我的MySQL数据库,但我不明白为什么我的代码不起作用。

I create the java code for android and a php file which help me to insert the data into the db.

我创建了android的java代码和一个php文件,它帮助我将数据插入到db中。

I passed two parameters, email and password, beacuse I need to find an user's id.

我传递了两个参数,电子邮件和密码,因为我需要找到用户的ID。

RegisterBody.java

RegisterBody.java

    private void registerUser() {
        String waist = etWaist.getText().toString();
        String hips = etHips.getText().toString();
        String breast = etBreast.getText().toString();
        String wrist = etWrist.getText().toString();
        String weight = etWeight.getText().toString();
        String height = etHeight.getText().toString();
        register(waist, hips, breast, wrist, weight, height);
    }

    private void register(String waist, String hips, String breast, String wrist, String weight, String height) {
        class RegisterUser extends AsyncTask<String, Void, String> {

            ProgressDialog loading;
            RegisterUserClass ruc = new RegisterUserClass();

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

                loading = ProgressDialog.show(RegisterBody.this, "Please wait...", null, true, true);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
            }

            @Override
            protected String doInBackground(String... params) {
                HashMap<String, String> data = new HashMap<String, String>();
                data.put("waist", params[0]);
                data.put("hips", params[1]);
                data.put("breast", params[2]);
                data.put("wrist", params[3]);
                data.put("weight", params[4]);
                data.put("height", params[5]);
                data.put("email", email);
                data.put("password", password);

                String result = ruc.sendPostRequest(REGISTER_URL, data);
                return result;
            }
        }
        RegisterUser ru = new RegisterUser();
        ru.execute(waist,hips,breast,wrist,weight,height, email, password);
        Bundle b=new Bundle();
        b.putStringArray(null, new String[]{name, surname, age, email, password,waist,hips,breast,wrist,weight,height});
        Intent in=new Intent(this,ChooseDiet.class);
        in.putExtras(b);
        startActivity(in);
    }

}

RegisterBody.php

RegisterBody.php

    $conn = mysqli_connect("localhost","root","","sfitness") or die("Error " . mysqli_error($conn));


    $sql="SELECT ID_Person FROM Person WHERE Email='$email' AND Password='$password'";

    $id_person=mysqli_query($conn,$sql);
    if (!$sql)
    {
            echo 'Could not run query:'.mysql_error();
            exit;
    }
    $id_person = mysqli_fetch_row($sql);


    $query ="INSERT INTO Body (Waistline, Hips, Breast, Wrists, Weight, Height, ID_person) values(?,?,?,?,?,?,?)";

    $stmt = mysqli_prepare($query);
    if ( $stmt === FALSE ) {
        mysqli_error($conn);
        exit;
    }
    $stmt->bind_param('ssssssi', '$waist','$hips','$breast','$wrist','$weight','$height','$id_person');

    $result = mysqli_execute($stmt);
    if ( $result === FALSE ){
        echo $mysqli_stmt_error($stmt);
        exit;
    }

What is wrong? What part of my code doesn't work? I don't understand what I wrong into my codes.

哪里不对?我的代码的哪一部分不起作用?我不明白我的错误在我的代码中。

1 个解决方案

#1


0  

This isn't going to work:

这不起作用:

$conn = mysqli_connect("localhost","root","","sfitness") or die("Error " . mysqli_error($conn));

If the connection attempt fails, then $conn will be a boolean false, which you CAN'T use for mysqli_error(). The error function requires a valid connection handle, so it's a chicken/egg situation. That's why there's mysqli_connect_error(), which is explicitly designed to return errors from a connection attempt, WITHOUT requiring a connection handle.

如果连接尝试失败,那么$ conn将是一个布尔值false,你不能用于mysqli_error()。错误函数需要一个有效的连接句柄,所以它是鸡/蛋的情况。这就是mysqli_connect_error()的原因,它明确地设计为从连接尝试返回错误,而不需要连接句柄。

Beyond that, you have multiple other problems:

除此之外,您还有其他多个问题:

  1. where are $password and $username being defined? as well as $waist, $hip, etc...? NOne of those are defined anywhere - you just start using them. Either they're set up in code which you haven't shown, you're operating under the false assumption that register_globals is still a thing...
  2. 在哪里定义$ password和$ username?还有腰围,臀部等...?这些都是在任何地方定义的 - 你只是开始使用它们。要么它们是在你没有展示的代码中设置的,那么你的假设是register_globals仍然是一个东西......
  3. Exactly what is $mysqli_stmt_error($stmt) trying to accomplish? Again that's an undefined variable, maybe you meant it to be just mysqli_stmt_error(...) - a function call, not a variable function.
  4. 究竟是什么$ mysqli_stmt_error($ stmt)试图完成?再一次,这是一个未定义的变量,也许你的意思是它只是mysqli_stmt_error(...) - 一个函数调用,而不是一个变量函数。
  5. You need to learn basic PHP strings: '$waist' will also NOT work. '-quoted strings do not interpolate variables, so you're trying to insert the literal characters $, w, a, etc...
  6. 你需要学习基本的PHP字符串:'$ waist'也不行。 ' - 引用字符串不插入变量,所以你试图插入文字字符$,w,a等...

#1


0  

This isn't going to work:

这不起作用:

$conn = mysqli_connect("localhost","root","","sfitness") or die("Error " . mysqli_error($conn));

If the connection attempt fails, then $conn will be a boolean false, which you CAN'T use for mysqli_error(). The error function requires a valid connection handle, so it's a chicken/egg situation. That's why there's mysqli_connect_error(), which is explicitly designed to return errors from a connection attempt, WITHOUT requiring a connection handle.

如果连接尝试失败,那么$ conn将是一个布尔值false,你不能用于mysqli_error()。错误函数需要一个有效的连接句柄,所以它是鸡/蛋的情况。这就是mysqli_connect_error()的原因,它明确地设计为从连接尝试返回错误,而不需要连接句柄。

Beyond that, you have multiple other problems:

除此之外,您还有其他多个问题:

  1. where are $password and $username being defined? as well as $waist, $hip, etc...? NOne of those are defined anywhere - you just start using them. Either they're set up in code which you haven't shown, you're operating under the false assumption that register_globals is still a thing...
  2. 在哪里定义$ password和$ username?还有腰围,臀部等...?这些都是在任何地方定义的 - 你只是开始使用它们。要么它们是在你没有展示的代码中设置的,那么你的假设是register_globals仍然是一个东西......
  3. Exactly what is $mysqli_stmt_error($stmt) trying to accomplish? Again that's an undefined variable, maybe you meant it to be just mysqli_stmt_error(...) - a function call, not a variable function.
  4. 究竟是什么$ mysqli_stmt_error($ stmt)试图完成?再一次,这是一个未定义的变量,也许你的意思是它只是mysqli_stmt_error(...) - 一个函数调用,而不是一个变量函数。
  5. You need to learn basic PHP strings: '$waist' will also NOT work. '-quoted strings do not interpolate variables, so you're trying to insert the literal characters $, w, a, etc...
  6. 你需要学习基本的PHP字符串:'$ waist'也不行。 ' - 引用字符串不插入变量,所以你试图插入文字字符$,w,a等...