SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    822
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Upload/Select Option Within Form & Upload to DB table

    I'm a rookie with a complex issue:
    I'm having trouble working through an upload/select option within my form. I have successfully created a form for a client to select from a variety of photos to create a new record in the database. Problem is, they want me to combine the menu selection with an option to upload a file to the server and record the filename within the database. I'm familiar with creating upload forms, but not in combination with a menu selection as an option and processing it. I told you I was a rookie.

    My db table 'staff' has several fields with one being 'staff_photo' for the filename. I'm not sure how to combine the processing script with the current one I have. I've been piecing together bits to try to make this work, but I'm coming across some snags:

    #1 The record is not recording in the database, nor is the filename copying to the server.

    #2 None of my error messages are coming up if something is working incorrectly

    #3 After the script is successful, it should load up the page 'staff_list.php'.

    I was hoping to have had this figured out by now, considering I've been on this for over a week and still haven't found the solution. I hope someone can help me out with this.

    My form is as follows:
    HTML Code:
    <form method="post" name="form1" enctype="multipart/form-data" action="<?php echo $editFormAction; ?>">
    <table width="80%" align="center" cellspacing="0" border="0">
    <tr valign="baseline">
    <td nowrap align="right">Select Photo:</td>
    <td><select name="staff_photo">
    <option value="">No photo</option>
    <?php buildImageList('../../photos/staff/'); ?>
    </select>
    <span class="small">&nbsp;&nbsp;&nbsp;(If there is no photo available, please choose 'No-Phot-Available.jpg' or 'No photo')</span></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Upload Photo: </td>
    <td><input name="staff_photo" type="file" />
    </td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">Name:</td>
    <td><input name="staff_name" type="text" class="widebox" value="" size="32" maxlength="150"></td>
    </tr>
    <tr valign="baseline">
    <td nowrap align="right">&nbsp;</td>
    <td><input type="submit" value="Insert record"></td>
    </tr>
    </table>
    <input type="hidden" name="MM_insert" value="form1">
    <input name="staff_photo" type="hidden" id="staff_photo" />
    </form>
    And here's the process:
    PHP Code:
    $editFormAction $_SERVER['PHP_SELF'];
    if (isset(
    $_SERVER['QUERY_STRING'])) {
      
    $editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
    }

    if ((isset(
    $_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      
    define('UPLOAD_DIR''../../photos/staff/');
      
    $file str_replace(' ''_'$_FILES['staff_photo']['name']);
      
    $permitted = array('image/gif''image/jpeg''image/pjpeg''image/png');
      
    $typeOK false;
      

      foreach (
    $permitted as $type) {
        if (
    $type == $_FILES['staff_photo']['type']) {
          
    $typeOK true;
          break;
          }
        }
        
    if (
    $typeOK) {
        
        switch(
    $_FILES['staff_photo']['error']) {
          case 
    0:
            if (!
    file_exists(UPLOAD_DIR.$file)) {
                
    $success move_uploaded_file($_FILES['staff_photo']['name'], UPLOAD_DIR.$file);      
            } 
            
            if (
    $success) {
            
    $insertSQL sprintf("INSERT INTO staff (staff_name, staff_position, staff_subposition, staff_desc, staff_photo, staff_spec_1, staff_spec_2, staff_spec_3, staff_spec_4, staff_spec_5) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                           
    GetSQLValueString($_POST['staff_name'], "text"),
                           
    GetSQLValueString($_POST['staff_position'], "text"),
                           
    GetSQLValueString($_POST['staff_subposition'], "text"),
                           
    GetSQLValueString($_POST['staff_desc'], "text"),
                           
    GetSQLValueString($_POST['staff_photo'], "text"),
                           
    GetSQLValueString($_POST['staff_spec_1'], "text"),
                           
    GetSQLValueString($_POST['staff_spec_2'], "text"),
                           
    GetSQLValueString($_POST['staff_spec_3'], "text"),
                           
    GetSQLValueString($_POST['staff_spec_4'], "text"),
                           
    GetSQLValueString($_POST['staff_spec_5'], "text"));

      
    mysql_select_db($database_wvgsadmin$wvgsadmin);
      
    $Result1 mysql_query($insertSQL$wvgsadmin) or die(mysql_error());

       if (
    $Result1) {
       
       
    $insertGoTo "staff_list.php";
        if (isset(
    $_SERVER['QUERY_STRING'])) {
        
    $insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
        
    $insertGoTo .= $_SERVER['QUERY_STRING'];
        
      
    header(sprintf("Location: %s"$insertGoTo));
      }
      }
      } else {    
                
    $result "Error uploading $file. Please try again.";
            }    
          break;
          case 
    3:      
            
    $result "Error uploading $file. Please try again.";
          default:
            
    $result "System error uploading $file. Contact webmaster.";
          }
          
    } else {
        
    $result "$file cannot be uploaded. Acceptable file type: .gif, .jpf, .png";
    }
    }
    ?> 
    Thank you for your time!
    toad78

  2. #2
    SitePoint Evangelist achintya's Avatar
    Join Date
    Apr 2005
    Location
    Chandannagar(India)
    Posts
    459
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think you have got a problem with your naming in the html as there are 3 fields with name 'staff_photo'. So please give them different names and try running the code again.

  3. #3
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If I may add a tip on debugging forms, print out the $_POST variable

    PHP Code:
    <?php
    // simulating a form
    $_POST['pet']='cats';
    $_POST['pest']='rat';

    // showing form data
    echo "_POST= \n<pre>\n";
    print_r ($_POST);
    echo 
    "</pre>\n";
    ?>
    this would have show the incoming data

    Code:
    _POST=
    Array
    (
        [pet] => cats
        [pest] => rat
    )
    Denny Schlesinger
    web services

  4. #4
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    822
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Hmm...never thought of it that way. I'll try that.

    Thank you!


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
  •