我正在與帖子的博客工作。 我想插入3個表試圖插入到兩個表使用PHP和mysqli
帖子,類別和Post_categories(顯示爲一個職位的合適的類別)
我什麼都試過使用的mysqli如:
$titel = $_POST['titel'];
$post = $_POST['post-text'];
$sql=
"INSERT INTO posts (titel, post)
VALUES ('$titel', '$post')
INSERT INTO post_categories (idposts)
VALUES ('". $mysqli->insert_id() ."')
";
if (!mysqli_query($link,$sql))
{
die('Error: ' . mysqli_error($link));
}
echo "1 record added";
mysqli_close($link);
但這沒沒有工作。我現在堅持了兩個小時,我幾乎放棄了這一點。 我真的不知道如何在Mysqli中做到這一點。我知道它是如何在Mysql中起作用的。
希望有人能幫助我解決這個問題。
我的表結構是這樣的:
帖子
idposts
Titel的
後
添加
分類
idcategor IES
類別
post_categories
ID
idposts
idcategories
在谷歌上搜索「mysql insert into multiple tables」。有幾個像這樣的例子:http://stackoverflow.com/questions/10043887/sql-insert-into-multiple-tables 基本上,如果你不做一個「存儲過程」,它不會成爲可能。您需要插入第一個,然後是第二個等。 – mogosselin