Results 1 to 2 of 2

Thread: image upload change

  1. #1
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default image upload change

    Hi

    I am winning, I got the signup form working with the multiple images upload and got the view listings working so only the logged in user can view their own listings and am now working on the edit listings page and the other input fields work all ok apart from the multiple image upload

    am looking to allow the user to upload new images and replace the original images uploaded by the user, so for example if the user uploads image1.jpg and image2.jpg originally and then edits their listing and uploads new images called imagenew1.jpg and imagenew2.jpg, would them new images to be uploaded on the database and server

    the coding I got for the HTML for is below

    HTML Code:
    <form action="" method="post" enctype="multipart/form-data">
     <input type="hidden" name="id" value="<?php echo $id; ?>"/>
     <div>
     <strong>Listing Title: *</strong> <input type="text" name="listingtitle" value="<?php echo $listingtitle; ?>"/>
     <br/>
     <strong>Make: *</strong> <input type="text" name="make" value="<?php echo $make; ?>"/>
     <br/>
     <strong>Model: *</strong> <input type="text" name="model" value="<?php echo $model; ?>"/>
     <br/>
     <strong>Exterior Colour: *</strong> <input type="text" name="exteriorcolour" value="<?php echo $exteriorcolour; ?>"/>
     <br/>
     <strong>Engine Size: *</strong> <input type="text" name="enginesize" value="<?php echo $enginesize; ?>"/>
     <br/>
     <strong>Fuel Type: *</strong> <input type="text" name="fueltype" value="<?php echo $fueltype; ?>"/>
     <br/>
     <strong>Year Registered: *</strong> <input type="text" name="yearregistered" value="<?php echo $yearregistered; ?>"/>
     <br/>
     <strong>Transmission: *</strong> <input type="text" name="transmission" value="<?php echo $transmission; ?>"/>
     <br/>
     <strong>Mileage: *</strong> <input type="text" name="mileage" value="<?php echo $mileage; ?>"/>
     <br/>
     <strong>Number of Doors: *</strong> <input type="text" name="nodoors" value="<?php echo $nodoors; ?>"/>
     <br/>
     <strong>Body Style: *</strong> <input type="text" name="bodystyle" value="<?php echo $bodystyle; ?>"/>
     <br/>
     <strong>Price: *</strong> <input type="text" name="price" value="<?php echo $price; ?>"/>
     <br/>
     <strong>Photo One:</strong> <input type='hidden' name='size' value='350000'><input type='file' name='photo[]'>
     <br>
     <strong>Photo Two:</strong> <input type='hidden' name='size' value='350000'><input type='file' name='photo[]'>
     <br>
     <input type="submit" name="submit" value="Submit">
     </div>
     </form>
    The code for the php upload and UPDATE query is below

    PHP Code:
    <?php
    /* 
     EDIT.PHP
     Allows user to edit specific entry in database
    */

    session_start();

     
    //This is the directory where images will be saved
    $target "private-listing-images/";

     
    // creates the edit record form
     // since this form is used multiple times in this file, I have made it a function that is easily reusable
     
    function renderForm($id$listingtitle$make$model$exteriorcolour$enginesize$fueltype$yearregistered$transmission$mileage$nodoors$bodystyle$price$pic1$pic2$error)
     {
     
    ?>
    PHP Code:
    <?php
    // connect to the database
     
    include('includes/connect-db.php');
     
     
    // check if the form has been submitted. If it has, process the form and save it to the database
     
    if (isset($_POST['submit']))
     { 
     
    // confirm that the 'id' value is a valid integer before getting the form data
     
    if (is_numeric($_POST['id']))
     {
     
    // get form data, making sure it is valid
     
    $id $_POST['id'];
     
    $listingtitle $_POST['listingtitle'];
     
    $make $_POST['make'];
     
    $model $_POST['model'];
     
    $exteriorcolour $_POST['exteriorcolour'];
     
    $enginesize $_POST['enginesize'];
     
    $fueltype $_POST['fueltype'];
     
    $yearregistered $_POST['yearregistered'];
     
    $transmission $_POST['transmission'];
     
    $mileage $_POST['mileage'];
     
    $nodoors $_POST['nodoors'];
     
    $bodystyle $_POST['bodystyle'];
     
    $price $_POST['price'];

     
    //$lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
     
     // check that firstname/lastname fields are both filled in
     
    if ($listingtitle == '')
     {
     
    // generate error message
     
    $error 'ERROR: Please fill in all required fields!';
     
     
    //error, display form
     
    renderForm($id$listingtitle$make$model$exteriorcolour$enginesize$fueltype$yearregistered$transmission$mileage$nodoors$bodystyle$price$pic1$pic2$error);
     }
     else
     {
         
     
    // save the data to the database
     
    mysql_query("UPDATE privatelistings SET listingtitle='$listingtitle', make='$model', model='$model', exteriorcolour='$exteriorcolour', enginesize='$enginesize', fueltype='$fueltype', yearregistered='$yearregistered', transmission='$transmission', mileage='$mileage', nodoors='$nodoors', bodystyle='$bodystyle', price='$price', photo='$pic1', photo1='$pic2' WHERE id='$id'")
     or die(
    mysql_error()); 
     
     
    // once saved, redirect back to the view page
     
    header("Location: view-private-listings-info.php?id={$_SESSION['user_id']}"); 
     }
     }
     else
     {
     
    // if the 'id' isn't valid, display an error
     
    echo 'Error!';
     }
     }
     else
     
    // if the form hasn't been submitted, get the data from the db and display the form
     
    {
     
     
    // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
     
    if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
     {
     
    // query db
     
    $id $_GET['id'];
     
    $result mysql_query("SELECT * FROM privatelistings WHERE id=$id")
     or die(
    mysql_error()); 
     
    $row mysql_fetch_array($result);
     
     
    // check that the 'id' matches up with a row in the databse
     
    if($row)
     {
     
     
    // get data from db
     
    $listingtitle $row['listingtitle'];
     
    $make $row['make'];
     
    $model $row['model'];
     
    $exteriorcolour $row['exteriorcolour'];
     
    $enginesize $row['enginesize'];
     
    $fueltype $row['fueltype'];
     
    $yearregistered $row['yearregistered'];
     
    $transmission $row['transmission'];
     
    $mileage $row['mileage'];
     
    $nodoors $row['nodoors'];
     
    $bodystyle $row['bodystyle'];
     
    $price $row['price'];
     
     
    // use static values in that case
    $pic1basename($_FILES['photo']['name'][0]);

    $pic2basename($_FILES['photo']['name'][1]);

    if(!empty(
    $_FILES['photo']['tmp_name']))
    {
        
        
    // Number of uploaded files
         
    $num_files count($_FILES['photo']['tmp_name']);

         
    /** loop through the array of files ***/
         
    for($i=0$i $num_files;$i++)
         {
             
    // check if there is a file in the array
             
    if(!is_uploaded_file($_FILES['photo']['tmp_name'][$i]))
             {
                 
    $messages[] = 'No file uploaded';
             }
             else
             {
                 
    // move the file to the specified dir
                 
    if(move_uploaded_file($_FILES['photo']['tmp_name'][$i],$target.'/'.$_FILES['photo']['name'][$i]))
                 {
                
                     
    $messages[] = $_FILES['photo']['name'][$i].' uploaded';
                    
                 }
                 else
                 {
                     
    // an error message
                     
    $messages[] = 'Uploading '.$_FILES['photo']['name'][$i].' Failed';
                 }
             }
         }
     
     
    // show form
     
    renderForm($id$listingtitle$make$model$exteriorcolour$enginesize$fueltype$yearregistered$transmission$mileage$nodoors$bodystyle$price$pic1$pic2'');
     }
     }
     else
     
    // if no match, display result
     
    {
     echo 
    "No results!";
     }
     }
     else
     
    // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
     
    {
     echo 
    'Error!';
     }
     }
    ?>
    With that coding, I get the following error on the edit listing page

    Notice: Undefined index: photo in edit-private-listings-info.php on line 205
    Notice: Undefined index: photo in edit-private-listings-info.php on line 207

    on them lines is the following

    PHP Code:
    $pic1basename($_FILES['photo']['name'][0]);

    $pic2basename($_FILES['photo']['name'][1]); 
    can anyone help point me in the right direction please

    Thank you in advance, appreciate it

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    daalevis (10-21-2014)

Similar Threads

  1. Request for Max Upload Filesize Change
    By groogruxking40 in forum PHP
    Replies: 1
    Last Post: 09-26-2009, 05:38 AM
  2. Image upload
    By thallasridevi in forum PHP
    Replies: 1
    Last Post: 07-23-2008, 04:18 PM
  3. Image Upload
    By dimmemo in forum Other
    Replies: 0
    Last Post: 07-22-2008, 01:36 PM
  4. how can i do file upload control to upload any image
    By Rai_87 in forum MySQL and other databases
    Replies: 0
    Last Post: 07-04-2008, 01:36 AM
  5. Upload image WHERE?? HELP!
    By lisav in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 01-06-2005, 08:39 AM

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
  •