crawl.php无法向数据库写入数据,WAMP64及线上服务器故障求助
Let’s walk through the potential issues step by step—starting with the frontend code you shared, then moving to backend and database checks.
Frontend Code Fixes (Immediate Issues)
Looking at your HTML and JavaScript, there are a few syntax/logic errors that could break the workflow entirely:
Incorrect Alert Selector
You’re using$("textGradient")to target the status alert, but this is a class selector—you need to add a dot (.) before the class name:// Wrong $("textGradient").text("Processing Please wait..."); // Correct $(".textGradient").text("Processing Please wait...");This is why you’re stuck on the "Processing" message—your code can’t actually find or update the alert element.
Broken Button Re-enable Logic
In the AJAX success callback, you’re using.text()instead of.prop()to re-enable the button, and the syntax is incorrect. Fix it to:$("#buttonSubmit").prop('disabled', false).text('Submit');This will re-enable the button and reset its text after a successful request.
Duplicate
nameAttribute
Your URL input has twoname="url"attributes. Remove one to avoid potential parsing conflicts:<input type="url" class="form-control" name="url" placeholder="Submit URL here..." required aria-label="Search" aria-describedby="basic-addon1" />Missing AJAX Error Handling
Right now, you have no way to know if the request fails. Add anerrorcallback to debug issues:$.ajax({ type: 'post', url: 'crawl.php', data: $('form').serialize(), success: function() { $("#buttonSubmit").prop('disabled', false).text('Submit'); $(".textGradient").html("The system has received the url"); }, error: function(xhr, status, error) { $("#buttonSubmit").prop('disabled', false).text('Submit'); $(".textGradient").html(`Error: ${error} (Status: ${status})`); console.log(xhr.responseText); // Log full server response to console } });This will show you exactly if the request fails and what the server returns.
Backend (crawl.php) & Database Checks
Even with a fixed frontend, if crawl.php isn’t working correctly, the database won’t update. Here’s what to verify:
Confirm POST Data Reception
At the top ofcrawl.php, add code to check if it’s receiving the URL:<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!isset($_POST['url'])) { die("Error: URL parameter missing"); } $url = $_POST['url']; // Log or print the URL for testing (remove in production) error_log("Received URL: " . $url); echo "Received URL: " . $url; }Submit the form again—if you don’t see this response, the data isn’t reaching the script.
Validate Database Connection
Ensure your credentials are correct (local WAMP and online servers often use different usernames/passwords). Add error handling to your connection code:// Example with mysqli $conn = mysqli_connect('localhost', 'username', 'password', 'database_name'); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }This will tell you immediately if the database connection fails.
Check SQL Insert Logic
Make sure your INSERT query is valid and includes error handling. Use prepared statements to avoid SQL injection:$sql = "INSERT INTO your_table_name (url_column) VALUES (?)"; $stmt = mysqli_prepare($conn, $sql); mysqli_stmt_bind_param($stmt, "s", $url); if (mysqli_stmt_execute($stmt)) { echo "URL added successfully"; } else { die("Insert failed: " . mysqli_error($conn)); }Server Permissions (Online Server)
- Ensure
crawl.phphas correct file permissions (usually 644). - Verify your database user has INSERT permissions for the target table.
- Ensure
Debugging Steps to Follow
- Open your browser’s Developer Tools (F12), go to the Network tab, then submit the form. Check if the request to
crawl.phpis sent, and view the Response tab to see what the server returns. - Test
crawl.phpdirectly with Postman or curl: send a POST request withurl=https://example.comand check if the database updates. - Review server error logs (WAMP:
wamp64/logs/php_error.log; online server: check your hosting control panel) for PHP or database errors.
内容的提问来源于stack exchange,提问作者Ken Kanaki

