Help determining is input has changed upon form submit.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kleptik
Forum Newbie
Posts: 1
Joined: Mon Nov 13, 2006 8:25 pm

Help determining is input has changed upon form submit.

Post by kleptik »

As you will be able to tell from my, *cough* code, i've very new to PHP. Basically I have a form (addlisting.php) that works fine, It uses an input type="file" to upload .jpg's, aswell as other input fields. Example of it here: http://www.leaveittobeever.com/listings.php

I also have an editlisting.php file that allows the user to edit the listing.

My question is, how do I tell if they have used the input type="file" to update the image? If they have uploaded a new image, I want to execute the sql to update the "Image" field with the image name, and if they have NOT selected to upload a new image, I want to leave the exisiting value in "Image" field in the DB alone.

I've tried something like this, I'm not sure if I'm just using incorrect synatax, or if I'm just way off alltogether.

Code: Select all

if (isset($uploadFile)) {
	          $query = "UPDATE ListingsTable SET Image='".$Image."' WHERE id='".$id."'";
					  		$result = $database->query($query);
		echo $query;
     	}
The entire editListing.PHP file below:

Code: Select all

<?php
@session_start();
  // get contact id
  $id = $_GET['id'];
  if (!empty($id)) { $_SESSION['recordId']=$id; }
  else { $id = $_SESSION['recordId']; }

  // if the script has been called without ID or the user hit "Cancel" just return to listing
  if (empty($id) || isset($_POST['cancel'])) {
    Header("Location: Listingslist.php");
    exit;
  }

  include('dbconnect.php');
  // run this only, once the user has hit the "OK" button
  if (isset($_POST['ok'])) {

    // assign form inputs
 	$ListingTitle = $_POST['ListingTitle'];
    $Description = $_POST['Description'];
    $Price = $_POST['Price'];
    $MLSNumber = $_POST['MLSNumber'];
    $Location = $_POST['Location'];
 	$Bedrooms = $_POST['Bedrooms'];
 	$Bathrooms = $_POST['Bathrooms'];
 	$SqFeet = $_POST['SqFeet'];
	$Sold = $_POST['Sold'];
	$Image = $_POST['Image'];
	$uploadFile = $_POST['uploadFile'];

    // validate inputs
    if ( !empty($ListingTitle) && !empty($Description) ) {

	  move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],"../images/{$_FILES['uploadFile'] ['name']}");
      $Image = $_FILES['uploadFile'] ['name'];

		if ($Sold == '') {
		 $Sold = '0';
		}

	    if (isset($uploadFile)) {
	          $query = "UPDATE ListingsTable SET Image='".$Image."' WHERE id='".$id."'";
					  		$result = $database->query($query);
		echo $query;
     	}



	// add listing to BD
      $query = "UPDATE ListingsTable SET SqFeet='". $SqFeet. "',Bathrooms='". $Bathrooms. "',ListingTitle='". $ListingTitle. "',Description='". $Description. "',Price='". $Price. "',MLSNumber='". $MLSNumber. "',Location='". $Location. "',Bedrooms='". $Bedrooms. "',Sold='". $Sold. "' WHERE id='". $id. "'";
      $result = $database->query($query);
      $database->disconnect();
	  echo $query;

	  echo '<BR><BR><a href="Listingslist.php">Continue</a>';

  	  // echo '<meta http-equiv="refresh" content="2;URL=Listingslist.php">';
      //Header("Location: listListings.php");

      //Header("Location: Listingslist.php");
      exit;
    }
    else {
      $error = true; // input validation failed
    }
  }
  else { // read member data from database
    $query = "SELECT * FROM ListingsTable WHERE id='".$id."'";
    $result = $database->query($query);
    $contact = $result->fetchRow(DB_FETCHMODE_ASSOC,0);
    $database->disconnect();

	$ListingTitle = $contact['ListingTitle'];
    $Description = $contact['Description'];
    $Price = $contact['Price'];
    $MLSNumber = $contact['MLSNumber'];
    $Location = $contact['Location'];
 	$Bedrooms = $contact['Bedrooms'];
 	$Bathrooms = $contact['Bathrooms'];
 	$SqFeet = $contact['SqFeet'];
 	$imageUrl = $contact['Image'];
 	$Sold = $contact['Sold'];
   }
?>
<html>
<head>
  <title>Edit Listing</title>
</head>
<body>
<a href="addListing.php">Add a Listing</a> | <a href="Listingslist.php">Site Listings</a>
<h1>Edit Listing</h1>
<form name="form1" enctype="multipart/form-data"  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <BR>Listing Title:<BR>
  <input name="ListingTitle" size="60" type="text" value="<?php echo $ListingTitle; ?>">
  <BR>MLS Number:<BR>
  <input name="MLSNumber" type="text" value="<?php echo $MLSNumber; ?>">
  <br>
  <BR>Price:<BR>
  <input name="Price" type="text" value="<?php echo $Price; ?>">
  <BR>
  <BR>Address:<BR>
  <input name="Location" size="60" type="text" value="<?php echo $Location; ?>">
  <br>
  <BR>Number of Bedrooms:<BR>
    <input name="Bedrooms" size="20" type="text" value="<?php echo $Bedrooms; ?>">
  <br>
  <BR>Number of Bathrooms:<BR>
    <input name="Bathrooms" size="20" type="text" value="<?php echo $Bathrooms; ?>">
  <br>
  <BR>Square Feet:<BR>
    <input name="SqFeet" size="20" type="text" value="<?php echo $SqFeet; ?>">
  <br>
  <br>

Listing Description:<br>
<textarea name="Description" cols="70" rows="6"><?php echo $Description; ?></textarea>
<BR><BR>
Current Photo Filename: <BR>
<input size="60" name="Image" disabled="true" value="<?php echo $imageUrl; ?>"><BR>
<BR>
<?php echo '<A target="_blank" Href="http://searchlink.las.interealty.com/PropSearch/PropertyDetail.asp?Code=&ML=',$MLSNumber,'&AgentID=210064"><IMG border="0" width="200" height="150" SRC="../../images/',$imageUrl,'"></A>';?>
<BR>

<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />

<BR>Current Photo Of Listing: (Click "Browse" to select an updated photo.)<BR>
<input name="uploadFile" size="60" type="file" /><BR><BR>


<input type="checkbox" name="Sold" value="1" <?php if ($Sold==1) echo "checked"; ?>>
  <B>SOLD?</B> (Check if the listing is sold.)<br><BR>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="submit" name="ok" value="    OK    ">
<input type="submit" name="cancel" value="Cancel">
</form>
</body>
</html>
Many thanks for your time and expertise.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

to determine if someone has used the "FILE" input to upload a new file or replace a file

check for the PHP predefined variable

Code: Select all

$_FILES
see the PHP manual for more info
User avatar
zeek
Forum Commoner
Posts: 48
Joined: Mon Feb 27, 2006 7:41 pm

An example...

Post by zeek »

An example would be to replace:

Code: Select all

if (isset($uploadFile)) {
with...

Code: Select all

if ( $_FILES['uploadFile'] ) {
Post Reply