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 13 of 13
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts

    upload form with dropdown menu

    i have a database with 3 tables that look like this:

    members:
    member_id
    firstname
    lastname
    login
    password

    images:
    id (auto incrementing also the primary key)
    name
    size
    created
    image_path
    gallery_type_id

    gallery_type:

    gallery_type_id
    gallery_type_desc

    i am trying to create an upload form with a drop down menu (using the select function) what i want to do is in the dropdown menu display the gallery_type_desc in order by gallery_type_id. i have got this code so far but it dont work. i dont suppose anyone could have a look through and explain what i need to add or change that would be really helpfull. im new to php so any help would be really gratefull!

    upload form code: (i get a parse error on the coloured line)

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <?php
    $query = "SELECT gallery_type_id, gallery_type_desc FROM gallery_type ORDER BY gallery_type_id";
    
    /* run the query */
    $result = $dbLink->query($query);
    
    /* load the query's results into an array */
    $galleryTypes = $result->fetch_assoc();
    $num_results = $result->num_rows;
    ?>
    
    </head>
    
    <body>
    
     <form action="add_file.php" method="post" enctype="multipart/form-data">
             <input type="file" name="uploaded_file"><br>
             <input type="submit" value="Upload file">
         </form>
         
         <form action="add_file.php" method="post" enctype="multipart/form-data">
    <label for="gallery_type">Gallery Type</label>
    <select name="gallery_type" id="gallery_type">
    <?php
    	while($i = 0; $i < $num_results; $i++) {
    	echo "<option value=\"" . $galleryTypes[$i]['gallery_type_id'] . "\">";
    	echo $galleryTypes[$i]['gallery_type_desc'];
    	echo "</option>\n";
    	}
    ?>
    </select>
    </form>
    
    </body>
    </html>
    add_file.php code


    PHP Code:
    <?php

     
    // Check if a file has been uploaded

    if(isset($_FILES['uploaded_file'])) {

         
    // Make sure the file was sent without errors

         
    if($_FILES['uploaded_file']['error'] == 0) {

             
    $target_path "images/";

            
    $target_path $target_path basename$_FILES['uploaded_file']['name']); 



        if(
    move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) {

        echo 
    "The file ".  basename$_FILES['uploaded_file']['name']). 

        
    " has been uploaded";

        

        
    $dbLink = new mysqli('localhost''root''''gallery');

             if(
    mysqli_connect_errno()) {

                 die(
    "MySQL connection failed: "mysqli_connect_error());

             }  
    // You missed this closing curly brace here



             // Gather all required data

             
    $name $dbLink->real_escape_string($_FILES['uploaded_file']['name']);

             
    $mime $dbLink->real_escape_string($_FILES['uploaded_file']['type']);

             
    $size intval($_FILES['uploaded_file']['size']);

             
    $image_path $dbLink->real_escape_string($target_path);

             
    $gallery_type $dbLink->real_escape_string($_POST['gallery_type']); 

        
    $query "INSERT INTO `images` (`name`, `mime`, `size`, `data`, `created`, `image_path`, `gallery_type_id`)

                 VALUES ('{$name}', '{$mime}', {$size}, '', NOW(), '{$image_path}', '{$gallery_type}')"
    ;

                 

                     
    $dbLink->query($query);  

                  

         }

         

             

     else {

         echo 
    'Error! A file was not sent!';

     }

     

         }

         

    }

      

     
    // Echo a link back to the main page

     
    echo '<p>Click <a href="member-index.php">here</a> to go back</p>';

     
    ?>

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Change "while" to "for" and that highlighted code should run (or at least not throw a parse error there).

    Other than that, can you explain what isn't working about your code? Is there a test page that you can link us to so that we can see the results of your code? Or is the parse error your only issue?
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    saunders1989 (01-14-2010)

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    i changed the while to a for. but now i get these errors:


    Notice: Undefined variable: dbLink in C:\wamp\www\Blean_Photos\upload.php on line 10

    Fatal error: Call to a member function query() on a non-object in C:\wamp\www\Blean_Photos\upload.php on line 10

    something to do with this line:

    $result = $dbLink->query($query);

    erm i wouldnt know how to link you to a test page. i could zip the two files that work together and email them to you if that is possible?

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Nah, I don't want to set this up on my host. Is this upload form running on your local computer or is it hosted online? If it's hosted online then you would just give us the URL that leads to your upload form.

    Anyway, your problem now is this:
    PHP has determined that your syntax is all OK so it is starting to execute code. When it reaches $result = $dbLink->query($query); it finds that $dbLink has not been assigned a value (in this case it has not been set as a valid database connection).

    You must assign a working database connection to the variable $dbLink to get this script on track.

    The exact script to do that will depend on your database. Are you using MySQL?

    Edit: I just realized that your connection script is right there in add_file.php.

    Just copy that part and paste it into your form page right above $result = $dbLink->query($query); and you should be all set.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    saunders1989 (01-14-2010)

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    wow such simple mistakes im making. it is now displaying some information in the dropdown menu which says:

    NOTICE: undefined offset: 0 in c:/wamp/www/Blean_Photos/upload.php on line 39.

    this corrosponds to this line:

    echo $galleryTypes[$i]['gallery_type_desc'];

    im not 100% sure why it isnt echoing the gallery_type_desc

    im thinking because in my database i have this:

    gallery_type_id
    1
    2
    3

    is it because i started at 1 instead i should start at 0?

    EDIT:

    it also says:

    NOTICE: undefined offset: 1 in c:/wamp/www/Blean_Photos/upload.php on line 39.
    NOTICE: undefined offset: 2 in c:/wamp/www/Blean_Photos/upload.php on line 39.
    NOTICE: undefined offset: 3 in c:/wamp/www/Blean_Photos/upload.php on line 39.
    etc
    Last edited by saunders1989; 01-14-2010 at 07:05 PM.

  • #6
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Either that or change your loop to start at 1:

    PHP Code:
    for($i 1$i $num_results+1$i++) { 
    Note that you will have to increase the limit by 1 also ($num_results+1) in order to avoid skipping the last entry.

    But yes, honestly, I'd just start the database ID's at 0.

    Edit: I should read more carefully...
    Last edited by Rowsdower!; 01-14-2010 at 07:32 PM.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    saunders1989 (01-14-2010)

  • #7
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    i tried both ideas. but i still get that error. its really odd. i really thought changing it to a zero to start wtih would make it work.

    dont suppose you have any other ideas?

  • #8
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Part of the problem is that I'm not at all familiar with OOP.

    You might need to change your code here:
    PHP Code:
    $galleryTypes $result->fetch_assoc(); 
    to something like this:
    PHP Code:
    $i=0;
    while(
    $row=$result->fetch_assoc()){
    $galleryTypes[$i] = $row;
    $i++;

    But again, I've actually never created anything using object-oriented PHP so the syntax might be crappy.

    Basically, I think the last problem you have is that your $result is only returning one set of table values rather than all of them.

    Maybe someone proficient in OOP can hop in and seal the deal?
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    saunders1989 (01-14-2010)

  • #9
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    that bit of code worked a treat thanks it now shows all my gallery names. but im having another issue. (sorry for buggin you soo much. this is very much appreciated!)

    $gallery_type = $dbLink->real_escape_string($_POST['gallery_type']);

    that line is producing this error:
    Notice: Undefined index: gallery_type_id in C:\wamp\www\Blean_Photos\add_file.php on line 48

    i thought it was to do with the [gallery_type'] i dont have in my database so i tried both ['gallery_type_id'] and ['gallery_type_desc'] but they both came back with the same error.

  • #10
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by saunders1989 View Post
    that bit of code worked a treat thanks it now shows all my gallery names. but im having another issue. (sorry for buggin you soo much. this is very much appreciated!)

    $gallery_type = $dbLink->real_escape_string($_POST['gallery_type']);

    that line is producing this error:
    Notice: Undefined index: gallery_type_id in C:\wamp\www\Blean_Photos\add_file.php on line 48

    i thought it was to do with the [gallery_type'] i dont have in my database so i tried both ['gallery_type_id'] and ['gallery_type_desc'] but they both came back with the same error.
    I think the problem is that you have two separate forms. The submit button exists in a form that does not contain the dropdown, so the variable you are trying to use has not been passed through $_POST and your script is failing.

    Try combining your two forms into one and see if that sorts it out.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    saunders1989 (01-14-2010)

  • #11
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    that was it!. but do you know why it has stopped writing to my database? beacuase before i put this in i would be able to upload my image. and display like image path and other details. now it doesnt write at all

    EDIT:

    you've been soo helpful. thank you soo much!!!!

  • #12
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Not off-hand. This is where you need to start debugging your variable values inside of the add_file.php file. I'd start by echoing $query right after this line:

    PHP Code:
    $query "INSERT INTO `images` (`name`, `mime`, `size`, `data`, `created`, `image_path`, `gallery_type_id`) 

                 VALUES ('{$name}', '{$mime}', {$size}, '', NOW(), '{$image_path}', '{$gallery_type}')"

    And make sure that the output is a valid database command. I would suspect that, for whatever reason, it is not a valid command. It could be missing values or field names or might have quotation marks screwing it up. Really, it could be a lot of things.

    Just start here and work your way back. At this stage it's a pretty safe bet that the upload page is OK the way it is so the error is most likely in the add_file.php file.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #13
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    i wrote this to
    PHP Code:
    echo $query
    is that correct because that displays this:

    INSERT INTO `images` (`name`, `mime`, `size`, `data`, `created`, `image_path`, `gallery_type_id`) VALUES ('church interior.jpg', 'image/jpeg', 607836, '', NOW(), 'images/church interior.jpg', '3')

    which is correct what i want to be written into my database. but it doesnt appear

    EDIT:

    i have now debugged it and was able to do an if statement around my execution of the query and picked up a few errors and i fixed them and now it is writing to my database!

    thanks for all your help. been very helpful!
    Last edited by saunders1989; 01-14-2010 at 10:31 PM. Reason: fixed problem


  •  

    Posting Permissions

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