使用PHP将数据库连接到angular以插入值。
我对PHP不熟悉。从头开始解释细节。
任何一种方法都不适合我
<?php
require 'connect.php';
// Get the posted data.
$postdata = file_get_contents("php://input");
if(isset($postdata) && !empty($postdata))
{
// Extract the data.
$request = json_decode($postdata);
// Sanitize.
$fullname = mysqli_real_escape_string($con, trim($request->data->fullname));
$email = mysqli_real_escape_string($con, trim($request->data->email));
$phonenumber = mysqli_real_escape_string($con, trim($request->data->phonenumber));
$dob = mysqli_real_escape_string($con, trim($request->data->dob));
$gender = mysqli_real_escape_string($con, trim($request->data->gender));
$nationality = mysqli_real_escape_string($con, trim($request->data->nationality));
$mothertongue = mysqli_real_escape_string($con, trim($request->data->mothertongue));
$photograph = mysqli_real_escape_string($con, trim($request->data->photograph));
// Store.
$sql = "INSERT INTO `loyola_personal_info`(`id`,`fullname`,`email`,`phonenumber`,`dob`,`gender`,`nationality`,`mothertongue`,`photograph`) VALUES (null,'{$fullname}','{$email}','{$phonenumber}', '{$dob}','{$gender},'{$nationality}','{$mothertongue}','{$photograph}')";
if(mysqli_query($con,$sql))
{
$personal = [
'fullname' => $fullname,
'email' => $email,
'phonenumber' => $mobile,
'dob' => $dob,
'gender' => $gender,
'nationality' => $nationality,
'mothertongue' => $mothertongue,
'photograph' => $photograph,
'id' => mysqli_insert_id($con)
];
echo json_encode(['data'=>$personal]);
}
else
{
http_response_code(422);
}
}
错误:
SyntaxError:意外的令牌<在JSON中,位于XMLHttpRequest.url的JSON.parse()位置0处