我发现你的错字了。
您的(坏)代码具有更好的格式:
$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
}