PHP注册页面输入数据无法插入MySQL数据库求助
Let’s break down the common issues in your code that could be stopping user data from hitting your database, plus fixes to get it working smoothly:
1. First: Verify Your Database Connection
Your code doesn’t check if the database connection actually succeeded. If there’s a typo in credentials, the database doesn’t exist, or MySQL isn’t running, you’ll never know without this check. Add this right after your connection line:
$db = mysqli_connect('127.0.0.1', 'root', '', 'techdrive'); // Add connection error check if (!$db) { die("Database connection failed: " . mysqli_connect_error()); }
2. Catch INSERT Query Errors
You’re running the INSERT query but not checking if it fails. Add error handling here to see exactly why the data isn’t being inserted—this will reveal issues like misspelled table/column names or permission problems:
// Replace your existing mysqli_query line with this if (!mysqli_query($db, $query)) { die("INSERT failed: " . mysqli_error($db)); }
3. Fix Header Redirect Issues
Your redirect uses a backslash (..\index.html), which acts as an escape character in PHP. Use forward slashes instead, and always add exit(); after the header to stop further code execution (this prevents unexpected behavior after redirect):
header('location: ../index.html'); exit();
4. (Bonus) Upgrade Password Security & Block SQL Injection
MD5 is no longer a secure way to store passwords—switch to password_hash() instead. Also, using prepared statements eliminates SQL injection risks and avoids issues with special characters in usernames/emails:
Updated Secure Registration Code
if (count($errors) == 0) { $password = password_hash($password_1, PASSWORD_DEFAULT); // Modern secure hashing // Use prepared statement to avoid injection $query = "INSERT INTO users (username, email, password) VALUES(?, ?, ?)"; $stmt = mysqli_prepare($db, $query); mysqli_stmt_bind_param($stmt, "sss", $username, $email, $password); if (mysqli_stmt_execute($stmt)) { $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: ../index.html'); exit(); } else { die("INSERT failed: " . mysqli_stmt_error($stmt)); } }
And update your login code to use password_verify() instead of rehashing the input:
if (count($errors) == 0) { $query = "SELECT * FROM users WHERE username='$username'"; $results = mysqli_query($db, $query); if (mysqli_num_rows($results) == 1) { $user = mysqli_fetch_assoc($results); if (password_verify($password, $user['password'])) { $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: ../index.html'); exit(); } else { array_push($errors, "Wrong username/password combination"); } } else { array_push($errors, "Wrong username/password combination"); } }
5. Double-Check Frontend Form Fields
Make sure your HTML form’s input name attributes match exactly what’s in your PHP:
- Username input:
name="username" - Email input:
name="email" - Password fields:
name="password_1"andname="password_2" - Signup button:
name="signup"
Start with steps 1 and 2 first—they’ll immediately reveal if the problem is a broken connection or invalid SQL syntax.
内容的提问来源于stack exchange,提问作者Aroussi Riahi

