0

So I am trying to do a basic registration activity by using an android platform to retrieve values from a user and register the users credentials in a basic mySQL database. When I run my responseListener the register.php page is returning Server error 500 so I am led to believe my php script has a flaw.

register.php

<?php
error_reporting(-1); // reports all errors
ini_set("display_errors", "1"); // shows all errors
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
try{
    $con = mysqli_connect("con","user","pass","db"); 
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        $email = mysqli_real_escape_string($con,$_POST['email']);
        $username = mysqli_real_escape_string($con,$POST['username']);
        $name = mysqli_real_escape_string($con,$_POST['name']);
        $password = mysqli_real_escape_string($con,$_POST['password']);
        $response = array();
        $sql = "Select * from Registration where email = '$email' or username = '$username'";
        $result = mysqli_query($con, $sql);
        $user = mysqli_fetch_assoc($result);
        if(($user['username'] === $username) or ($user['email'] === $email)){
            $response["success"] = false;
            }
        else{
            $myhash = password_hash($password, PASSWORD_DEFAULT);
            $sql1 = "insert into Registration (email, username, name, password) values('$email', '$username', '$name', '$myhash')";
            $insert = mysqli_query($con, $sql1);
            $validation = mysqli_num_rows($insert);
            if($validation == 1){
                $response["success"]= true;
                }
            else{
                $response["success"]= false;
                }
                }
        echo json_encode($response);
        die($con);
        }
    }
    catch(error $e){
        $response = array();
        $reponse["success"] = false;
        $response["error"] = $e;
        echo json_encode($response);
        die($con);
        }
?>

Thanks for any help

user3783243
  • 5,368
  • 5
  • 22
  • 41
Diggy
  • 1
  • So what does PHP's error reporting say? – Marvin Dec 10 '18 at 00:35
  • What do you mean by "not working"? Do you get any errors? Add [error reporting](//php.net/manual/function.error-reporting.php) at the top of your file(s): `ini_set("display_errors", 1); error_reporting(E_ALL);` and tell us what you get. – Blue Dec 10 '18 at 00:38
  • My problem is that the java message won't convert into a JSONResponse. It is giving the error message of "Errororg.json.JSONException:Value
    – Diggy Dec 10 '18 at 00:49
  • 1
    Possible duplicate of [How to log errors and warnings into a file?](https://stackoverflow.com/questions/3531703/how-to-log-errors-and-warnings-into-a-file) – miken32 Dec 10 '18 at 01:04
  • The mysqli extension is pretty minimal, it really just existed as a) a stopgap measure to replace the old mysql extension, and b) to be used as a base to build a higher-level API. Of note here is that it does not throw exceptions like a modern database API would, so your try/catch statements do nothing. In my opinion, PDO, is always the better choice. Also, first parameter to the mysqli constructor is a hostname or localhost. What is "con"? – miken32 Dec 10 '18 at 01:08
  • Why do you have `die($con);`? `$con` is a connect, no? I don't know what that would output but i imagine an error – user3783243 Dec 10 '18 at 01:08

0 Answers0