gpt4 book ai didi

php - mysql同时插入两个表

转载 作者:行者123 更新时间:2023-11-29 05:23:49 26 4
gpt4 key购买 nike

我需要将内容保存到两个表中。我有这段代码,但它只保存到桌面游戏中。我需要将所有内容保存到游戏中,并将文章的 ID、标题和日期保存到存档中。

有什么帮助吗?

非常感谢

 <?php
include("../includes/connect.php");

if(isset($_POST['submit']))
{
$games_date = date('y-m-d-h');
$games_title = $_POST['title'];
$games_author = $_POST['author'];
$games_keywords = $_POST['keywords'];
$games_link = $_POST['download_link'];
$games_image = $_FILES['image']['name'];
$games_tmp = $_FILES['image']['tmp_name'];
$games_content = $_POST['content'];

if($games_title=="" or $games_author=="" or $games_keywords=="" or $games_content=="" or $games_download_link="")
{
echo"<script>alert('any field is empty')</script>";
exit();
}
else
{
move_uploaded_file($games_tmp,"../uploaded_images/$games_image");
$games_query = "insert into games (games_title,games_date,games_author,games_image,games_keywords,games_link,games_content) values ('$games_title','$games_date','$games_author','$games_image','$games_keywords','$games_link','$games_content')";
$last_id = mysqli_insert_id($connect);
$archive_query = "insert into archive (title, pub_date) values ('$games_title','$games_date',)";
}
if(mysqli_query($connect,$games_query))
{
echo "<center><h1>Post published seccesfuly!</h1></center>";
}
}
?>

最佳答案

正如您提到的,您需要插入id,我更愿意采用这种方式

....
if(mysqli_query($connect,$games_query)){
$last_id = mysqli_insert_id($connect);
$archive_query = "insert into archive (id, title, pub_date) values ('$last_id', '$games_title','$games_date')";
mysqli_query($connect,$archive_query); // execute the archive query here
echo "<center><h1>Post published seccesfuly!</h1></center>";
}
}

关于php - mysql同时插入两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22579028/

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