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");
}
?>