PHP数据库更新失败求助:提交后无数据变更
Hey there, let's break down why your SQL update isn't taking effect—total bummer when you've spent hours digging through posts without luck, right? Here are the most common issues and how to fix them:
1. Verify Your Database Connection First
You defined your DB credentials, but did you actually confirm the connection is successful? Add this quick check to your code:
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME); // Check if connection failed if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }
If this throws an error, that's your root problem—fix the connection before moving on.
2. Make Sure Your SQL Statement Is Actually Executing (And Correct)
A lot of times, the page jumps but the update code never runs, or the SQL has a syntax/logic error. Try these steps:
- Print your SQL statement to see if it matches what you intended, then run it directly in a database client (like phpMyAdmin):
$sql = "UPDATE your_table SET your_column = 'new_value' WHERE id = 123"; echo $sql; // Copy this output and run it in your DB tool
If it doesn't work there either, your SQL is the issue—double-check the WHERE clause (is it targeting the right record?), field names, or data type matches (e.g., don't put quotes around numeric IDs).
- Check for MySQL errors after executing the query:
if(mysqli_query($link, $sql)){ echo "Records updated successfully."; } else { echo "ERROR: Could not run query. " . mysqli_error($link); }
This will spit out exactly what MySQL is complaining about—way better than guessing.
3. Check the Order of Your Code (Page Jump vs. Update)
Did you accidentally redirect the page before running the update? That's a super common mistake:
// ❌ Wrong: Redirect first, update never runs header("Location: your_target_page.php"); exit(); mysqli_query($link, $sql); // ✅ Correct: Run update first, then redirect mysqli_query($link, $sql); header("Location: your_target_page.php"); exit();
If you redirect first, the update code never gets executed—so no changes happen.
4. Validate Your Form Submission Data
If you're using a form to trigger the update, make sure you're grabbing the form data correctly:
// Get form values (use POST for safer submissions) $record_id = $_POST['id']; $new_data = $_POST['updated_value']; // Always sanitize data to prevent SQL injection! $record_id = mysqli_real_escape_string($link, $record_id); $new_data = mysqli_real_escape_string($link, $new_data); $sql = "UPDATE your_table SET column_name = '$new_data' WHERE id = $record_id";
If you're not pulling the right values from the form, your WHERE clause might target nothing—so no rows get updated.
5. Check for Uncommitted Transactions
If you're using database transactions, forgetting to commit will mean your update never saves to the database:
mysqli_begin_transaction($link); // Run your update mysqli_query($link, $sql); // Don't forget this! mysqli_commit($link);
Without a commit, the transaction rolls back, and your changes disappear.
Start with these checks—printing the SQL and checking for MySQL errors will usually point you right to the problem.
内容的提问来源于stack exchange,提问作者Micah George

