私はこのコードを作りました:
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 ミリ秒です。