I am having some difficulties trying to set up a connection to my database with php... I have tried so many things, double-checked my SQL queries, and just don't see why it is not working... I'm still a newbie, so I guess I'm missing something out of my range yet. I am trying to create an app that will take user registration.
This is the error I'm getting in Android Studio:
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
My php code is:
<?php
$servername = "my server here";
$username = "my username here";
$password = "my password here";
$dbname = "my db here";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO User (username, email, passcode) VALUES (?, ?, ?)";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Another important point, checking in the Postman plugin and verifying the url I get this:
Error: INSERT INTO User (username, email, passcode) VALUES (?, ?, ?)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?)' at line 1
I don't really understand why that is an error? Since I am expecting input from the user in the form...
As in of more information, this is the code from my RegisterActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText etUsername = (EditText)findViewById(R.id.etUsername);
final EditText etEmail = (EditText)findViewById(R.id.etEmail);
final EditText etPassword = (EditText)findViewById(R.id.etPassword);
final Button btnRegister = (Button)findViewById(R.id.btnRegister);
if (btnRegister != null) {
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username = etUsername.getText().toString();
final String email = etEmail.getText().toString();
final String passcode = etPassword.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(username, email, passcode, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
So, does anyone know where's this coming from and where else should I look into?
Cheers!