gpt4 book ai didi

php - POST METHOD 不工作但不显示任何错误

转载 作者:行者123 更新时间:2023-11-29 03:16:31 25 4
gpt4 key购买 nike

我是 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!"}

注册.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 数据库中插入姓名、电话、地址和出生日期值。

最佳答案

您的 URL 调用是 GET 方法,但您查询的是 POST 变量。

所以基本上你的 if 语句导致错误消息。将它们更改为 GET。

if(isset($_GET['phone']) &&
isset($_GET['name']) &&
isset($_GET['birthdate']) &&
isset($_GET['address']))

那么它应该可以工作了

关于php - POST METHOD 不工作但不显示任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55367148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com