使用PDO预处理语句的PHP表单无法提交,提交后仅显示空白页
Let's walk through the key issues causing your blank page and missing database entries, plus fix them step by step:
1. Form Method vs. PHP Submission Check Mismatch
Your form uses method="GET", but your PHP code checks for isset($_POST["submit"]). Since the submit button's data will live in $_GET (not $_POST) with this setup, your data processing code never runs—hence the completely blank page.
Fix: Change your form's method to POST (the standard for form submissions that modify database data):
<form action="profile.php" method="POST">
2. Mixing Up PDO & mysqli Syntax
You're using PDO for database connections, but you called bind_param()—that's a method for mysqli prepared statements, not PDO. This would throw a fatal error if your code ever reached that point.
Fix: Use PDO's correct syntax for prepared statements. The simplest approach is to pass your parameters directly to execute() as an array:
// Handle the color checkbox first (avoid undefined variable if no boxes are checked) $color = isset($_POST["color"]) ? implode(",", $_POST["color"]) : ""; $sql = "INSERT INTO profile (name, age, email, phone, address, gender, color, comments) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; $stmt = $conn->prepare($sql); // Pass parameters directly to execute() $stmt->execute([$fname, $age, $email, $phone, $address, $gender, $color, $comments]); // PDO doesn't require explicit close()—resources are cleaned up automatically
3. Disabled PDO Error Reporting
Your dbconn.php has the PDO error mode line commented out. Without this, PDO won't throw exceptions for database errors (like invalid SQL or missing columns), making it nearly impossible to debug issues.
Fix: Uncomment that line in your database connection:
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
4. Handle Unchecked Checkboxes Gracefully
If the user doesn't select any favorite color, $_POST["color"] won't exist, and calling implode() on it will throw an error. We already addressed this in step 2 by checking if the variable exists first.
5. Add User Feedback (Optional but Recommended)
Right now, even if the code works, profile.php will still be blank. Add success/error messages to let the user know what happened:
try { // ... your data processing code ... $stmt->execute([$fname, $age, $email, $phone, $address, $gender, $color, $comments]); echo "<div class='alert alert-success'>Profile saved successfully!</div>"; } catch(PDOException $e) { echo "<div class='alert alert-danger'>Error saving profile: " . $e->getMessage() . "</div>"; }
Full Modified Profile.php Code
Here's the complete, fixed version of your processing script:
<?php header('Access-Control-Allow-Origin: *'); error_reporting(E_ALL); ini_set('display_errors', 1); if(isset($_POST["submit"])){ include("dbconn.php"); $fname = $_POST["fname"]; // Use $_POST instead of $_REQUEST for clarity $age = $_POST["age"]; $email = $_POST["email"]; $phone = $_POST["phone"]; $address = $_POST["address"]; $gender = $_POST["gender"]; $color = isset($_POST["color"]) ? implode(",", $_POST["color"]) : ""; $comments = $_POST["comments"]; try { $sql = "INSERT INTO profile (name, age, email, phone, address, gender, color, comments) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; $stmt = $conn->prepare($sql); $stmt->execute([$fname, $age, $email, $phone, $address, $gender, $color, $comments]); echo "<div class='alert alert-success'>Data inserted successfully!</div>"; } catch(PDOException $e) { echo "<div class='alert alert-danger'>Error: " . $e->getMessage() . "</div>"; } } ?>
内容的提问来源于stack exchange,提问作者pritesh

