【发布时间】:2018-08-24 17:10:32
【问题描述】:
我想在插入后的 else 语句中从我的表购物车中获取最后一个插入 ID。但我没有得到身份证。 请检查我的代码并提出我做错了什么:
// Check to see if the cart COOKIE exists
if ($cart_id != '') {
// Here I want to update but not getting $cart_id, so everytime insert query fire in else statement
}else{
$items_json = json_encode($item);
$cart_expire = date("Y-m-d H:i:s",strtotime("+30 days"));
$db->query("INSERT INTO cart (items,expire_date) VALUES ('{$items_json}','{$cart_expire}') ");
$cart_id = $db->insert_id; // Here is the problem
setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
}
欢迎您提出建议。
【问题讨论】:
-
insert_id是否自动递增? -
是mysqli? ($db) ?
-
$cart_id = 选择@@identity;我可以这样用吗?
-
我在 localhost 中执行此操作。
标签: javascript php jquery mysql sql