# Image code modification

**URL:** <https://www.phphelp.com/t/image-code-modification/34218>\
**Category:** General PHP Help\
**Created:** [November 5, 2021, 7:09pm UTC](https://www.phphelp.com/t/image-code-modification/34218 "2021-11-05T19:09:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mekaboo](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@Mekaboo](https://www.phphelp.com/u/Mekaboo)\
**Post date:** [November 5, 2021, 7:09pm UTC](https://www.phphelp.com/t/image-code-modification/34218/1 "2021-11-05T19:09:14Z")

</div>

​

Hey☺️

Implemented a script and it works fine but want to tweak it so only one image is displayed after upload. As of now image is displayed after upload but also images previously uploaded (like a gallery). Goal is to have a singular profile image instead of a profile image gallery. Here is my code:

upload.php

```auto

<?php
// Include the database configuration file
require_once 'dbConfig.php';

// If file upload form is submitted
$status = $statusMsg = '';
if(isset($_POST["submit"])){
    $status = 'error';
    if(!empty($_FILES["image"]["name"])) {
        // Get file info
        $fileName = basename($_FILES["image"]["name"]);
        $fileType = pathinfo($fileName, PATHINFO_EXTENSION);
        
        // Allow certain file formats
        $allowTypes = array('jpg','png','jpeg','gif');
        if(in_array($fileType, $allowTypes)){
            $image = $_FILES['image']['tmp_name'];
            $imgContent = addslashes(file_get_contents($image));
        
            // Insert image content into database
            $insert = $db->query("INSERT into img (image, uploaded) VALUES ('$imgContent', NOW())");
            
            if($insert){
                $status = 'success';
                $statusMsg = "File uploaded successfully.";
            }else{
                $statusMsg = "File upload failed, please try again.";
            }
        }else{
            $statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.';
        }
    }else{
        $statusMsg = 'Please select an image file to upload.';
    }
}

// Display status message
//echo $statusMsg;
?>

```

view.php

````auto

   
      <?php
// Include the database configuration file
require_once 'dbConfig.php';

// Get image data from database
$result = $db->query("SELECT image FROM img ORDER BY uploaded DESC");
?>
      <div class="container">
    <?php if($result->num_rows > 0){ ?>
        <div class="w3-container">
            <?php while($row = $result->fetch_assoc()){ ?>
                <img src="data:image/jpg;charset=utf8;base64,<?php echo base64_encode($row['image']); ?>" />
            <?php } ?>
        </div>
    <?php }else{ ?>
        <p class="status error">Image(s) not found...</p>
    <?php } ?>
</div>
     ```

 

Thank ya ❤️

 

​
````

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [November 9, 2021, 9:43pm UTC](https://www.phphelp.com/t/image-code-modification/34218/2 "2021-11-09T21:43:18Z")

</div>

And, what is the problem?

---

<div class="post-metadata">

**Author:** ![Mekaboo](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@Mekaboo](https://www.phphelp.com/u/Mekaboo)\
**Post date:** [November 10, 2021, 1:10am UTC](https://www.phphelp.com/t/image-code-modification/34218/3 "2021-11-10T01:10:19Z")

</div>

@ErnieAlex

I thought I stated it: images after upload would show up as a gallery. What I wanted to create a profile pic upload.

I got figured out…added DESC query limit to 1 😊

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [November 10, 2021, 1:34am UTC](https://www.phphelp.com/t/image-code-modification/34218/4 "2021-11-10T01:34:18Z")

</div>

Glad you solved it! Always nice to solve a programming problem…
