Insert Rest API PHP

api-insert.php: - 


<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization, X-Requested-With');


$data=json_decode(file_get_contents("php://input"), true);

$name=$data['sname'];
$age=$data['sage'];
$city=$data['scity'];

include "config.php";

$sql="INSERT INTO students(student_name, age, city) VALUES ('{$name}', '{$age}', '{$city}')";


if(mysqli_query($conn, $sql)){
    echo json_encode(array('message'=>'Student Record Inserted', 'status'=>true));

}else{
    echo json_encode(array('message'=>'Student Record Not Inserted', 'status'=>false));

}

?>



config.php:

<?php
$conn=mysqli_connect("localhost","root","","poetrydb") or die("Connection failed");

?>






Comments

Popular posts from this blog

Send Data Child to Parent Component - React Js

Hide , Show and Toggle in ReactJs

Importance Of Web Content In SEO.