gpt4 book ai didi

mysql - PHP Slim API 调用 get 后返回 null

转载 作者:行者123 更新时间:2023-11-29 21:10:15 26 4
gpt4 key购买 nike

我在 Postman 中运行以下代码,但所有字段最终都为空。我使用 echo json_encode($cus) 来查看这一点。我已经摆弄了几个小时,但我不确定我的错误在哪里。任何帮助将不胜感激!

require 'Slim/Slim.php';

$app = new Slim();

$app->get('/Customers', 'getCustomers');
$app->get('/Customers/:id', 'getCustomer');
$app->post('/New_Customer', 'addCustomer');
$app->put('/Customers/:id', 'updateCustomer');
$app->delete('/Customers/:id', 'deleteCustomer');

$app->run();


// Add new Customer to the Database
function addCustomer() {
$request = Slim::getInstance()->request();
$cus = json_decode($request->getBody());

$sql = "INSERT INTO customers (Username, First_Name, Last_Name, Email, Status) VALUES (:username, :firstname, :lastname, :email, :status)";
try {
$db = DB_Connection();
$stmt = $db->prepare($sql);
$stmt->bindParam("username", $cus->Username);
$stmt->bindParam("firstname", $cus->First_Name);
$stmt->bindParam("lastname", $cus->Last_Name);
$stmt->bindParam("email", $cus->Email);
$stmt->bindParam("status", $cus->Status);
$stmt->execute();
$cus->id = $db->lastInsertId();
$db = null;
echo json_encode($cus);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
//echo json_encode($cus);
}
}

编辑:

var_dump($cus) results in
object(stdClass)#18 (5) { ["Username"]=> &NULL ["First_Name"]=> &NULL ["Last_Name"]=> &NULL ["Email"]=> &NULL ["Status"]=> &NULL

在 POSTMAN 中,我使用 form-data 和 x-www-form-urlencoded。

最佳答案

(你的版本是什么?)

我看看我的 slim

你的

$request = Slim::getInstance()->request();

我的:版本2.4.2

$request = \Slim\Slim::getInstance()->request();

关于mysql - PHP Slim API 调用 get 后返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36444921/

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