Sprint666l Posted January 13, 2021 Share Posted January 13, 2021 Hello guys, I got problem with this code im still beginer in coding(PDO), so I think, that someone help me with this code. when someone help me I will be realy glad :). this is error massage ->Warning: Undefined array key "file" in C:\xampp\htdocs\forum_2021\account.php on line 63 Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\forum_2021\account.php on line 63 <form action="" method="post" enctype="multipart/form-data"> Select Image File to Upload: <input type="file" name="file"> <input type="submit" name="submit" value="Upload"> </form> <?php $statusMsg = ''; // File upload path $targetDir = "images/"; $fileName = basename($_FILES["file"]["name"]); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){ // Allow certain file formats $allowTypes = array('jpg','png','jpeg','gif','pdf'); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ // Insert image file name into database $insert = $connect->query("INSERT into users (profile_img) VALUES ('".$fileName."'"); if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; }else{ $statusMsg = "File upload failed, please try again."; } }else{ $statusMsg = "Sorry, there was an error uploading your file."; } }else{ $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.'; } }else{ $statusMsg = 'Please select a file to upload.'; } // Display status message echo $statusMsg; ?> Quote Link to comment https://forums.phpfreaks.com/topic/312000-php-7413-imge-uppload-to-database-using-pdo/ Share on other sites More sharing options...
requinix Posted January 13, 2021 Share Posted January 13, 2021 You cannot use anything in $_FILES until after you've tested to make sure there's something there to use. In other words, you can only set and use $fileName inside that main if block. Quote Link to comment https://forums.phpfreaks.com/topic/312000-php-7413-imge-uppload-to-database-using-pdo/#findComment-1583759 Share on other sites More sharing options...
Sprint666l Posted January 13, 2021 Author Share Posted January 13, 2021 Yah, I tried and still doesnt work, but now when I uploaded img new, image it will show in images / (name img.jpg). I need it to be uploaded to the database. Do you know how to fix it ? Because when I uploaded image show it error massage (File upload failed, please try again). I dont know what to do now. Quote Link to comment https://forums.phpfreaks.com/topic/312000-php-7413-imge-uppload-to-database-using-pdo/#findComment-1583760 Share on other sites More sharing options...
requinix Posted January 13, 2021 Share Posted January 13, 2021 6 minutes ago, Sprint666l said: Do you know how to fix it ? Unfortunately my crystal ball has exploded so I can't use my supernatural powers to instantly know everything there is to know about your application and code. So we'll have to do this the hard way by having you use your fingers and keyboard to type out the kind of information that is important for someone like me to have if I'm to tell you where the problem is. The "File upload failed" message means your INSERT query failed. Why did it fail? To find that out, try using PDO::errorInfo. 1 Quote Link to comment https://forums.phpfreaks.com/topic/312000-php-7413-imge-uppload-to-database-using-pdo/#findComment-1583761 Share on other sites More sharing options...
Sprint666l Posted January 13, 2021 Author Share Posted January 13, 2021 (edited) I used your broken crystal ball. And I immediately realized that there was a mistake somewhere. I realy dont undestand PDO:: for me its realy hard to know it. 😕 (ironie) I II just used basic php. Thanks for help. Fixed code: if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){ $statusMsg = ''; $targetDir = "images/"; $fileName = basename($_FILES["file"]["name"]); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); // Allow certain file formats $allowTypes = array('jpg','png','jpeg','gif','pdf'); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ $insert = "UPDATE users SET profile_img='$fileName' WHERE username='".$_SESSION['username']."'"; if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; }else{ $statusMsg = "File upload failed, please try again."; } }else{ $statusMsg = "Sorry, there was an error uploading your file."; } }else{ $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.'; } }else{ $statusMsg = 'Please select a file to upload.'; } echo $statusMsg; Edited January 13, 2021 by Sprint666l Quote Link to comment https://forums.phpfreaks.com/topic/312000-php-7413-imge-uppload-to-database-using-pdo/#findComment-1583763 Share on other sites More sharing options...
Barand Posted January 13, 2021 Share Posted January 13, 2021 If that is "fixed" then your testing is a little lax. Have you checked if the record is actually being updated (by magic, no doubt, as no query is executed)? Quote Link to comment https://forums.phpfreaks.com/topic/312000-php-7413-imge-uppload-to-database-using-pdo/#findComment-1583764 Share on other sites More sharing options...
Sprint666l Posted January 13, 2021 Author Share Posted January 13, 2021 Sorry, I were away from pc, and yeah, I see this problem my bad. I made mistake. if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ $insert = "UPDATE users SET profile_img='$fileName' WHERE username='".$_SESSION['username']."'"; $insert = mysqli_query($connect, $insert); if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; }else{ $statusMsg = "File upload failed, please try again."; } }else{ $statusMsg = "Sorry, there was an error uploading your file."; } }else{ $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.'; } }else{ $statusMsg = 'Please select a file to upload.'; } echo $statusMsg; Quote Link to comment https://forums.phpfreaks.com/topic/312000-php-7413-imge-uppload-to-database-using-pdo/#findComment-1583768 Share on other sites More sharing options...
Barand Posted January 13, 2021 Share Posted January 13, 2021 That still isn't going to work! (unless you aren't using PDO as you stated) mysqli_query() is not a PDO method (the clue's in the name). Get into the habit of using prepared statements and not putting variables in the sql code. $insert = "UPDATE users SET profile_img = ? WHERE username = ?"; $stmt = $connect->prepare($insert); $stmt->execute( [ $filename, $_SESSION['username'] ] ); Quote Link to comment https://forums.phpfreaks.com/topic/312000-php-7413-imge-uppload-to-database-using-pdo/#findComment-1583769 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.