当前位置: 首页 > 知识库问答 >
问题:

拒绝用户根的访问;@localhost'(使用密码:yes)和mysqli_query()要求参数1为mysqli,bool给定[duplicate]

巴帅
2023-03-14

这是我的android studio代码:

public class MainActivity extends AppCompatActivity {

// Creating EditText.
EditText FirstName, LastName, Email ;

// Creating button;
Button InsertButton;

// Creating Volley RequestQueue.
RequestQueue requestQueue;

// Create string variable to hold the EditText Value.
String FirstNameHolder, LastNameHolder, EmailHolder ;

// Creating Progress dialog.
ProgressDialog progressDialog;

// Storing server url into String variable.
String HttpUrl = "http://192.168.254.254:81/connection/insert_record.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Assigning ID's to EditText.
    FirstName = (EditText) findViewById(R.id.editTextFirstName);
    LastName = (EditText) findViewById(R.id.editTextLastName);
    Email = (EditText) findViewById(R.id.editTextEmail);

    // Assigning ID's to Button.
    InsertButton = (Button) findViewById(R.id.ButtonInsert);

    // Creating Volley newRequestQueue .
    requestQueue = Volley.newRequestQueue(MainActivity.this);

    progressDialog = new ProgressDialog(MainActivity.this);

    // Adding click listener to button.
    InsertButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // Showing progress dialog at user registration time.
            progressDialog.setMessage("Please Wait, We are Inserting Your Data on Server");
            progressDialog.show();

            // Calling method to get value from EditText.
            GetValueFromEditText();

            // Creating string request with post method.
            StringRequest stringRequest = new StringRequest(Request.Method.POST, HttpUrl,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String ServerResponse) {

                            // Hiding the progress dialog after all task complete.
                            progressDialog.dismiss();

                            // Showing response message coming from server.
                            Toast.makeText(MainActivity.this, ServerResponse, Toast.LENGTH_LONG).show();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {

                            // Hiding the progress dialog after all task complete.
                            progressDialog.dismiss();

                            // Showing error message if something goes wrong.
                            Toast.makeText(MainActivity.this, volleyError.toString(), Toast.LENGTH_LONG).show();
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {

                    // Creating Map String Params.
                    Map<String, String> params = new HashMap<String, String>();

                    // Adding All values to Params.
                    params.put("first_name", FirstNameHolder);
                    params.put("last_name", LastNameHolder);
                    params.put("email", EmailHolder);


                    return params;
                }

            };

            // Creating RequestQueue.
            RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

            // Adding the StringRequest object into requestQueue.
            requestQueue.add(stringRequest);

        }
    });

}

// Creating method to get value from EditText.
public void GetValueFromEditText(){

    FirstNameHolder = FirstName.getText().toString().trim();
    LastNameHolder = LastName.getText().toString().trim();
    EmailHolder = Email.getText().toString().trim();

}

下面是我的php代码:

insert_record.php

<?php

include 'DatabaseConfig.php';

$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

$f_name = $_POST['first_name'];
$l_name = $_POST['last_name'];
$email = $_POST['email'];

$Sql_Query = "insert into UserInfo (first_name,last_name,email) values 
('$f_name','$l_name','$email')";

if(mysqli_query($con,$Sql_Query)){

echo 'Data Inserted Successfully';

}
else{

echo 'Try Again';

}
mysqli_close($con);
?>

dataConfig.php

<?php
$HostName = "localhost";
$HostUser = "root";
$HostPass = "root";
$DatabaseName = "users";

?>

共有1个答案

戴霖
2023-03-14

我修好了,谢谢你的回复。我的数据库的第一列中有一个带有autoincrement的UserID主键,这就是为什么数组不适合插入数据库的原因。我必须把空放在我的值之前,所以现在它起作用了。

 类似资料: