反馈提交报错:Doesn't match number of tokens 问题排查求助
Hey Andre, let's break down what's causing this error and fix it quickly!
The Root Cause
The error "Doesn't match number of tokens" happens because your SQL INSERT statement has a mismatch between the number of columns you're inserting into and the number of placeholder tokens (?) you've defined.
Looking at your insert-message.php code:
$insert_query = "INSERT INTO tbl_feedback (employee_id, full_name, email, website, message) VALUES (?, ?, ?, ?)";
You're targeting 5 columns (employee_id, full_name, email, website, message) but only providing 4 placeholder tokens in VALUES. When you call execute() with 5 values ($employee_id, $full_name, $email, $website, $message), PDO throws an error because the count doesn't line up.
The Fix
Update your INSERT query to include 5 placeholder tokens to match the 5 columns:
$insert_query = "INSERT INTO tbl_feedback (employee_id, full_name, email, website, message) VALUES (?, ?, ?, ?, ?)";
Bonus Cleanup Tips
While you're at it, here are a couple of small improvements to make your code more robust:
- Remove redundant curly braces: You have an extra
{right after$message = ($_POST['message']);that doesn't serve any purpose and can cause confusion. - Add try-catch for better error handling: Wrapping your PDO code in a try-catch block will let you see detailed error messages if something goes wrong, instead of just relying on
ERRMODE_EXCEPTION:try { $database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $insert_query = "INSERT INTO tbl_feedback (employee_id, full_name, email, website, message) VALUES (?, ?, ?, ?, ?)"; $insert = $database->prepare($insert_query); $insert->execute(array($employee_id, $full_name, $email, $website, $message)); echo "<script>alert('Successfully sent!'); window.location='feedback.php'</script>"; } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } - Clean up duplicate autofocus: Your form has two fields with
autofocus="autofocus"– only one field can have autofocus at a time, so you can remove one of those to avoid unexpected behavior.
That should resolve the token mismatch error and make your feedback form work as expected!
内容的提问来源于stack exchange,提问作者Andre F

