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

表单提交在localhost本地正常,部署到线上服务器后失效求助

Troubleshooting Your Form Submission Issue (Local vs. Production)

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 to product.phpr (note the extra r at 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_size or upload_max_filesize in php.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 service table (local users often have full access, but production users might be restricted).
  • Character Set Consistency: Ensure your production database table service uses the utf8 or utf8mb4 character set to match your mysqli_set_charset call.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 10:07:48