PHP / MySQL: insert a record if it does not 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 exist yet, it will add it to the database, if it already exists, then it does nothing. Also, will it also be case sensitive? Can you get around it without any restrictions?

+4
source share
1 answer

INSERT IGNORE ... will not be inserted if you have a unique constraint for your column. I would omit the line before inserting it if you want it to be case insensitive.

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

It may also be helpful.

+7
source

Source: https://habr.com/ru/post/1397544/


All Articles