-1

I'm working on a register page and i got this problems(as what is shown in the picture)[screenshot of the problem]: https://i.stack.imgur.com/q3rA0.jpg. I'm new to php and online database so i hope you can bear with me. I'm using xampp as my server.

Here's my android studio code:

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

}

And here's my php code:

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

?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shinjid
  • 35
  • 4
  • Double check if your credentials are correct. I think xampp usually doesn't have a password for root. Check if you can connect to the database on your computer. –  Mar 04 '20 at 07:06
  • Thanks, you're right about the root as password, i removed it then it works. But know it fails and say's try again(as what is in echo). Does it mean it can't store my data in database? – Shinjid Mar 04 '20 at 07:12
  • Your code is vulnerable to SQL injection. You should use prepared statements. You need to fix it immediately. – Dharman Mar 04 '20 at 09:37
  • @Dharman How do I fix it? The SQL injection? – Shinjid Mar 04 '20 at 12:13

1 Answers1

1

I fixed it, thanks for the reply guys. I have a UserID primary key with autoincrement in my first column of my database, that's why the array doesn't fit the insert into database. I had to put null before my values so now it works.

Shinjid
  • 35
  • 4