0

コードに構文エラーがあります

$insert = @mysql_query("INSERT INTO topics (t_title, t_desc, t_pic, t_link, t_date,cat_id)
SELECT '$t_title','$t_desc','$t_pic','$t_link','$t_date','$cat_id'
WHERE NOT EXISTS (SELECT t_link
FROM topics
WHERE t_link = $t_link
) 
")or die(mysql_error());

これはエラーを返します:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE NOT EXISTS (SELECT t_link FROM topics WHERE t_link = 'showthread.php?t=120' at line 3

問題は t_link = $t_link にあると思いました

しかし、それを通常の値に置き換えても、問題は解決しません。

助けはありますか?

4

3 に答える 3

3

FROMあなたは最初のSELECTを逃しました

SELECT '$t_title','$t_desc','$t_pic','$t_link','$t_date','$cat_id'
# MISSED HERE FROM ???
WHERE NOT EXISTS
于 2012-10-11T22:10:15.180 に答える
1

FROM句のここでの解決策は、私のものではなくchumkiuの解決策として確認してください。

create table a ( i int);

insert into a (i )
select 1
from dual
where 1=2;

insert into a (i )
select 3
from dual
where 1=1;

結果

于 2012-10-11T22:27:14.860 に答える
0

テーブルに一意のインデックスがある場合t_linkは、次のことができます。

$insert = @mysql_query("INSERT IGNORE INTO topics (t_title, t_desc, t_pic, t_link, t_date,cat_id)
    VALUES ('$t_title','$t_desc','$t_pic','$t_link','$t_date','$cat_id');

キーワードは、IGNORE挿入が一意キー制約を複製する場合、何もしないように指示します。

于 2012-10-11T23:12:10.677 に答える