Search code examples
phpmysqldatabasedateuser-accounts

mySQL/PHP - How do I post a creation date to a new table entry?


Cutting to the chase, I'm working on a forum-like website using PHP, Bootstrap and mySQL as part of a university assignment.

I managed to get some of the basics working - creating new user accounts as well as new threads to be viewed by the general public.

Sadly, there a lot of things I don't know how to do - one of them, in question, happens to be recording the current date whenever I create a new user account or forum thread.

For instance, I want each new record under the userstable (storing data of all existing user accounts in mySQL) to have their own creation dates, the exact date they were made.

Here's what I tried so far...

The following piece of code is the top of my user registration file in PHP format.

<?php require_once('../private/initialize.php'); ?>

<?php
if(is_post_request()) {
    $user = [];
    $user['username'] = $_POST['username'] ?? '';
    $user['email'] = $_POST['email'] ?? '';
    $user['password'] = $_POST['password'] ?? '';
    $user['confirm_password'] = $_POST['confirm_password'] ?? '';
    $mysqldate = $_POST['joined_date'] ?? '';


    $result = insert_user($user); //Keep this function in mind, readers
    if($result === true) {
        $new_id = mysqli_insert_id($db);
        log_in_user($user);
        $_SESSION['message'] = 'You successfully created a new account.';
        redirect_to(url_for('/index.php'));
    } else {
        $errors = $result;
    }
} else {
        $user = [];
        $user['username'] = '';
        $user['email'] = '';
        $user['password'] = '';
        $user['confirm_password'] = '';
        $user['joined_dated'] = '';
    }
?>

To put everything into context, my website is structured into two folders: 'public' (containing files to be accessed by the general public) and 'private' (files containing common functions used across the entire site accessed with the require and include PHP functions.

The initialize.php file that the require_once line refers to contains a host of require_once lines that call up the rest of those files I mentioned. Without it, the site fails to function.

Note: I have edited the code to only show the information relevant to this post.

<?php

  session_start(); // turn on sessions 

  require_once('query_functions.php');

  $db = db_connect();
  $errors = [];

?>

query_functions.php

function insert_user($user) {
        global $db;

        $errors = validate_user($user);
        if (!empty($errors)) {
            return $errors;
        }

        $mysqldate = date( 'Y-m-d H:i:s', $phpdate );
        $phpdate = strtotime( $mysqldate );

        $hashed_password = password_hash($user['password'], PASSWORD_BCRYPT);

        $sql = "INSERT INTO users ";
        $sql .= "(username, email, hashed_password, joined_date) ";
        $sql .= "VALUES (";
        $sql .= "'" . db_escape($db, $user['username']) . "',";
        $sql .= "'" . db_escape($db, $user['email']) . "',";
        $sql .= "'" . db_escape($db, $hashed_password) . "',";
        $sql .= "'" . db_escape($db, $mysqldate) . "'";
        $sql .= ")";
        $result = mysqli_query($db, $sql);

        // For INSERT statements, $result is true/false
        if($result) {
            return true;
        } else {
            // INSERT failed
            echo mysqli_error($db);
            db_disconnect($db);
            exit;
        }
    }

These two codes, $mysqldate = date( 'Y-m-d H:i:s', $phpdate ); $phpdate = strtotime( $mysqldate );

I tried them after reading one of the articles I found. It kind of recorded a date in every new user account, except those dates always show as "0000-00-00".

Every other article I found so far shows a code solution that doesn't exactly follow the formula I wrote down (which I learned from a course on Lynda.com - https://www.lynda.com/PHP-tutorials/PHP-MySQL-Essential-Training-1-Basics/587674-2.html)

So hopefully I've explained everything there is to know about my problem. So what am I missing? What am I doing wrong? How do I finally get the site to record the current date to put into each new user account?

Thanks.


Solution

  • In query_function.php, $phpdate is undefined :

    $mysqldate = date( 'Y-m-d H:i:s', $phpdate );
    

    And this line is unused :

    $phpdate = strtotime( $mysqldate );
    

    Replace those both lines by this one :

    $mysqldate = date('Y-m-d H:i:s');