数据未在MYSQL DB中发布?

时间:2017-12-21 17:21:42

标签: php api mysqli

我试图使用Rest Api在MySQL DB中创建新记录。但是, $ data 中存储的数据不包含我通过参数输入的任何值。

我试图回复/打印以查看其中包含的内容,但它没有任何内容。

网址:http://localhost/api/v1/test.php?firstname=Yacub?lastname=Ali 。 我可以获取数据而不是POST。

function insertQueue()
{
    global $connection;

    $data = json_decode(file_get_contents('php://input'), true);
    echo $data;
    var_dump($data);
    $firstname    = $data["userName"];
    $lastname     = $data["lastname"];
    $organization = $data["organization"];
    $type         = $data["type"];
    $service      = $data["service"]; 


    echo $query="INSERT INTO queue SET 
    firstname='".$firstname."', 
    lastname='".$lastname."', 
    organization='".$organization."', 
    type='".$type."', 
    service='".$service."'";

    if(mysqli_query($connection, $query))
    {
        $response=array(
            'status' => 1,
            'status_message' =>'Added to queue successfully.'
        );
    }
    else
    {
        $response=array(
            'status' => 0,
            'status_message' =>'Queue Insertion has failed.'
        );
    }
    header('Content-Type: application/json');
    echo json_encode($response);
}

1 个答案:

答案 0 :(得分:4)

以下内容不是有效的网址。如果你想发送两者,你需要使用&

http://localhost/api/v1/test.php?firstname=Yacub?lastname=Ali

上面的正确的是:

http://localhost/api/v1/test.php?firstname=Yacub&lastname=Ali

其次,这是GET请求。 POST请求不会包含任何查询字符串。因此,将<form>的方法更改为POST

<form action="my.php" method="post">
<!--------------------^^^^^^^^^^^^^

而不是使用它:

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

你可以使用:

$data = $_POST; // After you convert the form method to POST.
相关问题