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

PHP PDOStatement警告:住宿信息入库与图片上传异常排查求助

Hey there! It sounds like you're stuck on a frustrating file upload + database insert issue—especially when the DB part works but the uploads just won't cooperate. Let's walk through some common culprits and actionable fixes for your code.

1. Split Your Upload Checks to Isolate the Failure

Your current code only runs the database insert if both uploads succeed. If either move_uploaded_file fails, the entire condition skips the insert, and you won't get any hints about why. Let's break this down to find which step is breaking:

// Test each upload separately instead of combining them
$imageUploaded = move_uploaded_file($_FILES["imageheb"]["tmp_name"], $imgtarget);
$photoUploaded = move_uploaded_file($_FILES["photoheb"]["tmp_name"], $phttarget);

// Print results to see exactly what's happening
echo "Image upload status: " . ($imageUploaded ? "✅ Success" : "❌ Failed") . "<br>";
echo "Photo upload status: " . ($photoUploaded ? "✅ Success" : "❌ Failed") . "<br>";

Also, check PHP's built-in error codes for each file—these give specific clues about what's wrong:

echo "Image error code: " . $_FILES["imageheb"]["error"] . "<br>";
echo "Photo error code: " . $_FILES["photoheb"]["error"] . "<br>";

Here's what the most common error codes mean:

  • 0: No error (upload worked as expected)
  • 1: File exceeds upload_max_filesize set in your php.ini
  • 2: File exceeds the MAX_FILE_SIZE value you defined in your form
  • 3: Only part of the file was uploaded
  • 4: No file was selected for upload
  • 6: PHP can't find its temporary upload directory
  • 7: Failed to write the file to disk

2. Verify Target Paths & Folder Permissions

Incorrect paths or insufficient permissions are the #1 cause of upload failures:

  • Check if target directories exist: If $imgtarget points to a folder that doesn't exist, move_uploaded_file will fail. Create the folder first if needed:
    // Create image directory (and nested folders) if it doesn't exist
    $imageDir = dirname($imgtarget);
    if (!is_dir($imageDir)) {
        mkdir($imageDir, 0755, true);
    }
    // Repeat for your photo target directory
    $photoDir = dirname($phttarget);
    if (!is_dir($photoDir)) {
        mkdir($photoDir, 0755, true);
    }
    
  • Check folder permissions: The web server user (usually www-data on Linux, IUSR on Windows) needs write access to the target folders. Stick with 0755 for production (safe) or 0777 for testing only.
  • Confirm path correctness: Print the target paths to ensure they're pointing where you intend:
    echo "Image target path: " . $imgtarget . "<br>";
    echo "Photo target path: " . $phttarget . "<br>";
    
    Absolute paths (like /var/www/html/uploads/room.jpg) are far more reliable than relative paths here.

3. Double-Check Your Form Setup

If your form isn't configured for file uploads, nothing will work—even if your PHP code is perfect:

  • Make sure your form includes the enctype="multipart/form-data" attribute:
    <form method="POST" enctype="multipart/form-data">
        <!-- Your file inputs and other form fields -->
        <input type="file" name="imageheb">
        <input type="file" name="photoheb">
    </form>
    
  • Verify that the name attributes on your file inputs match exactly what you're using in PHP (imageheb and photoheb—this is case-sensitive!).

4. Rule Out Silent Database Insert Failures

Even if uploads succeed, your prepared statement might fail silently. Add error checking to your PDO code:

if ($imageUploaded && $photoUploaded) {
    $insertheb = $bdd->prepare("INSERT INTO hebergement(codetypeheb, nomheb, imageheb, nbplaceh...) VALUES (?, ?, ?, ...)");
    // Replace with your actual parameter values
    $insertSuccess = $insertheb->execute([$codetypeheb, $nomheb, $imgtarget, $nbplaceh...]);
    
    if (!$insertSuccess) {
        // Print PDO errors to debug the insert
        print_r($insertheb->errorInfo());
    }
}

Also, ensure the imageheb and photoheb columns in your database are long enough to store the file paths (use VARCHAR(255) or longer if needed).

5. Check PHP's Temporary Upload Directory

If PHP can't store the temporary upload file, move_uploaded_file will fail. Verify the temp directory exists and has permissions:

echo "PHP upload temp dir: " . ini_get('upload_tmp_dir') . "<br>";

If this returns empty or points to a non-existent folder, update your php.ini file to set a valid upload_tmp_dir with write access.


Start with splitting the upload checks and checking the error codes—this will immediately tell you where the problem lies. Let me know if you find a specific error code or path issue, and we can dig deeper!

内容的提问来源于stack exchange,提问作者user9177993

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:22:42