There is a form through which I change my login, but if I already have such a login and write it again for newusername, then there will be two identical logins in the database, why does the commented condition not work?

<?php include 'core/init.php'; protect_page(); include 'includes/overall/header.php'; if (empty($_POST) === false) { $required_fields = array( 'username', 'newusername' ); foreach ($_POST as $key => $value) { if (empty($value) && in_array($key, $required_fields) === true) { $errors[] = 'поля со звездочкой обязательны'; break 1; } if (user_exists($_POST['username']) === ($_POST['newusername'])) { $errors[] = 'этот логин \'' . $_POST['username'] . '\' используется'; // условие которое должно выводить ошибку и не обновлять логин в БД если такой есть } } } ?> <h1>пользователи!</h1> <?php if (isset($_POST['username'], $_POST['newusername'])) { $username = mysql_real_escape_string($_POST['username']); $newusername = mysql_real_escape_string($_POST['newusername']); mysql_query("UPDATE `users` SET `username` = '$newusername' WHERE `username` = '$username'"); } $ath = mysql_query("SELECT * FROM `users` WHERE `type`=0"); if ($ath) { // Определяем таблицу и заголовок echo "<table border=1>"; echo "<tr><td>user_id</td><td>логин</td><td>пароль</td><td>имя</td><td>email</td></tr>"; // Так как запрос возвращает несколько строк, применяем цикл while ($author = mysql_fetch_array($ath)) { echo "<tr><td>" . $author['user_id'] . " </td><td>" . $author['username'] . " </td><td>" . $author['password'] . " &nbsp </td><td>" . $author['first_name'] . " </td><td>" . $author['email'] . " &nbsp </td></tr>"; } echo "</table>"; } else { echo "<p><b>Error: " . mysql_error() . "</b><p>"; exit(); } 

    2 answers 2

    I would do this:

    file verify.php (login and change)

     <?php $username = $_POST['username']; $new_name = $_POST['new_name']; if(strlen($username) == 0 || $new_name == 0){echo "Поля со звездочками обязательны";exit;} if($username == $new_name){echo "Новый логин должен отличаться от старого!";exit;} if(mysql_result(mysql_query("SELECT COUNT(`username`) FROM `users` WHERE `username` = '$new_name'"),0) == 1){echo "Пользователь с таким именем уже существует!";exit;} mysql_query("UPDATE `users` SET `username` = '$new_name' WHERE `username` = '$username'"); echo 'Успешно измененно'; ?> 
    • so it does not change anything at all - LLIAKAJI pm
    • change the names of the tables and others for yourself - mazanax
    • no, it doesn’t write anything at all and doesn’t change anything, even after correcting tables and variables in the code as it should! echo "The new login must be different from the old one!"; not from the old but the new one should not be what the table already has - LLIAKAJI
    • Well, it should also be different from the old one, because a meaningless load on the server is not good - mazanax
    • try to display the request itself and enter it manually - mazanax

    What is the undescribed user_exists function?

    • checks if there is such a login - LLIAKAJI
    • Problem something in it :) Throw it here. - Shevsky 4:05 pm
    • can matter in the user_exists function? - mazanax
    • no, not in it, on registration, it works there, the truth is such a condition if (user_exists ($ _ POST ['username']) === true) {$ errors [] = 'login \' '. $ _POST ['username']. '\' busy '; } but if I do this here, it simply writes that it needs to be written, but it still changes the login to the database! - LLIAKAJI
    • === TRUE, and you have === ($ _POST ['newusername']) - mazanax