1

私はこのコードを作りました:

INSERT into author(authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
select '".addslashes($_POST['authorfirstname1'])."','".addslashes($_POST['authorlastname1'])."','".addslashes($_POST['authorfirstname2'])."','".addslashes($_POST['authorlastname2'])."'
from author
where not exists(select authorfirstname1, authorlastname1, authorfirstname2, authorlastname2 from author
where author.authorfirstname1='".addslashes($_POST['authorfirstname1'])."'
and author.authorlastname1='".addslashes($_POST['authorlastname1'])."'
and author.authorfirstname2='".addslashes($_POST['authorfirstname2'])."'
and author.authorlastname2='".addslashes($_POST['authorlastname2'])."'
);

このコードのポイントは、値がデータベースにすでに存在するかどうかをチェックし、存在しない場合はそれを入力することです。この '".addslashes($_POST['authorlastname2'])."' は入力を表しますが、'%myentereddata%' に簡単に置き換えることができます。

私の問題は、それが何もしないことです...エラーメッセージ、その成功さえもしませんが、データが既にデータベースに存在しない場合はデータを入力せず、データが存在する場合にデータの入力を停止するかどうかはわかりません。

したがって、誰かがこのコードの問題を解決するのを手伝ってくれたり、別の方法で別の方法を示したりして、うまくいくようにしてくれたらありがたいです。

私のIDはプライマリでシリアルなので、挿入する必要はありません

INSERT INTO author (authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
VALUES ('one','two','three','four');

クエリが正常に返されました: 1 行が影響を受け、60 ミリ秒の実行時間。

INSERT into author(authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
select 'one','two','three','four'
from author
where not exists(select authorfirstname1, authorlastname1, authorfirstname2, authorlastname2 from author
where author.authorfirstname1='one'
and author.authorlastname1='two'
and author.authorfirstname2='three'
and author.authorlastname2='four'
);

クエリが正常に返されました: 657 行が影響を受け、実行時間は 40 ミリ秒です。

INSERT into author(authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
select 'new','new','new','new'
from author
where not exists(select authorfirstname1, authorlastname1, authorfirstname2, authorlastname2 from author
where author.authorfirstname1='new'
and author.authorlastname1='new'
and author.authorfirstname2='new'
and author.authorlastname2='new'
);

クエリが正常に返されました: 1314 行が影響を受け、実行時間は 70 ミリ秒です。

4

2 に答える 2

1

問題は、FROM 句が 1 つではなく 2 つあることです。実際には、テーブル全体にある行と同じ回数だけ同じ行を挿入しています (WHERE 句が満たされた場合)。クエリは次のようになります (最初の FROM が削除されていることに注意してください)。

INSERT into author(authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
select 'one','two','three','four'
where not exists(select 1 from author
where author.authorfirstname1='one'
and author.authorlastname1='two'
and author.authorfirstname2='three'
and author.authorlastname2='four'
);

内部選択の列も削除されました。SELECT 1 FROM...行が存在するかどうかを確認するには十分です。特定の列を抽出する必要はありません。いずれにせよ、それらは上位レベルで破棄されます。

別の無関係な問題は、addslashes挿入されたパラメーターに引用符が含まれるとすぐに、エスケープが無効なクエリを生成することです。

これは、PGstandard_conforming_stringsが ON に設定されている場合、バックスラッシュは何もエスケープしない通常の文字であるためです。PostgreSQL 9.1 以降、デフォルトでオンになっています。

pg_escape_string代わりに使用してください。

于 2013-05-24T13:40:38.250 に答える
0

テーブルに重複するエントリがないことを確認したいだけの場合は、 を使用する必要がありますUNIQUE

INSERTそうすれば、すでにそこにある行を呼び出すたびに、エラーが発生します。

このような:

CREATE UNIQUE INDEX ON author (authorfirstname1, authorlastname1, authorfirstname2, authorlastname2);

INSERTその後、同じ名と姓のセットを 2 回使用することはできなくなります。

次に、次INSERTのようにします。

INSERT into author(authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
VALUES ('".addslashes($_POST['authorfirstname1'])."', '".addslashes($_POST['authorlastname1'])."', '".addslashes($_POST['authorfirstname2'])."', '".addslashes($_POST['authorlastname2'])."')
于 2013-05-24T12:50:13.943 に答える