我是PHP新手。我在我的项目中使用下面的代码,但是它不起作用,但是没有引发任何错误。我试图将其转换为GET方法,但仍然无法正常工作。
PHP是我的前端,而MySQL数据库是后端。
我已经在后端创建了表,并检查了用户表,但没有插入值。
尝试的方法:
我已经使用insert命令手动插入了数据,并且工作正常。
API调用
http://localhost/test/register.php?phone=12345&name=Smith&birthdate=1974-12-01&address=7th Avenue
错误:
{"error_msg":"Required parameter (phone,name,birthdate,address) is missing!"}
Register.php
<?php
require_once 'db_functions.php';
$db=new DB_Functions();
$response=array();
if(isset($_POST['phone']) &&
isset($_POST['name']) &&
isset($_POST['birthdate']) &&
isset($_POST['address']))
{
$phone=$_POST['phone'];
$name=$_POST['name'];
$birthdate=$_POST['birthdate'];
$address=$_POST['address'];
if($db->checkExistsUser($phone))
{
$response["error_msg"]="User already exists with" .$phone;
echo json_encode($response);
}
else
{
//Create new user
$user=$db->registerNewUser($phone,$name,$birthdate,$address);
if($user)
{
$response["phone"]=$user["Phone"];
$response["name"]=$user["Name"];
$response["birthdate"]=$user["Birthdate"];
$response["address"]=$user["Address"];
echo json_encode($response);
}
else
{
$response["error_msg"]="Unknown Error occurred in registration!";
echo json_encode($response);
}
}
}
else
{
$response["error_msg"]="Required parameter (phone,name,birthdate,address) is missing!";
echo json_encode($response);
}
?>
CheckUser.PHP
<?php
require_once 'db_functions.php';
$db=new DB_Functions();
$response=array();
if(isset($_POST['phone']))
{
$phone=$_POST['phone'];
if($db->checkExistsUser($phone))
{
$response["exists"]=TRUE;
echo json_encode($response);
}
else
{
$response["exists"]=FALSE;
echo json_encode($response);
}
}
else
{
$response["error_msg"]="Required parameter (phone) is missing!";
echo json_encode($response);
}
?>
db_functions.php
<?php
class DB_Functions
{
private $conn;
function __construct()
{
require_once 'db_connect.php';
$db=new DB_Connect();
$this->conn=$db->connect();
}
function __destruct()
{
// TODO: Implement __destruct() method.
}
/*
* Check user exists
* return true/false
*/
function checkExistsUser($phone)
{
$stmt=$this->conn->prepare("select * from User where Phone=?");
$stmt->bind_param("s",$phone);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0)
{
$stmt->close();
return true;
}
else{
$stmt->close();
return false;
}
}
/*
* Register new user
* return User Object if user was created
* Return error mssage if have exception
*/
public function registerNewUser($phone,$name,$birthdate,$address)
{
$stmt=$this->conn->prepare("INSERT INTO User(Phone,Name,Birthdate,Address) VALUES(?,?,?,?)");
$stmt->bind_param("ssss",$phone,$name,$birthdate,$address);
$result=$stmt->execute();
$stmt->close();
if($result)
{
$stmt=$this->$this->conn->prepare("SELECT * FROM User where Phone = ?");
$stmt->bind_param("s",$phone);
$stmt->execute();
$user=$stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
}
else
{
return false;
}
}
}
db_connect.php
<?php
class DB_Connect{
private $conn;
public function connect()
{
require_once 'config.php';
$this->conn=new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
return $this->conn;
}
}
?>
姓名,电话,地址和出生日期值需要插入MYSQL数据库中。
答案 0 :(得分:0)
您的URL调用是GET方法,但是您正在查询POST变量。
因此,基本上,您的if语句导致为false,从而导致错误消息。将它们更改为GET。
if(isset($_GET['phone']) &&
isset($_GET['name']) &&
isset($_GET['birthdate']) &&
isset($_GET['address']))
然后它应该工作