Create URL and insert into mysql

Discussion in 'PHP' started by blade_922, Apr 15, 2011.

  1. #1
    i,

    I have around 5000 rows in mysql each with unique i.d. Now a field in each row caled boxart holds the url to an image i.e

    images/uploads/abcdef.jpg

    Now i have had a brainwave to save me alot of time. I have around 3500 images to upload, so i will upload these via FTP instead of individually. Each image is uniquely identified by a 4 digit number which is the same as the row in the DB that the url will be on.

    So for example row 4543 has an image 4543.jpg to be uploaded and the following url images/uploads/abcdef.jpg to be inserted into the url. The images/uploads/ is the same directory for all of the images. Only diff is the image name which is the ending of the url. All images are jpg

    Now could i insert the url for all the rows by only SQL? So just do it in phpmyadmin? Or would i have to create a php script?

    Would the below work?

    <?php
    
    include 'config.php';
    include 'opendb.php';
    
    
    $result = mysql_query("SELECT * FROM ccms_gameindex ORDER BY id ASC") 
    or die(mysql_error());  
    
    INSERT INTO ccms_gameindex (boxart) VALUES ("images/uploads/".$id.".jpg")
    
    
    
    include 'closedb.php';
    
    ?>
    
    PHP:
     
    blade_922, Apr 15, 2011 IP
  2. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well if the id and image have the same value you don't have to insert it to the DB, just make the url each time you want to work with it. this way is easier and better since even if you had to change the location of all the images you'd still be able to make the url without changing the database!
    
    <?php
    ...
    //by this point in code you have the id in say $row[id] and you want to show the image
    $url="images/uploads/".$row[id].".jpg";
    echo '<img src="'.$url.'" alt=""/>'
    ...
    
    ?>
    
    PHP:
     
    Sepehr, Apr 15, 2011 IP
  3. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #3
    AstarothSolutions, Apr 15, 2011 IP
  4. JonasProductions

    JonasProductions Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No idea how to. You can search on google for mysql support forums.
     
    JonasProductions, Apr 15, 2011 IP
  5. blade_922

    blade_922 Well-Known Member

    Messages:
    396
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Hi Sepehr,

    Only issue is i have already uploaded around 1000 images that arent like this. They are named all different and dont contain the ID.
     
    blade_922, Apr 16, 2011 IP
  6. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I thought you said the ID and filename are the same.

    If your problem is that you have a folder of images and want to insert their name into the database without entering each and every filename, you can use the scandir() function to get the filenames and then create an SQL query to insert all of them into your database!
     
    Sepehr, Apr 16, 2011 IP