0

I have this function for registering a user into the database with their date and time that the account was successfully created. When I click register it says it successfully registers but still nothing is added in thedatabase.

created_at is in TIMESTAMP format with default value of CURRENT_TIMESTAMP

here is my function

function registerUser(){
    global $connect, $name, $gender, $username, $password, $number, 
           $occupation, $address, $birth_date, $user_type, $photo,
           $status;
    $passwordHash = password_hash($password, PASSWORD_DEFAULT);
    $statement = mysqli_prepare($connect, "INSERT INTO client
                        (username, password, name, phone_number, 
                        gender, address, occupation, birth_date, 
                        user_type, photo, status, created_at) 
                    VALUES (?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)");
    mysqli_stmt_bind_param($statement, "sssssssssss", $username, 
                                        $passwordHash, $name, $number, 
                                        $gender, $address, $occupation, 
                                        $birth_date, $user_type, $photo, 
                                        $status);
    mysqli_stmt_execute($statement);

}

Hoping to fix this as soon as possible

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
shiela mekelele
  • 51
  • 2
  • 10

2 Answers2

1

Can you update the Mysql column data type now?

ALTER TABLE `your_table` 
MODIFY COLUMN `created_at ` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0);

so you don't have to send a value to the created_at column in every insert or update.

Your code;

function registerUser(){
global $connect, $name, $gender, $username, $password, $number, $occupation, $address, $birth_date, $user_type, $photo, $status;
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
$statement = mysqli_prepare($connect, "INSERT INTO client(username, password, name, phone_number, gender, address, occupation, birth_date, user_type, photo, status) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
mysqli_stmt_bind_param($statement, "sssssssssss", $username, $passwordHash, $name, $number, $gender, $address, $occupation, $birth_date, $user_type, $photo, $status);
mysqli_stmt_execute($statement);
}
selçuk doğan
  • 406
  • 4
  • 12
0

Depending on the date type you have set in the MySQL database, the format has to be correct when inserting date string.

If MySQL has created_at formatted as just a date, e.g. - 2018-05-11 then use PHP's date function with these parameters : date("Y-m-d") this will be the correct format for the database.

If the date format includes time and date, then use date(Y-m-d H:i:s). this will be the correct format that MySQL will accept.

Zac
  • 1,719
  • 3
  • 27
  • 48