Mysql 是否在最后一个查询之前获取查询的最后一个插入ID?
如何获取主题id?所以我可以在用户发布帖子后将其重定向到他的主题? 如果我将其切换,则无法在帖子中插入主题id。嗯Mysql 是否在最后一个查询之前获取查询的最后一个插入ID?,mysql,pdo,Mysql,Pdo,如何获取主题id?所以我可以在用户发布帖子后将其重定向到他的主题? 如果我将其切换,则无法在帖子中插入主题id。嗯 $stmt = $db->prepare("INSERT INTO topics (subject, posted, first_post_id, last_post, last_post_id, forum_id) VALUES (:subject,:posted,:first_post_id,:last_post,:last_post_id,:forum_id)");
$stmt = $db->prepare("INSERT INTO topics (subject, posted, first_post_id, last_post, last_post_id, forum_id)
VALUES (:subject,:posted,:first_post_id,:last_post,:last_post_id,:forum_id)");
$stmt->execute(array(':subject'=>$subject,':posted'=>time(),':first_post_id'=>$cid,':last_post'=>time(),':last_post_id'=>$cid,':forum_id'=>$f));
$stmt = $db->prepare("INSERT INTO posts (poster_id, poster_ip, message, posted, topic_id)
VALUES (:poster_id,:poster_ip,:message,:posted,:topic_id)");
$stmt->execute(array(':poster_id'=>$cid,':poster_ip'=>$_SERVER['REMOTE_ADDR'],':message'=>$message,':posted'=>time(),':topic_id'=>$db->lastInsertId()));
header("Location: /thread/".$db->lastInsertId()); // This will get the post ID not topic ID that i want it to
使用临时变量:
$stmt = $db->prepare(/* FIRST QUERY */);
$stmt->execute();
$temp = $db->lastInsertId();
$stmt = $db->prepare(/* SECOND QUERY */);
$stmt->execute();
header("Location: /thread/".$temp);
哦当然哇,我怎么没想到我现在觉得自己很傻哈哈