简体   繁体   English

PHP / MySQL:如果不存在则插入记录

[英]PHP/MySQL: Insert record if doesnt exist

$sql = "INSERT INTO toplist 
       (serverName, serverPassword, serverIp, serverPort, serverBanner,
        serverShortDescription, serverDescription)
       VALUES
       ('$_POST[serverName]', '$_POST[serverPassword]', '$_POST[serverIp]',
        '$_POST[serverPort]', '$_POST[serverBanner]', 
        '$_POST[serverShortDescription]', '$_POST[serverDescription]')";

if (!mysql_query($sql, $con)) {
    die('Error: ' . mysql_error());
}

echo "The server " . $_POST[serverName] . "," . $_POST[serverIp] 
    . " has been added.";

mysql_close($con);

How can I make it so that if the entry "serverIp" does not already exist, it will add it to the database, if it does already exist, then it doesn't do anything. 我怎样才能这样做,如果条目“serverIp”尚不存在,它将把它添加到数据库,如果它已经存在,那么它什么都不做。 Also, will this be case sensitive too? 此外,这也是区分大小写的吗? Will you be able to bypass it bY tYpInG LiKe tHiS? 你是否可以绕过它,因为它有什么好处?

INSERT IGNORE ... will not insert if you have a unique constraint on your column. INSERT IGNORE ...如果您的列上有唯一约束,则不会插入。 I would lower case the string before inserting it if you want it to be case insensitive. 如果您希望它不区分大小写,我会在插入之前小写字符串。

http://dev.mysql.com/doc/refman/5.5/en/insert.html http://dev.mysql.com/doc/refman/5.5/en/insert.html

This may also be helpful. 这也可能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM