Jump to content

[PHP 7.4.13 ] imge uppload to database using PDO::


Sprint666l

Recommended Posts

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;
?>

 

Link to comment
Share on other sites

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.

3df3fd3.PNG.1b015fb58645281a0f3138eae3c87ee2.PNG

Link to comment
Share on other sites

6 minutes ago, Sprint666l said:

Do you know how to fix it ?

:psychic:

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.

  • Like 1
Link to comment
Share on other sites


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 by Sprint666l
Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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'] ] );

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.