Ответов: 3 Просмотров: 214
|
Здравствуйте, помогите пожалуйста, нужно добавить обязательный ввод старого пароля и текущего, чтобы изменить пароль. Новичок в php и как осуществить, не понимаю, буду очень благодарен за помощь!
PHP:
скопировать код в буфер обмена
<?PHP include('server.php')?> <?PHP //PHP-код для обновления данных из базы mysql if(isset($_POST['update'])) { $hostname = "localhost"; $username = "root"; $password = ""; $databaseName = "registration"; $connect = mysqli_connect($hostname, $username, $password, $databaseName); // получать значения $id = $_POST['id']; $password = $_POST['password']; // запрос mysql для обновления данных $query = "UPDATE `users` SET `password`= '".$password."' WHERE `id` = $id"; if($result) { echo 'Data Updated'; }else{ echo 'Data Not Updated'; } } ?> <!DOCTYPE html> <html> <head> <title> PHP UPDATE DATA </title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div class="container"> <div class="frame"> <div class="nav"> <ul class"links"> <li class="signin-active"><a class="btn">Update inform</a></li> </ul> </div> <form class="form-signin" action="change-password.php" method="post"> ID To Update: <input class="form-styling" type="text" name="id" required value="<?PHP echo $id; ?>" readonly ><br><br> New Password:<input class="form-styling" type="text" name="password" required ><br><br> <input class="btn-signin" type="submit" name="update" value="Update Data"> </form> <div class="forgot"> <a href="index.php">Back</a> </div> </div> </div> </body> </html>
server.php
PHP:
скопировать код в буфер обмена
<?PHP // initializing variables $username = ""; $email = ""; $phone = ""; // connect to the database //username if (isset($_GET['username'])){ $username = $_GET['username']; $userquery = "SELECT * FROM users WHERE username = '$username'"; die ("That username could not be found"); } $id = $row['id']; $password = $row['password']; $username = $row['username']; $email = $row['email']; $phone = $row['phone']; $dbusername = $row['username']; } if($username != $dbusername){ die ("There has been a fatal error."); } } // REGISTER USER if (isset($_POST['reg_user'])) { // receive all input values from the form // form validation: ensure that the form is correctly filled ... // by adding (array_push()) corresponding error unto $errors array if ($password_1 != $password_2) { array_push($errors, "The two passwords do not match"); } // first check the database to make sure // a user does not already exist with the same username and/or email $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1"; if ($user) { // if user exists if ($user['username'] === $username) { } if ($user['email'] === $email) { } } // Finally, register user if there are no errors in the form if (count($errors) == 0 ) { $password = $password_1;//$password = md5($password_1) $query = "INSERT INTO users (username, email, phone, password) VALUES('$username', '$email', '$phone','$password')"; $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: index.php'); } } if (isset($_POST['login_user'])) { } } if (count($errors) == 0 ) { $password = $password; //$password = md5($password) $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: index.php'); }else { array_push($errors, "Wrong username/password combination"); } } } ?>
|