How can you implement a check for the uniqueness of the input field:
database schema:
CREATE TABLE `humans` ( `id` int(11) NOT NULL auto_increment, `lastname` varchar(100) NOT NULL, `name` varchar(100) NOT NULL, PRIMARY KEY (`id`) ); connection file to the config / db.php database:
<?php $databaseHost = 'localhost'; $databaseName = 'simple'; $databaseUsername = 'root'; $databasePassword = ''; $mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName); ?> html form:
<html> <head> <title>Добавление</title> </head> <body> <form action="add.php" method="post" name="form1"> <table width="25%" border="0"> <tr> <td>Фамилия</td> <td> <input type="text" name="lastName"> </td> </tr> <tr> <td>Имя</td> <td> <input type="text" name="name"> </td> </tr> <tr> <td>Отчество</td> <td> <input type="text" name="patronymic"> </td> </tr> </table> </form> </body> </html> And the script itself, on which the question:
<html> <head> <title>Добавление ++</title> </head> <body> <?php include_once("./config/db.php"); if(isset($_POST['Submit'])) { $lastName = stripslashes(trim($_POST['lastName'])); $name = stripslashes(trim($_POST['name'])); // проверка на пустые поля if(empty($lastName) || empty($name) ) { if(empty($lastName)) { echo "<font color='red'>Заполните фамилию</font><br/>"; } if(empty($name)) { echo "<font color='red'>Заполните имя</font><br/>"; } //link to the previous page echo "<br/><a href='javascript:self.history.back();'>Назад</a>"; } else { // if all the fields are filled (not empty) //insert data to database $result = mysqli_query($mysqli, "INSERT INTO humans(lastname,name) VALUES('$lastName','$name')"); //display success message echo "<font color='green'>Успех"; echo "<br/><a href='index.php'>Результат</a>"; } } ?> </body> </html> How can I competently check for the fact that:
if the database already has such a name, do not add it, leave it as it is, and just overwrite the last name, do not check if it is there or not.