bind_param("ss", $value--6ren">
gpt4 book ai didi

php - 尝试获取插入值后得到的返回 ID,但 mysqli_insert_id() 返回 null

转载 作者:行者123 更新时间:2023-11-29 01:27:08 26 4
gpt4 key购买 nike

<?php 

$value = json_decode(file_get_contents('php://input'));
$mysql_pekare= new mysqli ("serv", "user","pass", "db");

if(!empty($value)) {
$stmt = $mysql_pekare->prepare("INSERT INTO users (`username`, `password`) VALUES(?,?)");

$stmt->bind_param("ss", $value->username, $value->password);
$stmt->execute();

if(!empty($stmt)) {

$contacts = array();
$id = mysqli_insert_id();
$contact = array("objectId" => ($id));
array_push($contacts, $contact);
echo json_encode(array('results' => $contacts), JSON_PRETTY_PRINT);
}

$stmt->close();
$mysql_pekare->close();

}
?>

现在,当我从我的前端插入我的信息时,值(用户名 + 密码)被添加到 MySQL 中并且具有唯一 ID,但我没有得到返回的 ID,目前我是这样得到的:

   {
"results": [
{
"objectId": null
}
]
}

最佳答案

您的 $id 很可能为 null 或不确定的错误...

//This is for procedural and needs a link
$id = mysqli_insert_id();
//should be
$id = mysqli_insert_id($mysql_pekare);
//$mysql_pekare I am assuming is your connection...

//however you are using oo (or I think) so should be
$stmt->insert_id

关于php - 尝试获取插入值后得到的返回 ID,但 mysqli_insert_id() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38019164/

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