使用PDO更新MySQL数据库无变化,表单提交跳转后数据未更新求助
Hey there! Let's walk through the most common reasons your PDO update isn't taking effect in the MySQL database, even though your page redirects to view-event.php?action=updated. Here are the key checks to run step by step:
1. Confirm your UPDATE query is actually running & hitting rows
- First, make sure your code isn't skipping the execution logic. Add a
var_dump($_POST);right before your PDO code to verify form data is being submitted correctly. If the output is empty, your form's submission trigger (likeif(isset($_POST['submit']))) might not be working. - After executing the statement, use
$stmt->rowCount()to check how many rows were affected. A return value of0means either:- Your
WHEREclause isn't matching any records in the database - The values you're trying to update are identical to the existing ones (MySQL doesn't write changes when nothing's different)
- Your
2. Double-check your WHERE clause
This is the #1 culprit for silent failures:
- Ensure the parameter you're using in the
WHEREclause (like an event ID) is correctly pulled from your form or URL. For example, if you're usingWHERE id = ?, confirm the bound ID matches a valid record in youreventstable. - Test the raw SQL (replace placeholders with actual values) directly in your MySQL client. If it doesn't update anything there, your clause is the problem.
3. Enable PDO error reporting (you're probably missing errors)
PDO doesn't throw errors by default—so syntax mistakes or binding issues will fail silently. Fix this immediately:
// Enable exception mode right after initializing your PDO connection $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Wrap your update logic in a try-catch block to see errors try { $stmt = $pdo->prepare("UPDATE events SET title = ?, description = ? WHERE id = ?"); $stmt->execute([$_POST['title'], $_POST['description'], $_POST['event_id']]); // Always exit after a header redirect to stop further code execution header("Location: view-event.php?action=updated"); exit; } catch(PDOException $e) { die("Update failed: " . $e->getMessage()); }
This will force any errors to display instead of letting the page redirect without feedback.
4. Check for uncommitted transactions
If you started a transaction with $pdo->beginTransaction() but forgot to call $pdo->commit(), all changes will be rolled back automatically. Make sure your transaction logic includes a commit if the update succeeds.
5. Validate your HTML form setup
- Confirm your form uses
method="POST"(GET is not intended for modifying data and can cause issues with parameter handling). - Verify that input
nameattributes in your HTML match exactly what you're accessing in$_POST. For example, if your input is<input name="event_title">, don't try to fetch$_POST['title']—the names must be identical.
6. Verify database user permissions
While rare, if your MySQL user account doesn't have UPDATE permissions for the events table, the query will fail without obvious feedback. Run this in your MySQL client to check:
SHOW GRANTS FOR 'your_db_user'@'localhost';
Ensure you see UPDATE listed for your target database/table.
内容的提问来源于stack exchange,提问作者Arnab Dasgupta ADG

