-
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
$pic1= basename($_FILES['photo']['name'][0]);
$pic2= basename($_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:
$pic1= basename($_FILES['photo']['name'][0]);
$pic2= basename($_FILES['photo']['name'][1]);
can anyone help point me in the right direction please
Thank you in advance, appreciate it
-