我正在使用php slim框架3 我的函数是插入null而不是数据
function addstud(Request $request, Response $response)
{
$stud = $request->getParsedBody();
$sql = "INSERT INTO students (`name`, `username`, `password`) VALUES (':name', ':username', ':password')";
try {
$db = GetDatabase();
$stmt = $db->prepare($sql);
$stmt->bindParam("name", $stud->name);
$stmt->bindParam("username", $stud->username);
$stmt->bindParam("password", $stud->password);
$stmt->execute();
$stud->id = $db->lastInsertId();
$db = null;
echo json_encode($stud);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
任何人都可以帮助我吗?
答案 0 :(得分:0)
你错误地绑定了参数:你忘了冒号了。所以,而不是
$stmt->bindParam("name", $stud->name);
$stmt->bindParam("username", $stud->username);
$stmt->bindParam("password", $stud->password);
你应该做
$stmt->bindParam(":name", $stud->name);
$stmt->bindParam(":username", $stud->username);
$stmt->bindParam(":password", $stud->password);