php拼接多个insert,php – 将多个INSERTS分成一个表和多个表
我正在嘗試使用PostgreSQL數(shù)據(jù)庫(kù)在 PHP中開(kāi)發(fā)一個(gè)Q& A網(wǎng)站.
我有一個(gè)動(dòng)作來(lái)創(chuàng)建一個(gè)頁(yè)面,其中包含標(biāo)題,正文,類別和標(biāo)簽.我設(shè)法插入所有這些字段,但是我在插入多個(gè)標(biāo)記值時(shí)遇到了一些問(wèn)題.
我使用這個(gè)函數(shù)將逗號(hào)分隔的值放到一個(gè)數(shù)組中,現(xiàn)在我想要一些東西將每個(gè)數(shù)組元素插入到表標(biāo)記的數(shù)據(jù)庫(kù)中(避免重復(fù)),然后插入我的多對(duì)多關(guān)系表問(wèn)題標(biāo)簽:
$tags = explode(',', $_POST['tags']); //Comma separated values to an array
打印這樣的東西:
Array ( [0] => hello [1] => there [2] => this [3] => is [4] => a [5] => test )
動(dòng)作/ create_question.php
$category = get_categoryID_by_name($_POST['category']);
$question = [
'userid' => auth_user('userid'),
'body' => $_POST['editor1'],
'title' => $_POST['title'],
'categoryid' => $category
];
create_question($question, $tags);
然后我的create_question我應(yīng)該插入標(biāo)簽.
function create_question($question, $tags) {
global $conn;
$query_publications=$conn->prepare("SELECT * FROM insert_into_questions(:body, :userid, :title, :categoryid);
");
$query_publications->execute($question);
}
我在考慮做這樣的事情:
全球$conn;
foreach ($tags as $tag) {
$query_publications=$conn->prepare("INSERT INTO tags(name) VALUES($tag);
");
$query_publications->execute($question);
}
但后來(lái)我需要在我的多對(duì)多表中插入標(biāo)簽id.我是否需要?jiǎng)?chuàng)建另一個(gè)過(guò)程get_tags_id然后獲取tag_id數(shù)組并在我嘗試標(biāo)記時(shí)插入它們?
我什么時(shí)候執(zhí)行查詢?插入后或彼此結(jié)束?
對(duì)于任何誤用的術(shù)語(yǔ)或我的新手問(wèn)題,我們深表歉意.我是PHP的新手,我正在努力解決一些新概念.
最佳答案 您可以使用CTE在一個(gè)SQL命令中完成所有操作.
假設(shè)Postgres 9.6和這個(gè)經(jīng)典的多對(duì)多模式(因?yàn)槟銢](méi)有提供它):
CREATE TABLE questions (
question_id serial PRIMARY KEY
, title text NOT NULL
, body text
, userid int
, categoryid int
);
CREATE TABLE tags (
tag_id serial PRIMARY KEY
, tag text NOT NULL UNIQUE);
CREATE TABLE questiontags (
question_id int REFERENCES questions
, tag_id int REFERENCES tags
, PRIMARY KEY(question_id, tag_id)
);
要使用標(biāo)記數(shù)組插入單個(gè)問(wèn)題:
WITH input_data(body, userid, title, categoryid, tags) AS (
VALUES (:title, :body, :userid, :tags)
)
, input_tags AS ( -- fold duplicates
SELECT DISTINCT tag
FROM input_data, unnest(tags::text[]) tag
)
, q AS ( -- insert question
INSERT INTO questions
(body, userid, title, categoryid)
SELECT body, userid, title, categoryid
FROM input_data
RETURNING question_id
)
, t AS ( -- insert tags
INSERT INTO tags (tag)
TABLE input_tags -- short for: SELECT * FROM input_tags
ON CONFLICT (tag) DO NOTHING -- only new tags
RETURNING tag_id
)
INSERT INTO questiontags (question_id, tag_id)
SELECT q.question_id, t.tag_id
FROM q, (
SELECT tag_id
FROM t -- newly inserted
UNION ALL
SELECT tag_id
FROM input_tags JOIN tags USING (tag) -- pre-existing
) t;
dbfiddle here
這會(huì)創(chuàng)建任何尚未存在的標(biāo)記.
Postgres數(shù)組的文本表示如下所示:{tag1,tag2,tag3}.
如果保證輸入數(shù)組具有不同的標(biāo)記,則可以從CTE input_tags中刪除DISTINCT.
詳細(xì)說(shuō)明:
> Insert data in 3 tables at a time using Postgres
> How to use RETURNING with ON CONFLICT in PostgreSQL?
> How to implement a many-to-many relationship in PostgreSQL?
> Cannot INSERT: ERROR: array value must start with “{” or dimension information
如果您有并發(fā)寫(xiě)入,則可能需要執(zhí)行更多操作.特別考慮第二個(gè)鏈接.
總結(jié)
以上是生活随笔為你收集整理的php拼接多个insert,php – 将多个INSERTS分成一个表和多个表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Matlab中的logspace函数,m
- 下一篇: php hash pbkdf2,PHP