Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8229217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:43:34+00:00 2026-06-07T16:43:34+00:00

I am currently storing in MySQL database image names for easier way to retrieve

  • 0

I am currently storing in MySQL database image names for easier way to retrieve the actual images. I am having problems with the php code I created that stores the names. Duplicate and blank insertions are being made into the DB without my permission.

Is there a way to avoid this issue of duplicate or blank values being inserted when the page refreshed?

enter image description here

<?
$images = explode(',', $_GET['i']);

$path = Configuration::getUploadUrlPath('medium', 'target');


if (is_array($images)) {
    try {
        $objDb = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
        $objDb->exec('SET CHARACTER SET utf8');
    } catch (PDOException $e) {
        echo 'There was a problem';
    }

    $sql = "INSERT INTO `urlImage` (`image_name`) VALUES ";

    foreach ($images as $image) {
        $value[] = "('" . $image . "')"; // collect imagenames
    }

    $sql .= implode(',', $value) . ";"; //build query
    $objDb->query($sql);
}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:43:37+00:00Added an answer on June 7, 2026 at 4:43 pm

    I reformatted things into what I think should be slightly more readable and more easily separate what’s going on in the code. I also updated your queries to show how you can properly “sanitize” your input.

    I still think the process by which you’re going about sending the data to the server is wrong, but hopefully this code helps you out a little bit. I’d also do this more object orientedly.. but I feel that leaves the scope of your question just a little bit =P. It’s kind of like everyone else is saying though, the logic for your code was only off just slightly.

    As for the duplication thing, look into checking if the file already exists before adding it to the database.

    <?php
    $_GET['i'] = 'file1.png, file2.png, file3.png'; // This is just for testing ;].
    
    $images = retrieve_images();
    insert_images_into_database($images);
    
    function retrieve_images()
    {
        //As someone else pointed out, you do not want to use GET for this and instead want to use POST. But my goal here is to clean up your code
        //and make it work :].
    
        $images = explode(',', $_GET['i']);
        return $images;
    }
    
    function insert_images_into_database($images)
    {
        if(!$images)//There were no images to return
            return false;
    
        $pdo = get_database_connection();
    
        foreach($images as $image)
        {
            $sql = "INSERT INTO `urlImage` (`image_name`) VALUES ( ? )";
            $prepared = $pdo->prepare($sql);
            $prepared->execute(array($image));
        }
    }
    
    function get_database_connection()
    {
        $host = 'localhost';
        $db = 'test';
        $user = 'root';
        $pass = '';
        try {
            $pdo = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
            $pdo->exec('SET CHARACTER SET utf8');
    
            } catch(PDOException $e) {
                die('There was a problem');
            }
    
        return $pdo;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently storing dates in my MYSQL database as a PHP timestamp (integer field).
What is the best way to retrieve the current date? Currently I am storing
I'm working on a storefront application in PHP with MySQL. I'm currently storing my
I am having problems displaying images which have been queried from the database. The
I currently have a MySQL database that I am accessing using PHP. You search
I'm currently having an issue storing mouse coordinates into an array in which a
I took over some old php application with MySQL as database. Inside the database,
I currently use enums as TINYINTs in MySQL database. My tables have a few
Has anyone had an experience storing user uploaded images in a MSSQL 2008 Database
I'm designing a website using PHP and MySQL currently and as the site proceeds

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.