Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post

    Adding a reference to an album upload

    Hi,

    I have a script that works fine for uploading an album but I want to add a reference number and I can't seem to do this. The reference number is generated from another page and sent to this page as part of a query string q=reference number.

    I will just paste below the relevant parts of the script from the main page, it is all on one page, let me know if you do need the whole script to makes sense of it.

    The code is as follows:
    PHP Code:

    $ref
    =$HTTP_GET_VARS["q"];

    //edited out

            
    function save($data) {
                if(
    $sql="INSERT INTO photos (Id, ref, subcategory, photo, date) 
                                  VALUES ('"
    .$_SESSION['Id']."', '".$data['ref']."', '".$data['photo']."', '".$data['filename']."', NOW())"){
            echo 
    "This is " $sql "<br />";
                return 
    true;                
            
            }

    //edited out

            
    if(move_uploaded_file($_FILES['file']["tmp_name"][$key], UPLOADS_DIR $newFileName.$fileType)) {
                        if(
    $this->createThumb(UPLOADS_DIR $newFileName.$fileType$fileTypeTHUMBS_DIR $newFileName.$fileType200200)) {
                            
    $data['ref'] = $ref;
                            
    $data['photo'] = $key;
                        
    $data['filename'] = $newFileName.$fileType;
                            if(
    $this->save($data)) {
                                
    $this->status 'Photo #'.$key.' is uploaded and saved to the db<br />';    
                            } else { 
    Now I have tried all variations:
    PHP Code:

                      $data
    ['ref1'] = '.$ref.';
                      
    $data['ref2'] = "$ref";
                      
    $data['ref3'] = $ref;
                      
    $data['ref4'] = "{$ref}";
                      
    $data['ref5'] = ".$ref."
    I have even tried changing the insert command:
    PHP Code:
                if($sql="INSERT INTO blogphotos (Id, ref, subcategory, photo, date) 
                                  VALUES ('"
    .$_SESSION['Id']."', '".$ref."', '".$data['photo']."', '".$data['filename']."', NOW())"){ 
    Nothing works. But if I echo $ref, it shows wonderfully.

    I'm also not sure where I have gone wrong in my logic here?

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    what type is the ref field in db and value of the ref variable?

  • Users who have thanked poyzn for this post:

    Jenny Dithe (12-09-2010)

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    The ref field in the database is currently set as varchar, even though at the moment it is a set of random numbers (I am debating changing it to a mix of letters and numbers which is why I haven't changed the varchar to int.)

    I did do a test changing it to int, just in case, and that didn't work.

    When I echo $sql out it just has '' for the ref value. So the value isn't actually getting to INSERT INTO command, but if I write echo $ref, elsewhere in the script, it does echo out. So I am assuming I have the wrong phrasing for the insert into, but as I said I tried everything I could think of.

  • #4
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    oh, just put next code inside the save function
    PHP Code:
    $ref=$HTTP_GET_VARS["q"]; 
    and make '".$ref."' in the sql syntax
    or next code into the upload function
    PHP Code:
    $data['ref'] = $HTTP_GET_VARS["q"]; 
    Last edited by poyzn; 12-08-2010 at 12:37 PM.

  • Users who have thanked poyzn for this post:

    Jenny Dithe (12-09-2010)

  • #5
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    It won't for some reason come up with the function.

    What I currently have is:
    PHP Code:
    <?php

    $ref
    =$HTTP_GET_VARS['q'];
    echo 
    "this is 1 " $ref "<br />";

    //connect to db code
            
            
    function save($data) {
            
    $dref=$ref;
            echo 
    "this is dref" $dref "<br />";
                if(
    $sql="INSERT INTO photos (Id, ref, subcategory, photo, date) 
                                  VALUES ('"
    .$_SESSION['Id']."', '{$dref}', '".$data['photo']."', '".$data['filename']."', NOW())"){
            echo 
    "This is " $sql "<br />";
                return 
    true;                
            
            }
    And the echo is:
    this is 1 1074615275128082806527590
    this is dref
    This is INSERT INTO photos (Id, ref, subcategory, photo, date) VALUES ('1893475024', '', '0', '32838e374bd6429.jpg', NOW())
    Photo #0 is uploaded and saved to the db

    I originally changed it to the below which I believe is what you suggested:
    PHP Code:


    //connect to db code
            
            
    function save($data) {
            
    $ref=$HTTP_GET_VARS['q'];
                if(
    $sql="INSERT INTO photos (Id, ref, subcategory, photo, date) 
                                  VALUES ('"
    .$_SESSION['Id']."', '".$ref."', '".$data['photo']."', '".$data['filename']."', NOW())"){
            echo 
    "This is " $sql "<br />";
                return 
    true
    And then changed it to the above to check that it was only within the function that it wouldn't be echoed out.

    I have no idea why it would work outside the function and not within, unless I have misunderstood where to place the code.

  • #6
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts

  • #7
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Thank you. Problem very easily solved with that great link.

    I'm afraid I had come across globals before, but not having needed them I hadn't really understood their purpose until now.


  •  

    Posting Permissions

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