Display Images From Url in Mysql database

Discussion in 'Web Design and Development' started by ldenman, May 7, 2006.

  1. ldenman macrumors regular

    Joined:
    Jul 20, 2005
    #1
    Basically this is what i want:

    A Form Where the User Inputs an Image Url.
    After Submitting, The Image is Displayed.
    The image url is going to be placed inside a database.

    Thanks.
     
  2. thejadedmonkey macrumors 604

    thejadedmonkey

    Joined:
    May 28, 2005
    Location:
    Pa
    #2
    to clarify, I submit a URL which is then stored in a database, or the image file itself gets stored in the database?

    Let me know and I can probably pull up some PHP code I have from leftover projects and post it here.
     
  3. ldenman thread starter macrumors regular

    Joined:
    Jul 20, 2005
    #3
    What i want is like, you enter in url to the image.
    The url gets placed inside the database.
    When the page calls the database, i want the image to be shown, not the address.
     
  4. thejadedmonkey macrumors 604

    thejadedmonkey

    Joined:
    May 28, 2005
    Location:
    Pa
    #4
    So to insert a URL into a database...
    insert.php
    PHP:
    <html>
    <head>
        <title>URL inserter</title>
        <style>
        body,td{
            font-family: verdana;
            font-size: 12px;
            color: black
            background: white;
        }
        input{
            font-family: verdana;
            font-size: 12px;
        }
        </style>
    </head>
    <body>
    <b>URL inserter - <a href="http://www.rilet.com" target="_blank">www.rilet.com</a></b><br /><br />
    <?
    if(!empty($_POST['address']))
    {
        @
    extract($_POST);
        
    $address mysql_real_escape_string("$address");
        
    $name mysql_real_escape_string("$name");
        
        if (
    $address == "http://www.site.com/image.jpg" || empty($name)){
            echo
    "Please make sure you have entered a valid name and URL.";
        }else{

            
    //insert into database...
            
    require("database.php");

            
    mysql_query("INSERT INTO image_addresses (name, address) VALUES('$name', '$address') ")
            or die(
    mysql_error());

            
    //Get the ID of the inserted image...

            
    $result mysql_query("SELECT id FROM image_addresses WHERE name='$name' ORDER BY id DESC LIMIT 1")
            or die(
    mysql_error());

            while(
    $row mysql_fetch_array$result )) {
            
    $id $row['id'];
            print 
    'The image URL has been <a href="get_image.php?id='$id .'">successfully added</a> to the database.<br><br>';
            }
        }
    }
    ?>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    URL:<input type="text" value="http://www.site.com/image.jpg" name="address" maxlength="100" size="32">
    Name:<input type="text" value="" name="name" maxlength="35"><br />
    <input type="submit" value="Submit" onClick="this.value='Please wait...';">
    </form>
    </body></html>
    get_image.php
    PHP:
    <html>
    <head>
        <title>Image viewer</title>
        <style>
        body,td{
            font-family: verdana;
            font-size: 12px;
            color: black
            background: white;
        }
        input{
            font-family: verdana;
            font-size: 12px;
        }
        </style>
    </head>
    <body>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="get">
    Image<select name="id">
    <?php
    require("database.php");

        
    $result mysql_query("SELECT name,id FROM image_addresses")
        or die(
    mysql_error());

        while(
    $row mysql_fetch_array$result )) {
            
    $name $row['name'];
            
    $id $row['id'];
        print
    '<option value="'.$id.'">'.$name.'</option>';
        }
    ?>
    </select>
    <input type="submit" value="View" onClick="this.value='Please wait...';">
    </form>

    <?php
    @$id $_GET['id'];
    if (!empty(
    $id)){
        
        
    // Get all the data from image_addresses
        
    $result mysql_query("SELECT name,address FROM image_addresses WHERE id='$id'")
        or die(
    mysql_error());

        while(
    $row mysql_fetch_array$result )) {
            
    $name $row['name'];
            
    $address $row['address'];
        print 
    "$name ($address)<br><br>";
        print
    '<img src="'.$address.'" alt="'.$name.'" style="max-width:100%;">';
        }
    }
    ?>
    </body></html>
    Database.php
    PHP:
    <?php
    $username 
    "";
    $password "";
    $hostname "localhost";
    $db "";
        
    $database mysql_connect($hostname$username$password
        or die(
    "Unable to connect to MySQL");
    mysql_select_db($db$database);

    ?>
    MySQL Table:
    PHP:
    CREATE TABLE `image_addresses` (
      `
    idint(4NOT NULL auto_increment,
      `
    namevarchar(35NOT NULL default '',
      `
    addressvarchar(100NOT NULL default '',
      
    KEY `id` (`id`)
    TYPE=MyISAM AUTO_INCREMENT=;
     
  5. thejadedmonkey macrumors 604

    thejadedmonkey

    Joined:
    May 28, 2005
    Location:
    Pa
    #5
    I don't want to edit the above post, in case vBulletin messes the post up, but there's a demo at http://442.rilet.com/test/1/

    It's pretty basic what I did, no way to delete an entry, but it gets the job done (if I understood you correctly). if you wanted something else, let me know...I'm pretty bored right now.

    P.S. Cool! someone used it!
     
  6. Lixivial macrumors 6502a

    Lixivial

    Joined:
    Jan 13, 2005
    Location:
    Between cats, dogs and wanderlust.
    #6
    I put in the three logos because, somehow, I just couldn't resist. What fun! :D

    To the original poster, though, hot-linking offsite isn't always a well-regarded behavior. It'd be a good idea to remember that if that was your original intention. ;)
     
  7. ldenman thread starter macrumors regular

    Joined:
    Jul 20, 2005

Share This Page