Home > Blockchain >  How to display data from mysql using php to html page
How to display data from mysql using php to html page

Time:12-26

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 >
        <div >
            <div >
                <div >
                    <div >
                        <img  style="width: 200px; height: 200px;" src="<?php echo isset($user['profileImage']) ? $user['profileImage'] : './assets/profile/beard.png'; ?>" alt="">
                        <h4 >
                            <?php
                            if(isset($user['firstName'])){
                                printf('%s %s', $user['firstName'], $user['lastName'] );
                            }
                            ?>
                        </h4>
                    </div>
                </div>

                <div >
                    <ul >
                        <li ><b>First Name: </b><span><?php echo isset($user['firstName']) ? $user['firstName'] : ''; ?></span></li>
                        <li ><b>Last Name: </b><span><?php echo isset($user['lastName']) ? $user['lastName'] : ''; ?></span></li>
                        <li ><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 >
            <a href="register.php"><img src="./image/logo.png" width="150px" height="150px" alt=""></a>
        </nav>
        <div >
            <div >
                <div >
                    <h1 >Sign in.</h1>
                    <p >Welcome back! Please enter your details.</p>
                </div>
                <div >
                    <div >
                        <div >
                            <img  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"  alt="profile">
                        <small >Choose Image</small>
                        <input type="file" form="reg-form"  name="profileUpload" id="upload-profile">
                    </div>
                </div>
                <div >
                    <form action="login.php" method="post" enctype="multipart/form-data" id="reg-form">
                        <div >
                            <div >
                                <input type="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];  ?>" required name="email" id="email"  placeholder="Email*">
                            </div>
                        </div>

                        <div >
                            <div >
                                <input type="password" required name="password" id="password"  placeholder="Password*">
                            </div>
                        </div>
                        <small id="login-error" >hi</small>
                        <div >
                            <input type="checkbox" name="agreement" >
                            <label for="agreement" >Remember for 30 days</label>
                            <a href="#">Forgot password</a>
                        </div>

                        <div >
                            <button type="submit" onclick="confirmPassword(event)" >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

CodePudding user response:

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";
}

CodePudding user response:

You haven't set the value for

$_SESSION['userID']
use this

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

hope it will solve your problem

  • Related