我將一個表中的記錄插入到另一個表中,但 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 仍然插入兩行而不是一行。我的插入代碼有問題嗎?我們將非常感謝您的幫助。
1 回答

哆啦的時光機
TA貢獻1779條經驗 獲得超6個贊
你有$insertarticle_stmt->execute();
兩次并且$insertsubcat_stmt
從未被處決。
重復使用變量或確保它們完全分離。
- 1 回答
- 0 關注
- 135 瀏覽
添加回答
舉報
0/150
提交
取消