update mysql field

Discussion in 'PHP' started by sanyi007, Feb 26, 2010.

  1. #1
    Hi all!
    The thing i'm triing to achieve:
    users can post forms on my site, but for security reasons they can't upload to the site, just post some remote url which then saved to a field in my mysql database (directly linked to images).
    I would like to cache those images to my server. I already have a working function, and i can cache the images to the given folder on my server, and i can get the path to the image, but how do i update the mysql field (delete the old url in mysql and replace with the new path, to the cached image)?

    This is the code for caching:
    <?php
    $db = mysql_connect("host", "name", "password");
    mysql_select_db("mydatabase", $db);
    $query  = "SELECT * FROM table WHERE something='somethingtwo'";
    $result = mysql_query($query);
    function cache_image($image_url){
    	//replace with your cache directory
    	$image_path = './cache/';
    	//get the name of the file
    	$exploded_image_url = explode("/",$image_url);
    	$image_filename = end($exploded_image_url);
    	$exploded_image_filename = explode(".",$image_filename);
    	$extension = end($exploded_image_filename);
    	//make sure its an image
    	if($extension=="gif"||$extension=="jpg"||$extension=="png"){
    		//get the remote image
    		$image_to_fetch = file_get_contents($image_url);
    		//save it
    		$local_image_file  = fopen($image_path.$image_filename, 'w+');
    		chmod($image_path.$image_filename,0755);
    		fwrite($local_image_file, $image_to_fetch);
    		fclose($local_image_file);	
    	}
    }
    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    cache_image($row['something_value']);
    } 
    mysql_close($db);
    ?>
    PHP:
     
    sanyi007, Feb 26, 2010 IP
  2. HostingProvider

    HostingProvider Active Member

    Messages:
    1,480
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    95
    #2
    UPDATE table SET url='$newurl' WHERE something='somethingtwo' LIMIT 1;
    Code (markup):
    Should work perfectly, unless I've made a typo.
     
    HostingProvider, Feb 26, 2010 IP
  3. sanyi007

    sanyi007 Peon

    Messages:
    189
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the help! I will try it!
     
    sanyi007, Feb 26, 2010 IP