Jump to content

Editing existing records help


dm404

Recommended Posts

I've come up against a few problems when updating existing records & was hoping someone could help.

 

In short the user (a venue) can update their profile when they are logged in. I'm ok with updating text fields etc.

 

The problems are arising around an optional image field (VenueLogo):

 

1. My if else statements are not working correctly, I've tried adjusting them but with no success. When no image is uploaded in the form, the update should occur on all other fields except the image. However when I click the submit button, and no image is uploaded, it still wants to process the image and I get the error:

 

Please upload an image file such as jpeg, jpg, gif

 

An error occurred in script 'C:\xampp\htdocs\mywebsite\updatevenue.php' on line 179:

unlink() [function.unlink]: No error

 

2. The first time an image is uploaded, I can get it to appear as a picture in the form. However when I start updating that field with a new image, then update it back to a previously chosen image I always get a red cross as opposed to the currently chosen image. I'm guessing this is a problem to do with the way files are stored on the server.

 

When I got php to rename the file, I couldn't keep the image properties so images were just being saved as plain files. Is there a way to keep the image property of the file whilst getting php to rename it? Is it neccessary to delete the old files if they are updated?

 

Here is my code so far.:

 

 

<?php // Script 14 updatevenue.php

// Start output buffering.
ob_start();

// Initialize a session.
session_start();

// Include error management file
require_once ('C:\xampp\htdocs\mywebsite\config.inc.php');

if(!session_is_registered('VenueName')){

// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
// Add the page.
$url .= '/events.php';

ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.




} else {

$vid = $_SESSION['VenueID']; //Set venue variable

}
// Connect to the database
require_once ('C:\xampp\htdocs\mysql_connect.php');

// Check if form has been submitted

if (isset($_POST['submitted'])) {


// Check for a venue name

if (eregi ('^[[:alnum:]\.\' \-]{1,60}$', stripslashes(trim($_POST['venuename'])))) {
$vnm = escape_data($_POST['venuename']);
} else {
$vnm = FALSE;
echo '<p><font color="red" size="+1">Please enter the venue name!</font></p>';
}

// Check for venue address 1

if (eregi ('^[[:alnum:]\.\' \-]{1,100}$', stripslashes(trim($_POST['venueaddress1'])))) {
$va1 = escape_data($_POST['venueaddress1']);
} else {
$va1 = FALSE;
echo '<p><font color="red" size="+1">Please enter your last name!</font></p>';
}

// Set venue address2 variable

$va2 = escape_data($_POST['venueaddress2']);


// check for postcode

if (eregi ('^[[:alnum:]\.\' \-]{1,8}$', stripslashes(trim($_POST['venuepostcode'])))) {
$vpc = escape_data($_POST['venuepostcode']);
} else {
$vpc = FALSE;
echo '<p><font color="red" size="+1">Please enter the venue postcode!</font></p>';
}

// Check for venue phone contact

if (eregi ('^[[:digit:]\.\' \-]{8,15}$', stripslashes(trim($_POST['venuephone'])))) {
$vph = escape_data($_POST['venuephone']);
} else {
$vph = FALSE;
echo '<p><font color="red" size="+1">Please enter the venue contact number !</font></p>';
}

// Check for venue description

if (!empty($_POST['venuedescription'])) {
$vde = escape_data($_POST['venuedescription']);

} else {

$vde = NULL;
echo '<p><font color="red" size="+1">Please enter a description of the venue for our venue guide prints and vetting purposes!</font></p>';

}
// Check for venuedirections

if (!empty($_POST['venuedirections'])) {
$vdi = escape_data($_POST['venuedirections']);

} else {

$vdi = NULL;
echo '<p><font color="red" size="+1">Please enter directions for the venue for our venue guide prints and vetting purposes!</font></p>';

}

// Check if image is empty

if (empty($_FILES['venuelogo'])) {

if ($vnm && $va1 && $va2 && $vpc && $vph && $vde && $vdi) {

$query27 = "UPDATE venue SET VenueName='$vnm', VenueAddress1='$va1', VenueAddress2='$va2', VenuePostcode='$vpc', VenuePhone='$vph', VenueDescription='$vde', VenueDirections='$vdi' WHERE VenueID=$vid";
$result27 = @mysql_query ($query27); // Run the query.
if (mysql_affected_rows() ==1) { // If it ran ok.

// Print a message


echo '<h3>Your venue has been updated. Click<a href="venue.php">here to return</a> </h3>';

exit();

// Print a message

} else { // Didn't run ok

echo '<h3>Your venue has NOT been updated. We apologize! Click<a href="venue.php">here to return</a> </h3>';

}

}

} else { // Process if image was added

if (isset($_FILES['venuelogo'])) {

$allowed = array ('image/gif', 'image/jpeg', 'image/jpp', 'image/pjpeg', 'image/bmp');

if (in_array($_FILES['venuelogo']['type'], $allowed)) {

// Move the file over

if (move_uploaded_file($_FILES['venuelogo']['tmp_name'], "uploads/{$_FILES['venuelogo']['name']}")){

} else { // Couldn't move the file over

echo '<p><font color="red" size="+1">File could not be moved over because: <b>';

//Print a message based on the error.
switch ($_FILES['venuelogo']['error']) {

case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini.';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;
case 3:
print 'The file was only partially uploaded.';
break;
case 4:
print 'No file was uploaded.';
break;
case 6:
print 'No temporary folder was available.';
break;
default:
print 'A system error occurred.';
break;
} // End of switch.

print '</b></font>.</p>';

} // End of move....IF

} else { //Invalid image type
echo '<p><font color="red" size="+1">Please upload an image file such as jpeg, jpg, gif<b></font>';


@unlink ($_FILES['venuelogo']['tmp_name']); // Delete file.

}

// Set variable

$vlg =($_FILES['venuelogo']['name']);

}



if ($vnm && $va1 && $va2 && $vpc && $vph && $vde && $vdi && $vlg) { // All good


// Ma\ke the query

$query17 = "UPDATE venue SET VenueName='$vnm', VenueAddress1='$va1', VenueAddress2='$va2', VenuePostcode='$vpc', VenuePhone='$vph', VenueDescription='$vde', VenueDirections='$vdi', VenueLogo='$vlg' WHERE VenueID=$vid";
$result17 = @mysql_query ($query17); // Run the query.
if (mysql_affected_rows() ==1) { // If it ran ok.

// Print a message


echo '<h3>Your venue has been updated. Click<a href="venue.php">here to return</a> </h3>';

exit();

// Print a message

} else { // Didn't run ok

echo '<h3>Your venue has NOT been updated. We apologize! Click<a href="venue.php">here to return</a> </h3>';

}

} // End of image proccess


}

}





// retrieve venue info.

$query23 = "SELECT * FROM venue WHERE VenueID=$vid";
$result23 = @mysql_query ($query23); // Run the query

if (mysql_affected_rows() ==1) { // If it ran ok, show the form

// Get user info

$row = mysql_fetch_array ($result23, MYSQL_ASSOC);

// Create the form

?>


<form enctype="multipart/form-data" action="updatevenue.php" method="post">

<p><b>Venue Name: </b> <input type="text" name="venuename" size="15" maxlength="15" value=<?php echo "'$row[VenueName]'" ?>/></p>
<p><b>Address 1:</b> <input type="text" name="venueaddress1" size="15" maxlength="15" value=<?php echo "'$row[VenueAddress1]'" ?>/></p>
<p><b>Address 2:</b> <input type="text" name="venueaddress2" size="15" maxlength="15" value=<?php echo "'$row[VenueAddress2]'" ?>/></p>
<p><b>Postcode:</b> <input type="text" name="venuepostcode" size="15" maxlength="15" value=<?php echo "'$row[VenuePostcode]'" ?>/></p>

<p><b>Phone:</b> <input type="text" name="venuephone" size="15" maxlength="15" value=<?php echo "'$row[VenuePhone]'" ?>/></p>

<p><b>Description:</b> <textarea rows="10" cols="50" name="venuedescription"><?php echo "$row[VenueDescription]" ?></textarea> </p>

<p><b>Directions:</b> <textarea rows="5" cols="50" name="venuedirections"><?php echo "$row[VenueDirections]" ?></textarea> </p>

<p><b>Website:</b> <input type="text" name="venuewebsite" size="40" maxlength="60" value=<?php echo "'$row[VenueWebsite]'" ?>/></p>

<p>Add Logo (optional):<input type="file" name="venuelogo" size="30"> <?php


echo "Current logo: <img src=http://localhost/mywebsite/uploads/".$row['VenueLogo'] ."> <br>";
}

?>




<div align="center"><input type="submit" name="submit" value="Update Venue Profile" /></div>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="venueid" value="$vid" />

<form/>

 

 

Any help is much appreciated.

 

 

Thanks in advance,

 

 

Daniel

Link to comment
https://forums.phpfreaks.com/topic/103134-editing-existing-records-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.