PHP:将图像名称插入数据库时​​的重复/空白值

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);
}

?>

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

Try setting a session variable and tell it to exit or redirect when session variable is not set.

For example

if (!isset($_SESSION['session_name']))
{
   exit();
}

You probably should change the following line:

if(is_array($images)){

to this:

if(!empty($images) && is_array($images)){

explode() returns an empty array even if no "i" parameter is provided

The easiest way to avoid duplicates upon refresh is to re-direct the page after the POST, so just doing header("Location: {$_SERVER['PATH_INFO']}"); should solve that for you.

To avoid empty entries try is_array($images) && count($images)

First, you should be checking for blank names in your foreach statement, as such:

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

Secondly, you should look into header("Location: ..."); to prevent users from refreshing the page.

Thirdly, you could also set a session variable or cookie to prevent a user from uploading the same image twice.

Lastly, if the image names are unique, you can set a UNIQUE index on the image name. Then use INSERT IGNORE and that will remove all of your duplicates.