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 exceedsupload_max_filesizeset in yourphp.ini2: File exceeds theMAX_FILE_SIZEvalue you defined in your form3: Only part of the file was uploaded4: No file was selected for upload6: PHP can't find its temporary upload directory7: 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
$imgtargetpoints to a folder that doesn't exist,move_uploaded_filewill 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-dataon Linux,IUSRon Windows) needs write access to the target folders. Stick with0755for production (safe) or0777for testing only. - Confirm path correctness: Print the target paths to ensure they're pointing where you intend:
Absolute paths (likeecho "Image target path: " . $imgtarget . "<br>"; echo "Photo target path: " . $phttarget . "<br>";/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
nameattributes on your file inputs match exactly what you're using in PHP (imagehebandphotoheb—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

