1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Image file doesn't appear in table column

Discussion in 'PHP' started by chrisj, Nov 20, 2014.

  1. #1
    Sorry, this thread has the wrong title. It should be "Image file doesn't appear in the correct row". Anyway, can you give me some ideas why the image file is uploading to the /upload/ folder, and into the thumbnail column (of the table named 'videos') but only into its own row, instead of into the same row as the rest of the Upload Form entries?

    $allowedExts = array("gif", "jpeg", "jpg", "pdf", "png");
    
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = strtolower( end($temp) );
    if (!in_array($extension,$allowedExts))
    {
    echo ("Error - Invalid File Name");
    }
    $length = 20;
    $thumbnail = $_SESSION['user_id'].$_FILES["file"]["name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $thumbnail);
    $sql = "INSERT INTO videos ( thumbnail ) VALUES( '$thumbnail' )";
    mysql_query($sql);
    $file_location = '<a href="http://www.....com/upload/' . $thumbnail . '">' . $thumbnail . '</a>';
    Code (markup):
    Any help will be appreciated, I'd be happy to provide addtional info or files/code
     
    Last edited: Nov 20, 2014
    chrisj, Nov 20, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Because you're not telling it which row it's supposed to upload to? You need to use a WHERE-clause to tell the info where it needs to go.
     
    PoPSiCLe, Nov 20, 2014 IP
  3. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Thanks for your reply.
    I don't know how to tell it where to go.
    I don't know how to identify the row.
    Any help with that will be greatly appreciated.
     
    chrisj, Nov 20, 2014 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #4
    From the text in your first post it looks like the file is being uploaded and is to be linked to existing information in your site - for example a profile picture or a product image.

    In your sql statement you have
    $sql = "INSERT INTO videos ( thumbnail ) VALUES( '$thumbnail' )";
    PHP:
    This creates a new row in the database and doesn't have any info in it to link it to anything else in your database. I'd expect, at the very least, the IP of the person uploading the image, but more likely the person's ID, the datetime of upload, a title and description.
     
    sarahk, Nov 20, 2014 IP
  5. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    Thanks for your reply. Yes, the 'videos' table has 'indexer' (which is a new record_id number for each row) and video_id, both of which I believe get created upon 'submit'. I thought something like:

    $sql = "UPDATE videos SET thumbnail='$thumbnail' WHERE video_id = '????'";
    Code (markup):
    might work, but I need additional help, with that or any other ideas, please.
     
    chrisj, Nov 20, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Well, that means you now have two entries with "INSERT INTO" in the processing of the form - turn it into one, after the upload has taken place? The code you posted is just a snippet, and therefore it's hard to tell you exactly what to do - if you're using a proper database handler, for instance PDO, you could also do a $query->lastInsertId() after the first insert, to get the ID of the row, and then use that value in the second insert, but again, why don't you just combine the two?
     
    PoPSiCLe, Nov 20, 2014 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    Which really should be the first fix here, as someone should already have read you the riot act about using the deprecated soon to no longer be supported mysql_ functions that we've been told for EIGHT YEARS to stop using, and that they finally two and a half years ago added giant red warning boxes to the manual waving you off.

    Though really we're not seeing your database structure -- is there an auto-update field or some other tracking value for that insert?

    You also shouldn't be relying on explode to parse your URI's... there can be issues with that and its' why PHP has the pathinfo function. Don't be brute forcing things PHP already has functions to handle.
     
    deathshadow, Nov 20, 2014 IP
  8. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #8
    Thanks for all of the replies.
    Can I please get an example of "you could also do a $query->lastInsertId() after the first insert, to get the ID of the row, and then use that value in the second insert, but again, why don't you just combine the two?"

    Also, no I don't believe there is an "auto-update field"
     
    chrisj, Nov 21, 2014 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    Can you please post the complete code for that function / call, so we can see how the first bit is being inserted also? Basically, you just combine the two insert-queries you have now into one - place it where the insert-query you posted above is, and that should basically do it - unless you're going in and out of functions or loops in the code. And you should look into using something else than mysql_ as @deathshadow said - it's deprecated, and really, REALLY not recommended
     
    PoPSiCLe, Nov 21, 2014 IP