SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Echo out something that proves that you actually got something out of the database.

    Tell us what the error message was.

    I'd prefer to do this;

    PHP Code:
        foreach( $galleryTypes as $gallery ) {

        echo 
    '<option value="' $gallery['gallery_type_id'] . '\">'  ;
            echo 
    $gallery['gallery_type_desc'] . '</option>' PHP_EOL ;

        } 
    Less error-prone, saves having to use a counter, easier to read. If you are creating double quoted html attributes then try surrounding them in single quotes and the constant PHP_EOL does \n but correctly picks the right one for the operating sytem it is hosted on.

    If $gallery is only destined to be used for the next few lines, then shortening it to something like $gall or even $g that makes no big difference and is arguably easier to read too.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how would i echo something from the database? im not very sure how to do that.

    but at the moment i changed the while to a for loop and 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

    they refer to the line:

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

  4. #4
    SitePoint Member
    Join Date
    Aug 2009
    Location
    Aberdeen, Scotland
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by saunders1989 View Post
    how would i echo something from the database? im not very sure how to do that.

    but at the moment i changed the while to a for loop and 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

    they refer to the line:

    $result = $dbLink->query($query);
    You havent defined $dbLink anywhere in the upload.php script, you need to add the following line somewhere before line 10:

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

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh wow thank you for that. i completly forgot i needed to define my db again.

    it now picks up some information in the drop down menu but not the info i want

    it gives me a error saying:

    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

    which corrosponds to this:
    echo $galleryTypes[$i]['gallery_type_desc'];

    any ideas what to change?

    ive tried changing my for loop to this:
    for($i = 1; $i < $num_results+1; $i++)

    and starting my gallery_type_id at 0 but nothing has worked

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EDIT:

    i am now able to view my gallery names due to this code:

    $i=1;
    while($row=$result->fetch_assoc()){
    $galleryTypes[$i] = $row;
    $i++;
    }

    but get this error:

    Notice: Undefined index: gallery_type_id in C:\wamp\www\Blean_Photos\add_file.php on line 48

    from this line:

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

    it uploads to the folder okay but it doesnt write to the database due to that. i thought changing the ['gallery_type'] to either ['gallery_type_id'] or ['gallery_type_desc'] but that didnt make any difference.

    anyideas guys?

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EDIT:
    ive also got rid of the error. and now my problem is that it doesnt write to my db any more.

    ive echoed my query and got this back:

    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 the write stuff that needs to be written into my db but it doesnt appear in my db.

  8. #8
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Echo out something that proves that you actually got something out of the database.

    Tell us what the error message was.

    I'd prefer to do this;

    PHP Code:
        foreach( $galleryTypes as $gallery ) {

        echo 
    '<option value="' $gallery['gallery_type_id'] . '\">'  ;
            echo 
    $gallery['gallery_type_desc'] . '</option>' PHP_EOL ;

        } 
    Less error-prone, saves having to use a counter, easier to read. If you are creating double quoted html attributes then try surrounding them in single quotes and the constant PHP_EOL does \n but correctly picks the right one for the operating sytem it is hosted on.

    If $gallery is only destined to be used for the next few lines, then shortening it to something like $gall or even $g that makes no big difference and is arguably easier to read too.
    Off Topic:

    you cannot use \n or \r\n ie PHP_EOL while you are passing the dropdown data to javascript


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
  •