简体   繁体   中英

How to display data from mysql using php to html page

Hi I'm learning php and mysql and creating simple user system. Now I wan't to display user data from mysql to the page using PHP. But I'm struggling with that because I can't get data from databasa even though user correctly sign in. I don't know what is wrong with the code, can't find problem and I was trying difference ways to grab data and display but nothing is working and always variable with data is empty.

login-process.php

<?php 

include('connection.php');


$error = array();

$email = $_POST['email'];

if(empty($email)){
    $error[] = 'Email can not be empty!';
}

$password = $_POST['password'];

if(empty($password)){
    $error[] = 'Password can not be empty!';
}

if(empty($error)){

    // query

    $query = "SELECT userID, firstName, lastName, email, password, profileImage FROM user WHERE email=?";
    $q = mysqli_stmt_init($conn);
    mysqli_stmt_prepare($q, $query);

    mysqli_stmt_bind_param($q, 's', $email);
    mysqli_stmt_execute($q);

    // store result

    $result = mysqli_stmt_get_result($q);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

    if(!empty($row)){
        // checking password
        // unhasing password
        if(password_verify($password, $row['password'])){
            header('location: user-profile.php');
            exit();
        } else {
            echo "You are not a member";
        }
    }


}


// get user info
function get_user_info($con, $userID){
    $query = "SELECT firstName, lastName, email, profileImage FROM user WHERE userID=?";
    $q = mysqli_stmt_init($con);

    mysqli_stmt_prepare($q, $query);

    // bind the statement
    mysqli_stmt_bind_param($q, 'i', $userID);

    // execute sql statement
    mysqli_stmt_execute($q);
    $result = mysqli_stmt_get_result($q);

    $row = mysqli_fetch_array($result);
    return empty($row) ? false : $row;
}

user-profile.php

<?php

session_start();
include ('header.php');

$user = array();


if(isset($_SESSION['userID'])){
    require ('mysqli_connect.php');
    $user = get_user_info($con, $_SESSION['userID']);
}

?>

<section id="main-site">
    <div class="container py-5">
        <div class="row">
            <div class="col-4 offset-4 shadow py-4">
                <div class="upload-profile-image d-flex justify-content-center pb-5">
                    <div class="text-center">
                        <img class="img rounded-circle" style="width: 200px; height: 200px;" src="<?php echo isset($user['profileImage']) ? $user['profileImage'] : './assets/profile/beard.png'; ?>" alt="">
                        <h4 class="py-3">
                            <?php
                            if(isset($user['firstName'])){
                                printf('%s %s', $user['firstName'], $user['lastName'] );
                            }
                            ?>
                        </h4>
                    </div>
                </div>

                <div class="user-info px-3">
                    <ul class="font-ubuntu navbar-nav">
                        <li class="nav-link"><b>First Name: </b><span><?php echo isset($user['firstName']) ? $user['firstName'] : ''; ?></span></li>
                        <li class="nav-link"><b>Last Name: </b><span><?php echo isset($user['lastName']) ? $user['lastName'] : ''; ?></span></li>
                        <li class="nav-link"><b>Email: </b><span><?php echo isset($user['email']) ? $user['email'] : ''; ?></span></li>
                    </ul>
                </div>

            </div>
        </div>
    </div>
</section>

<?php
include "footer.php";
?>

login.php

<?php
    session_start();
    include ('header.php');
    include('login-process.php');
?>
<?php
    $user = array();
    require ('connection.php');
    

    if(isset($_SESSION['userID'])){
        $user = get_user_info($conn, $_SESSION['userID']);
    }

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        require ('login-process.php');
    }
?>
    <section id="register">
        <nav class="navbar nav">
            <a href="register.php"><img src="./image/logo.png" width="150px" height="150px" alt=""></a>
        </nav>
        <div class="row ml-5 m-0">
            <div class="col-lg-4 offset-lg-1">
                <div class="text-center pb-5">
                    <h1 class="login-title text-white">Sign in.</h1>
                    <p class="p-1 m-0 font-poppins text-white-50">Welcome back! Please enter your details.</p>
                </div>
                <div class="upload-profile-image d-flex justify-content-center pb-5">
                    <div class="text-center">
                        <div class="d-flex justify-content-center">
                            <img class="camera-icon" src="./image/camera.png" alt="camera">
                        </div>
                        <img src=<?php echo isset($user['profileImage']) ? $user['profileImage']: "./assets/profile-picture/avatar.jpg"; ?> style="width: 200px; height: 200px" class="img rounded-circle" alt="profile">
                        <small class="form-text text-white-50">Choose Image</small>
                        <input type="file" form="reg-form" class="form-control-file" name="profileUpload" id="upload-profile">
                    </div>
                </div>
                <div class="d-flex justify-content-center">
                    <form action="login.php" method="post" enctype="multipart/form-data" id="reg-form">
                        <div class="form-row my-4">
                            <div class="col">
                                <input type="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];  ?>" required name="email" id="email" class="form-control" placeholder="Email*">
                            </div>
                        </div>

                        <div class="form-row my-4">
                            <div class="col">
                                <input type="password" required name="password" id="password" class="form-control" placeholder="Password*">
                            </div>
                        </div>
                        <small id="login-error" class="text-danger">hi</small>
                        <div class="form-inline">
                            <input type="checkbox" name="agreement" class="form-check-input">
                            <label for="agreement" class="form-check-label pr-3 font-poppins text-white-50">Remember for 30 days</label>
                            <a href="#">Forgot password</a>
                        </div>

                        <div class="submit-btn text-center my-5">
                            <button type="submit" onclick="confirmPassword(event)" class="btn btn-dark  rounded-pill text-white px-5 py-3">Create account</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>


<?php
    include ('footer.php');
?>

register-process.php

<?php

include ('connection.php');

//data from the form
$firstName = $_POST['first-name'];
$lastName = $_POST['last-name'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$files = $_FILES['profileUpload'];
$profileImage = upload_profile("./assets/profile-picture", $files);


//sending data from input to the database
$sql = "INSERT INTO user VALUES (DEFAULT , '$firstName', '$lastName', '$email', '$password', '$profileImage', NOW())";

if (mysqli_query($conn, $sql)) {

    session_start();
    $_SESSION['userID'] = mysqli_insert_id($con);


    header('location:login.php');
    exit();

} else {
    echo "ERROR";
    echo  mysqli_error($conn);

}

//image

function upload_profile($path, $file){
    $targetDir = $path;
    $default = "avatar.jpg";

    $filename = basename($file['name']);
    $targetFilePath = $targetDir.$filename;
    $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);

    if(!empty($filename)){
        // file format
        $allowType = array('jpg', 'png', 'gif', 'jpeg');
        if(in_array($fileType, $allowType)){
            // send file to the folder
            if(move_uploaded_file($file['tmp_name'], $targetFilePath)){
                return $targetFilePath;
            };
        }
    }

    //if user does not load picture return the default one

    return $path .$default;

}

I would like to ask for code example how to display data from database into website when I have ID

You're not setting $_SESSION['userId'] when a user logs in successfully, so the part

if(isset($_SESSION['userID'])){
    $user = get_user_info($conn, $_SESSION['userID']);
}

will not trigger and $user will not be set.

Change

if(password_verify($password, $row['password'])){
    header('location: user-profile.php');
    exit();
} else {
    echo "You are not a member";
}

to

if(password_verify($password, $row['password'])){
    $_SESSION['userId'] = $row['userId']
    header('location: user-profile.php');
    exit();
} else {
    echo "You are not a member";
}

You haven't set the value for

$_SESSION['userID']
use this

session_start(); $user_id = $_SESSION['userID'];

hope it will solve your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM