2

I developing a small crud application with php and restful api. I want to sent the code 200 if a record is created and code 403 for error msgs. Below is my code:

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];

$query = $restObj->create_record($firstname,$lastname,$age);
echo json_encode([
      "message" => $query ? "User Created" : "Error creating user"
]);

This is my function for creating record:

public function create_record($firstname,$lastname,$age)
{   
$sql = "INSERT INTO table1 (firstname,lastname,age) VALUES ('$firstname','$lastname','$age')";
$query = mysqli_query($this->conn,$sql);    
return $query;  
}

The code works fine and I can create record but I don't know how to display code 200 and code 403 messages. Please Help

Ajmal Razeel
  • 1,663
  • 7
  • 27
  • 51
  • 2
    Your code is wide open to [SQL injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Machavity Nov 06 '19 at 00:32

1 Answers1

0

You can use http_response_code(200) to get and set the HTTP response code.

Refer to PHP Manual: http-response-code

LF00
  • 27,015
  • 29
  • 156
  • 295