表单提交在localhost本地正常,部署到线上服务器后失效求助
Let’s walk through the likely issues with your code and environment that could cause this discrepancy between local and production:
1. Critical Typo in Redirect Logic
Look at your redirect code:
if (mysqli_query($conn,$q)) { header('Location: product.php'); exit(header("Location: product.phpr")); }
- You’re first sending a valid redirect to
product.php, but then immediately sending an invalid redirect toproduct.phpr(note the extrarat the end). This is not only redundant but will cause unexpected behavior in production servers—some might ignore the first header, others throw a warning. Remove the second line entirely;exit()should come right after the valid header:header('Location: product.php'); exit();
2. Unhandled SQL Errors & Injection Vulnerability
Your code doesn’t show detailed MySQL errors, which is crucial for debugging production issues. Plus, directly interpolating user input into SQL queries is a massive security risk (SQL injection) and can break queries if inputs contain special characters (like quotes) that aren’t escaped.
Fix with Prepared Statements (Secure & Reliable)
Replace your insert logic with prepared statements to avoid both issues:
if(isset($_POST['send'])) { $name = $_POST['name']; $email = $_POST['email']; $ip = $_SERVER["REMOTE_ADDR"]; // Use prepared statement to prevent injection and handle special characters $stmt = mysqli_prepare($conn, "INSERT INTO service(name, email, ip) VALUES (?, ?, ?)"); mysqli_stmt_bind_param($stmt, "sss", $name, $email, $ip); if (mysqli_stmt_execute($stmt)) { header('Location: product.php'); exit(); } else { // Show detailed error for debugging (remove in production after fixing) echo "<h2 style='text-align:center; padding:1px; margin-top:-3%;'>Error: " . mysqli_stmt_error($stmt) . "</h2>"; } mysqli_stmt_close($stmt); }
3. Production Environment Debugging Steps
Even with fixed code, check these production-specific settings:
- Error Reporting: Enable temporary error reporting to see hidden issues. Add this at the top of your script (remove after debugging):
error_reporting(E_ALL); ini_set('display_errors', 1); - POST Request Limits: Check if your production server has low
post_max_sizeorupload_max_filesizeinphp.ini—this can cause POST data to be truncated, making$_POST['send']unavailable. - Database Permissions: Double-check that your production database user has write permissions to the
servicetable (local users often have full access, but production users might be restricted). - Character Set Consistency: Ensure your production database table
serviceuses theutf8orutf8mb4character set to match yourmysqli_set_charsetcall.
4. Cleanup Minor Redundancies
You’re setting the database charset twice:
mysqli_set_charset($conn,"utf8"); // ... mysqli_set_charset($conn,"utf8");
Remove one instance to clean up your code.
内容的提问来源于stack exchange,提问作者Kemal Doğru

