Answer the question
In order to leave comments, you need to log in
Why is the data not entered from the database?
As a practice of learning PHP, I decided to write the simplest registration-authorization in pure PHP, but I had problems with displaying data from the database after authorization (the name of the authorized user is not displayed)
Authorization code:
<?php
session_start();
require_once 'db_connect.php';
$password = md5($_POST['password']);
$username = $_POST['username'];
$sql = "SELECT * FROM users WHERE username = :username AND password = :password";
if (empty($password) || empty($username)) {
$_SESSION['message'] = 'Please, fill all the lines';
header('Location: /index.php');
}
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
'username' => $username,
'password' => $password
));
$row_count = $stmt->fetchColumn();
if ($row_count > 0 ){
$user_data = $stmt->fetchAll();
$_SESSION['user'] = [
"id" => $user_data['id'],
"username" => $user_data['username'],
"email" => $user_data['email']
];
header('Location: authorized_page.php');
} else {
$_SESSION['message'] = 'Incorrect password or login';
header('Location: /index.php');
<?php session_start(); ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>Hello, <?= $_SESSION['user']['username']; ?></p>
<p><a href="log_out.php">Log out</a></p>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Because no one ever reads the documentation.
In which it is written, for example, what array fetchAll () returns. And is it possible to get something from it in the form of $user_data['id'].
And in the manual it is written which function is actually suitable in this case.
Also, it says what the fetchColumn() function does. Which, firstly, does not at all what the author of the code thinks, and secondly, even if it did, it is still not needed here in any way.
And I'm not saying that the manual says in dark red on light red that MD5 cannot be used to hash passwords.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question