0

I have created a LAMP Digital Ocean droplet and have placed some PHP files in /var/www/html. I have been trying to test a simple Register.php with Postman but it always returns "Some error". Code below:

    <?php
    require("password.php");

    $host = 'localhost'; 
    $user = 'user';
    $pass = 'password';
    $db = 'db';

    $con=mysqli_connect($host,$user,$pass,$db);
    // Check connection
    if ($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    } 

    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $dob = $_POST['dob'];
    $gender = $_POST['gender'];

        global $connect, $name, $email, $password, $dob, $gender;
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);

        $statement = mysqli_prepare($con, "INSERT INTO User(NAME, EMAIL, PASSWORD, DOB, GENDER) VALUES(?,?,?,?,?)");
        mysqli_stmt_bind_param($statement, "sssss", $name, $email, $passwordHash, $dob, $gender);

        if ($result = $con->query($statement))
        { 
            echo json_encode("New record created successfully");
        } 
        else 
        {
            echo json_encode("Some error");

        }
?>

Is there an issue with my PHP file or are there some pre-requisites required before being able to POST to PHP files on Digital Ocean.

EDIT:

I have updated the PHP code to the following but am now getting nothing returned in Postman:

    require("password.php");

    $host = 'localhost'; 
    $user = 'user';
    $pass = 'password';
    $db = 'db';

    $con=mysqli_connect($host,$user,$pass,$db);
    // Check connection
    if ($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    } 

    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $dob = $_POST['dob'];
    $gender = $_POST['gender'];

        global $connect, $name, $email, $password, $dob, $gender;
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);


        $statement = $mysqli->prepare("INSERT INTO User(NAME, EMAIL, PASSWORD, DOB, GENDER) VALUES(?,?,?,?,?)");
        $statement->bind_param("sssss", $name, $email, $passwordHash, $dob, $gender););


        if ($result = $con->execute($statement))
        { 
            echo json_encode("New record created successfully");
        } 
        else 
        {
            echo json_encode("Some error");

        }

?>
Olis
  • 37
  • 1
  • 8
  • Do you have PHP error reporting on? If not, turn it on and see if it provides any more insight. – kchason Nov 20 '17 at 11:44
  • please check mysql mode may be it is in strict_mode. https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-strict. change mode it should work, until then only select queries will work, no new records will not create into table – jack brone Nov 20 '17 at 11:48
  • This has nothing to do with the host you are using, you need `execute()` to execute your query and not `query()`. There might be more problems though... – jeroen Nov 20 '17 at 11:56
  • Your syntax for the prepared statement is wrong. Check the most [basic prepared statement routine](https://phpdelusions.net/mysqli) and fix your code accordingly. there could be other errors though – Your Common Sense Nov 20 '17 at 11:56
  • Thanks for the replies, I have made what I think are the relevant edits but am now getting nothing returned by Postman – Olis Nov 20 '17 at 12:26
  • what is Postman? Did you configure PHP/mysqli to report errors? – Your Common Sense Nov 20 '17 at 12:27
  • https://www.getpostman.com/ It's used to test HTTP requests. I'm using it to test the PHP by posting the parameters with their values. – Olis Nov 20 '17 at 12:37
  • why don;t you just navigate to your page from the browser and see the outcome? Anyway, first of all you must make yourself familiar with [basic error reporting](https://phpdelusions.net/articles/error_reporting) and **get the actual error message**. Before that not a single soul will be able to help you – Your Common Sense Nov 20 '17 at 13:04

0 Answers0