我将一个表中的记录插入到另一个表中,但 MySQL 在新表中插入 2 行而不是一行。我正在使用 php 准备好的语句插入数据库。我的PHP代码如下:
if(isset($_POST["articleID"]) && isset($_POST["action"]))
{
$articleID = $_POST["articleID"];
$action = $_POST["action"];
if($action == "publish" && $infoUserRank >= 2)
{
$article_sql = "UPDATE blog_db.draft_articles SET status = 1 WHERE articleID = :articleID";
$article_stmt = $pdo->prepare($article_sql);
$article_stmt->bindValue(':articleID', $articleID);
$article_stmt->execute();
header("Location: http://localhost/admin/pending");
exit();
}
else if($action == "examine" && $infoUserRank >= 3)
{
//This is the statement which inserts two records instead of one
$insertarticle_sql = "INSERT INTO blog_db.articles (userID, full_title, short_title, meta_desc, meta_keywords, cover_pic, content, html_content, main_category) SELECT userID, full_title, short_title, meta_desc, meta_keywords, cover_pic, content, html_content, main_category FROM blog_db.draft_articles WHERE articleID = :articleID";
$insertarticle_stmt = $pdo->prepare($insertarticle_sql);
$insertarticle_stmt->bindValue(':articleID', $articleID);
$insertarticle_stmt->execute();
$insertsubcat_sql = "INSERT INTO blog_db.article_subcategories (articleID, subCategoryID) SELECT articleID, subCategoryID FROM blog_db.draftarticle_subcategories WHERE articleID = :articleID";
$insertsubcat_stmt = $pdo->prepare($insertsubcat_sql);
$insertsubcat_stmt->bindValue(':articleID', $articleID);
$insertarticle_stmt->execute();
}
}
我还尝试在我的 php 代码中添加 exit() 以检查它是否被调用两次,但 mysql 仍然插入两行而不是一行。我的插入代码有问题吗?我们将非常感谢您的帮助。
哆啦的时光机