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

标签 php mysql sql mysqli

我是 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/

相关文章:

如果值为 "0",则 MySQL 不返回任何内容

sql - Bigquery 从任何列包含 'FINISHED' 的表中选择

mysql - SQL - 测试分贝

php - 在 .htaccess 中设置环境变量并在 PHP 中检索它

php - Magento如何同步订单另一个mysql数据库

php - 在没有while语句的情况下返回单个查询中的所有数据php

javascript - qtip2 气球和 php 表 : different balloon content for each row?

php - 为我的网站寻找标签脚本

php - 什么时候使用准备好的语句?

php - 使用 PDO 创建模型层