前提
ログインシステムを作成しています。
ユーザー情報をテーブルにINSERTする工程で問題が発生しています。
実現したいこと
- usersテーブルにユーザー情報を格納したい
- エラーを解消したい
発生している問題・エラーメッセージ
// signup.php Database error: failed to register
該当のソースコード
PHP
// authController.php <?php ini_set('display_errors', 1); session_start(); require '../config/db.php'; $errors = array(); $username = ''; $email = ''; if (isset($_POST['signup-btn'])) { $username = $_POST['username']; $email = $_POST['email']; $password_1 = $_POST['password_1']; $password_2 = $_POST['password_2']; if (empty($username)) { $errors['username'] = "Username required"; } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors['email'] = "Email address invalid"; } if (empty($email)) { $errors['email'] = "Email required"; } if (empty($password_1)) { $errors['password'] = "Password required"; } if ($password_1 !== $password_2) { $errors['password'] = "The two password do not match"; } $emailQuery = "SELECT * FROM users WHERE email = ? LIMIT 1"; $statement = $conn->prepare($emailQuery); $statement->bind_param('s', $email); $statement->execute(); $result = $statement->get_result(); $userCount = $result->num_rows; $statement->close(); if ($userCount > 0) { $errors['email'] = "Email already exists"; } if (count($errors) === 0) { $password = password_hash($password_1, PASSWORD_DEFAULT); $token = bin2hex(random_bytes(50)); $verified = false; $sql = "INSERT INTO users (username, email, verified, token, password) VALUES (?, ?, ?, ?, ?)"; $statement = $conn->prepare($sql); $statement->bind_param('ssbss', $username, $email, $verified, $token, $password); if ($statement->execute()) { $user_id = $conn->insert_id; $_SESSION['id'] = $user_id; $_SESSION['username'] = $username; $_SESSION['email'] = $email; $_SESSION['verified'] = $verified; $_SESSION['message'] = "You are now logged in!"; $_SESSION['alert-class'] = "alert-success"; header('location: index.php'); exit(); } else { $errors['db_error'] = "Database error: failed to register"; } } }
PHP
// signup.php <?php require_once '../controllers/authController.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Register</title> </head> <body> <div> <div> <form action="signup.php" method="POST"> <h3>Register</h3> <?php if (count($errors) > 0) : ?> <div> <?php foreach ($errors as $error) : ?> <li><?php echo $error; ?></li> <?php endforeach; ?> </div> <?php endif; ?> <div> <label for="username">Username</label> <input type="text" name="username" value="<?php echo $username; ?>"> </div> <div> <label for="email">Email</label> <input type="email" name="email" value="<?php echo $email; ?>"> </div> <div> <label for="password_1">Password</label> <input type="password" name="password_1"> </div> <div> <label for="password_2">Confirm Password</label> <input type="password" name="password_2"> </div> <div> <button type="submit" name="signup-btn">Sign Up</button> </div> <p>Already a member? <a href="login.php">Sign In</a></p> </form> </div> </div> </body> </html>
PHP
// index.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Homepage</title> </head> <body> <div> <div> <div> You are now logged in! </div> <h3>Welcome, Awa</h3> <a href="#">logout</a> <div> You need to verify your account. Sign in to your email account and click on the verification link we just emailed you at <strong>test@test.com</strong> </div> </div> <button>I am verified!</button> </div> </body> </html>
PHP
// db.php <?php require 'constants.php'; $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); if ($conn->connect_error) { die("Database error:" . $conn->connect_error); }
MySQL
mysql> show columns from users; +----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | username | varchar(100) | YES | | NULL | | | email | varchar(200) | YES | UNI | NULL | | | verified | tinyint | YES | | NULL | | | token | varchar(100) | YES | | NULL | | | password | varchar(255) | YES | | NULL | | +----------+--------------+------+-----+---------+-------+
試したこと
authController.phpに、ini_set('display_errors', 1);を記述した。
DBのエラー情報が返ってこないのでどこに問題があるのか分からない。
DBのホスト情報などが記載されているファイルではエラーが起きていない事を確認済み。
補足情報(FW/ツールのバージョンなど)
MySQL PHP MAMP MacOS
まだ回答がついていません
会員登録して回答してみよう