SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help replacing a mysql record

    I have a script that I am using to upload images. Whenever the form is submitted and an image is not uploaded, "/var/tmp/phpm4hih7" appears for the field in the MySQL database. When I go to upload a new image, it comes up with an error. The script below is what is used to find the name of the field and replace it. It works without any problem when there is an image name in the field such as "205.gif" but when "/var/tmp/phpm4hih7" is there it comes up with the error.

    Can somebody tell me how to change the preg_match script below so it will replace the "/var/tmp/phpm4hih7" with the new image name?

    PHP Code:
        if(preg_match ("/[^\.\/]+\.([^\.\/]+)$/"$image_name$matches)) 
          
    $newfilename .= ".".$matches[1]; 
    John Saunders

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand, why would you create an entry in the database if you didn't upload a picture?

  3. #3
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The form has about 10 other fields besides the image. Normally, an image is always uploaded but for some reason if one isn't uploaded, something like "/var/tmp/phpm4hih7" shows up instead of it just being empty.
    John Saunders

  4. #4
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can I see the code the code where the file name/path is used and inserted into the database?

  5. #5
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, here's the code:

    PHP Code:
    $newfilename $image_name;
    $result3 mysql_query ("SELECT id, image FROM sport WHERE id=$tid") or die ("Invalid query: Error 3");
    if (
    mysql_num_rows($result3)>0)
    {
        
    $row3 mysql_fetch_array($result3);
        
    $newfilename $row3["id"];
        
    $oldfilename $row3["image"];
        if(
    file_exists($oldfilename))
        {
         @
    unlink("$PHpath.$oldfilename");
        }
        if(
    preg_match ("/[^\.\/]+\.([^\.\/]+)$/"$image_name$matches)) 
         
    $newfilename .= ".".$matches[1];
    }
    mysql_free_result ($result3);
    if (!@
    move_uploaded_file($image$PHpath.$newfilename))
        
    $retval .= "<p >Image Uploading Error: Cannot copy to destination directory. Check the permissions on the upload directory.</p>";
    else
    {
        
    $retval .= "<p>Image uploaded!</p>";
         
    mysql_query ("UPDATE sport SET image='$newfilename' WHERE id=$id") or die ("Invalid query: Error 2");

    Thanks for the help!
    Last edited by JohnSaunders; Dec 11, 2003 at 11:45.
    John Saunders

  6. #6
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the board messed up your code a bit, could you upload it as an attachment?

  7. #7
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.

    OK...I uploaded it as an attachment but I guess SP has to approve attachments before they can be viewed. If you copy the code and paste it into Notepad, it should display correctly.
    Attached Files Attached Files
    Last edited by JohnSaunders; Dec 11, 2003 at 11:46.
    John Saunders

  8. #8
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting tired, I just had to 'reply' to your post to get the original code.
    Anyway, I'm looking at it.

  9. #9
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anybody know how to fix this error? I know Gaheris said he was looking into it but I really need to fix it ASAP.

    Thanks.
    John Saunders

  10. #10
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for not answering, but I'm still not sure what you want to achieve.

    Do you still want to create an entry in the database even when no image has been uploaded? As far as I understood the code, you want to have the possibility to have files with the same name by renaming them with the regex (id + filename). And when does that error come up and what kind of error is it?

  11. #11
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If a new image isn't uploaded, I want it to keep the same entry that's in the database. If a new image is uploaded, I want it to change the entry that's in the database whether it's a #.jpg or /var/tmp/phpm4hih7. It works fine when an image name is in the database, but when /var/tmp/phpm4hih7 is there, it comes up with the error "Invalid query : Error 2" the entry doesn't get replaced in the database.

    Thanks for your help.
    John Saunders

  12. #12
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds as though you need two seperate updates queries based on whether or not an image is uploaded. A simple if/else statement before the query should do the trick I would think.

  13. #13
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oopps... I was wrong.....now for some reason it is coming up with the Invalid query : Error 2 whenever I try to upload a new image for a new entry.
    John Saunders

  14. #14
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed the code around a bit so it will delete the image from the directory if there's an entry in the database. If someone can help me make it so it will not try and delete an image if something like "/var/tmp/*" is in the database, the problem will be resolved.

    PHP Code:
    if(!empty($oldfilename))
         
    unlink($PHpath.$oldfilename) or die ("Can't delete old image. Check the permissions on the upload directory."); 
    Any help would be greatly appreciated. I really need to get this working!
    John Saunders

  15. #15
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this?
    PHP Code:
    if (!empty($oldfilename) && (strpos($oldfilename'/var/tmp') === false)) {
        
    unlink($PHpath.$oldfilename) or die('Can\'t delete old image. Check the permissions on the upload directory.');


  16. #16
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked!

    Thanks a lot for the help!
    John Saunders


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •