点击发布按钮评论无法提交至数据库问题求助(附community.php代码)
Hey James, nice job ruling out typos already—that’s a solid first step. Let’s break down the most likely culprits for your form doing nothing when you click "publish":
1. You’re Missing a Submit Button (The #1 Oversight!)
Looking at your code snippet, there’s no <button type="submit"> or <input type="submit"> inside your form. Without this, clicking your "publish" element (whatever it is right now) won’t actually trigger the form to send data. Add this inside your <form> tag:
<button type="submit" name="submit_comment">Publish Comment</button>
2. Verify Form Submission & Data Handling
Your form uses action="", which means it submits to the same page (community.php). Let’s confirm the data is actually reaching your processing script:
- Add a debug line at the top of
check_com.phpto see if POST data comes through:
If this doesn’t show yourvar_dump($_POST); // This will print all submitted data exit;commentfield, there’s a problem with how the form is sending data. - Make sure you’re checking for the submit button before processing in
check_com.php:if (isset($_POST['submit_comment'])) { // Your database insertion logic goes here }
3. Check for JavaScript Blocking Submission
If you have frontend JS attached to the form or "publish" element, it might be stopping the default submission. Look for code like this:
document.querySelector('.main').addEventListener('submit', function(e) { e.preventDefault(); // This stops the form from submitting normally // If you’re using AJAX here, double-check the request is reaching the server });
If you’re using AJAX, confirm your request URL is correct and you’re handling the server response properly.
4. Fix Silent Database Errors
Even if the form submits, your database insertion might fail without telling you. Add error handling to your database code:
- Confirm your connection works:
$conn = mysqli_connect($host, $user, $pass, $db); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } - Check for query errors after insertion:
$sql = "INSERT INTO comments (comment_text) VALUES (?)"; $stmt = mysqli_prepare($conn, $sql); mysqli_stmt_bind_param($stmt, "s", $_POST['comment']); if (!mysqli_stmt_execute($stmt)) { echo "Error saving comment: " . mysqli_error($conn); } - Enable PHP error reporting temporarily to catch hidden issues:
error_reporting(E_ALL); ini_set('display_errors', 1);
5. Double-Check Your Textarea Closure
Your snippet cuts off at <textarea class="form-text" name="comment" id="comment"></...—make sure the textarea is properly closed with </textarea>. An unclosed element can break form rendering and submission.
Start with the submit button check first—it’s the most common fix for a form that does nothing on click. Let me know what you find!
内容的提问来源于stack exchange,提问作者James Smith

