PHP提交评分评论显示成功但数据未写入数据库问题排查
Hey there, let's dig into why your form shows a Success message but no data lands in the database—there are several critical oversights in your PHP code that are blocking the insert operations from actually running:
1. You're not executing the INSERT queries (just overwriting variables)
Right now, you initialize $query, $query2, $query3 with SQL statements and run $connection->query() on them immediately (before any form data is submitted)—this would fail because variables like $picture aren't set yet. Then, when the form is submitted, you just reassign these variables to arrays of data, but you never actually run the prepared statements to insert that data into the database.
Fix:
Use prepared statements correctly to bind your data and execute inserts when the form is submitted:
if (isset($_POST['submitPic'])) { if (isset($_POST['rating'])) { $rating = htmlspecialchars($_POST['rating']); $comment = htmlspecialchars($_POST['comment']); // Insert into rate table $stmt = $connection->prepare("INSERT INTO rate(userID, kuva, rate) VALUES(?,?,?)"); $stmt->bind_param("isi", $user, $kuva, $rating); // Adjust type codes to match your DB column types $stmt->execute(); // Insert into picture table $stmt2 = $connection->prepare("INSERT INTO picture(nimi, userID, rate, kommentti) VALUES (?,?,?,?)"); $stmt2->bind_param("sisi", $kuva, $user, $rating, $comment); $stmt2->execute(); // Insert into comment table (only if comment isn't empty) if (!empty(trim($comment))) { $stmt3 = $connection->prepare("INSERT INTO comment(userID, picture, date, text) VALUES (?,?,?,?)"); $stmt3->bind_param("isss", $user, $kuva, $date, $comment); $stmt3->execute(); } $success = '<span style="color:green;font-size:32px;">Success</span>'; } else { $error = '<span style="color:red;font-size:32px;">Error</span>'; } }
2. Variable name mismatch ($kuva vs $picture)
You define $kuva = "Space"; but then use $picture in your initial SQL queries and POST handling. Since $picture is never assigned a value, this causes invalid (or non-existent) data to be passed to your queries. Stick to one variable name consistently across the script.
3. Database connection is closed too early
You call $connection->close(); before handling the POST request. By the time you try to run your insert statements, the database connection is already closed, so no queries can execute.
Fix:
Move $connection->close(); to the very end of your script, after all POST handling and query execution.
4. Initial INSERT queries run before form submission
Your code runs these lines immediately when the page loads:
$query = $connection->query("INSERT INTO rate(userID, kuva, rate) VALUES(?,?,?)"); $query2 = $connection->query("INSERT INTO picture(nimi, userID, rate, kommentti) VALUES (?,?,?,?)"); $query3 = $connection->query("INSERT INTO comment(userID, picture, date, text) VALUES (?,?,?,?)");
These will throw errors because you're using placeholders ? with query() (which doesn't support prepared statements—you need prepare() instead), and you're running them before any form data is available. Delete these lines entirely—they should only run when the form is submitted.
5. Minor HTML syntax issue
Your textarea has an extra closing tag:
<textarea name="comment" cols="50" rows="4"></textarea> </textarea> <!-- Remove this redundant line -->
This won't break form submission, but fixing it avoids potential unexpected behavior.
Bonus: Enable error reporting
You mentioned no error messages are showing up—add these lines at the top of your PHP script to debug future issues:
error_reporting(E_ALL); ini_set('display_errors', 1);
内容的提问来源于stack exchange,提问作者ENzFMa

