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

  • SEARCH
  • Home
  • 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 6149337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:26:09+00:00 2026-05-23T19:26:09+00:00

I am writting a script that checks a folder K:/Comics and inserts each name

  • 0

I am writting a script that checks a folder K:/Comics and inserts each name + number into a database, table name = comics. Now what i would like to do would be to check to see if this comic already exists before we run the insert queries.

Table Structure:

CREATE TABLE IF NOT EXISTS `comics` (
  `id` int(100) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `issue` varchar(4) DEFAULT NULL,
  `bio` longtext NOT NULL,
  `pages` int(10) NOT NULL,
  `size` varchar(100) NOT NULL,
  `price` varchar(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

Code:

<?php
    $main_folder = 'K:/Comics/'; // should be K:\Comics\ but I changed it because of the highlighting issue
    $folders = glob($main_folder.'* [0-9]*', GLOB_ONLYDIR);

    $comics_series = array();
    foreach($folders as $folder){
        $comics_series[] = preg_split('/(.+)\s(\d+)/', str_replace($main_folder, '', $folder), -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
    }

    $values = array();
    foreach($comics_series as $pair){
        $values[] = "('".mysql_real_escape_string($pair[0])."', '".((int) $pair[1])."')";
    }

    $query = 'INSERT IGNORE INTO comics (name, issue) VALUES '.implode(',', $values);
    $result = mysql_query($query);
    echo ($result) ? 'Inserted successfully' : 'Failed to insert the values';
?> 

What I thought would work but doesn’t (still adds comics to the db that are already there):

$query = 'INSERT IGNORE INTO comics (name, issue) VALUES '.implode(',', $values);
$result = mysql_query($query);
echo ($result) ? 'Inserted successfully' : 'Failed to insert the values';

What did I forget?!? The documentation said just to add IGNORE in there and it would work…

  • 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-05-23T19:26:09+00:00Added an answer on May 23, 2026 at 7:26 pm
    <?php
    $main_folder = 'K:/Comics/';
    $folders = glob($main_folder.'* [0-9]*', GLOB_ONLYDIR);
    
    $comics_series = array();
    foreach($folders as $folder){
        $comics_series[] = preg_split('/(.+)\s(\d+)/', str_replace($main_folder, '', $folder), -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
    }
    
    $values = array();
    foreach($comics_series as $pair){
        // clean the values to protect against SQL injection
        $pair = array(
            mysql_real_escape_string($pair[0]),
            mysql_real_escape_string($pair[1])
        );
        // add it to the values array, for insert
        $values[] = "('".$pair[0]."', '".$pair[1]."')";
    }
    
    $query = 'INSERT INTO comics (name, issue) VALUES '.implode(',', $values).' '.
             'ON DUPLICATE KEY UPDATE `issue` = VALUES(`issue`)';
    $result = mysql_query($query);
    echo ($result) ? 'Inserted successfully' : 'Failed to insert the values';
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a script that checks if given domain is parked or not.
I'm writing a bash script that needs to loop over the arguments passed into
Im writing a php script that is used to update a database but it
I am writing a python script which checks for number of active connections for
I'm writing a simple script for my webby that would send me an e-mail
I’m working on a Python script that accesses number of websites. I don’t think
I'm currently writing some installer script that fires SQL files against different database types
I am writing a DB upgrade script that will check to see if an
I'm writing a script that has to move some file around, but unfortunately it
I am writing a script that will look in a custom reports directory, copy

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.