我发现你的错字指定动作。
你(坏的)具有更好的格式化代码:
$db->query("
INSERT INTO track (
tm,
user_agent,
host,
ip,
port,
lang
)
VALUES (
'$tm',
'$user_agent',
'$host',
'$ip',
'$port',
'$lang'
)
WHERE NOT EXISTS
(
SELECT ip FROM track WHERE ip='$ip'
");
修复后:
$db->query("
INSERT INTO track (
tm,
user_agent,
host,
ip,
port,
lang
)
VALUES (
'$tm',
'$user_agent',
'$host',
'$ip',
'$port',
'$lang'
)
WHERE NOT EXISTS
(
SELECT ip FROM track WHERE ip='$ip'
) -- that was missing
");
无论如何,你可能需要的东西是这样的:
// check if ip exists
$query = $db->$query("
SELECT count(ip) FROM track WHERE ip='$ip'
");
$result = $query->fetchFirstRowSomehow(); // i dont know methods you have in $db object
$rows = $result['0']; // getting first (only) column that should contain count()
if ($rows == 0)
{
// your insert code here
}
else
{
// your update code here
}
是什么错误消息?使用''die''函数来查看出了什么问题 – doniyor 2013-02-21 16:42:25
[INSERT Syntax](http://dev.mysql.com/doc/refman/5.5/en/insert.html) – 2013-02-21 16:44:06
我相信你的第二个查询是完全错误的。 [插入查询](http://www.w3schools.com/php/php_mysql_insert.asp)| [插入忽略](http://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql)| [插入更新](http://stackoverflow.com/questions/4205181/insert-to-table-or-update-if-exists-mysql) – UnholyRanger 2013-02-21 16:44:23