You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP表单无法更新数据库求助:无报错但数据未更新

Troubleshooting Your Room Info Update Issue

Hey there! Let's break down why your campus discussion room info isn't updating to the database—since you're new to PHP, we'll take this step by step so you can follow along easily.

1. Fix the Undefined $row2 in Your Form

First off, I spotted a critical mistake in your form code: you’re using $row2['id'] for the hidden ID field, but you never defined $row2 anywhere! Earlier in the code, you fetched the room data into $row = mysqli_fetch_assoc($keranjang);—so $row2 doesn’t exist at all.

This means when you submit the form, the id value sent to your processing script is empty. Your UPDATE statement’s WHERE id = $_POST[id] then tries to match a row with an invalid/empty ID, which won’t find any rows to update. Since the SQL syntax is technically valid, mysqli_error() won’t return an error—so you’re left wondering why nothing changed.

Fix: Update the hidden field to use the correct variable:

<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />

2. Secure and Validate Your UPDATE Query

Right now, your processing script uses unescaped $_POST['id'] directly in the SQL query, which poses a SQL injection risk. Plus, if your id is a string type in the database, this could cause syntax issues even if the ID was correctly submitted.

Option 1: Escape the ID (Quick Fix)

Add the ID to your escaped variables list:

$id = mysqli_real_escape_string($koneksi, $_POST['id']);
$sql = "UPDATE ruangan SET kode = '$kode', kursi = $kursi, meja = $meja, status = '$status', note = '$note' WHERE id = $id";

Parameterized queries eliminate SQL injection risks entirely and are a better habit to build as a new developer. Here’s how to rewrite your update logic:

// Prepare the statement (placeholders ? replace direct values)
$stmt = mysqli_prepare($koneksi, "UPDATE ruangan SET kode = ?, kursi = ?, meja = ?, status = ?, note = ? WHERE id = ?");

// Bind parameters: "siissi" means s=string, i=integer (match your database field types)
// Adjust the type codes if your fields use different types (e.g., s for string IDs)
mysqli_stmt_bind_param($stmt, "siissi", $kode, $kursi, $meja, $status, $note, $id);

// Execute the statement
mysqli_stmt_execute($stmt);

// Check how many rows were updated
if (mysqli_stmt_affected_rows($stmt) > 0) {
    echo "Room info updated successfully!";
} else {
    echo "No rows were updated—either the ID doesn't exist, or the data matches the existing record exactly.";
    echo "<br>Error details: " . mysqli_error($koneksi);
}

// Clean up the statement
mysqli_stmt_close($stmt);

3. Verify Your Query is Actually Affecting Rows

Even if mysqli_error() shows nothing, your query might not be matching any rows. Add this check right after executing your original query to see what’s happening:

$result = mysqli_query($koneksi, $sql);
if ($result) {
    $affected_rows = mysqli_affected_rows($koneksi);
    echo "Query ran successfully. Rows affected: $affected_rows";
    if ($affected_rows === 0) {
        echo "<br>Possible reasons: Invalid ID, or the submitted data is identical to the existing record.";
    }
} else {
    echo "Query failed: " . mysqli_error($koneksi);
}

4. Check if Form Data is Being Submitted Correctly

To make sure all your form fields are being sent to the processing script, add this at the top of process-ruangan-edit.php to print out the submitted data:

echo "<pre>";
print_r($_POST);
echo "</pre>";

This will show you exactly what values are coming through—you’ll be able to spot if the id is missing, or if any other fields have unexpected values.

5. Double-Check Your Database Table Structure

Make sure your ruangan table matches what your query expects:

  • Is the id field a primary key (so it’s unique and can be used to target specific rows)?
  • Are kursi and meja set to numeric types (like INT)? If they’re string types, you’ll need to wrap $kursi and $meja in quotes in your SQL query (e.g., kursi = '$kursi').
  • Do any fields have "NOT NULL" constraints that might block updates if you leave a field empty?

6. Turn on PHP Error Reporting

As a new developer, enabling PHP error reporting will help you catch small mistakes like undefined variables (like your original $row2 issue) right away. Add these lines at the top of both your form and processing scripts:

error_reporting(E_ALL);
ini_set('display_errors', 1);

This will show you any PHP-level errors that might be happening behind the scenes.


内容的提问来源于stack exchange,提问作者Vanan Andreas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:33:58