Hello all,
I've created an image upload form that has been working successfully when I try to add a listing with an image or delete image information from the database (I am not storing the image in the database, I move the file to an images folder the server). I'm running into problems when I try to update an already existing listing.
When I was error catching, MySQL echoed an error that showed me that '$photo' was blank on the editsave.
php page (the page where the actual MySQL UPDATE command takes place). I'm not sure why it is not carrying to this page. Any help would be lovely!
Here's the code.
edit.php
- PHP: Select all
<?php
if (isset($_POST['submit'])) {
include("../connect.php");
$query = "SELECT * FROM sponsors WHERE sponsor_id={$_POST['sponsor_id']}";
$results = mysql_query($query);
$row = mysql_fetch_array($results) or die(mysql_error());
$sponsor_id = $row['sponsor_id'];
$name = htmlentities($row['name']);
$website = htmlentities($row['website']);
$element = htmlentities($row['element']);
$since = $row['since'];
trim($level);
addslashes($name);
addslashes($element);
?>
<form action="sponsor_editsave.php" method="POST">
<p>Sponsor/Company Name: <input type="text" name="name" size="30" maxsize="100" value="<?php echo $name; ?>" /></p>
<p>Website: <input type="text" name="website" size="30" maxsize="40" value="<?php echo $website; ?>" /></p>
<p>Element: <input type="text" name="element" size="20" maxsize="30" value="<?php echo $element; ?>" /></p>
<p>Sponsor since: <input type="text" name="since" size="4" maxsize="4" value="<?php echo $since; ?>" /></p>
<?php
if ($photo != NULL){
echo "<img src=\"http://www.campyale.com/sponsors/images/".$row['photo'] ."\" />";
?>
</form>
<form action="sponsor_delete_logo.php" method="post">
<input type="hidden" name="sponsor_id" value="<?php echo $sponsor_id; ?>">
<p><input type="submit" name="logo_delete" value="Delete Logo" /></p>
</form>
<form action="sponsor_editsave.php" method="POST">
<?php
} else {
?>
<p>Sponsor Logo: <input type="file" name="photo" /></p>
<?php
}
?>
<form action="sponsor_editsave.php" method="post">
<input type="hidden" name="sponsor_id" value="<?php echo $sponsor_id; ?>">
<p><input type="submit" name="submit" value="Save Changes" /></p>
</form>
<form action="index.php" method="post">
<p><input type="submit" name="submit" value="Cancel" /></p>
</form>
</form>
<?php
} else {
echo "<p>Info not properly submitted. </p>";
mysql_close(); //Closes our SQL session
}
?>
editsave.php
- PHP: Select all
<?php
//This is the directory where images will be saved
$target = "../images/";
$target = $target . basename( $_FILES['photo']['name']);
if (isset($_POST['submit'])) {
// connect to database
include("../connect.php");
$sponsor_id = $_POST['sponsor_id'];
$name = $_POST['name'];
$website = $_POST['website'];
$element = $_POST['element'];
$since = $_POST['since'];
$photo = ($_FILES['photo']['name']);
$website = htmlentities($website);
$name = trim($name);
$element = trim($element);
$name = addslashes($name);
$element = addslashes($element);
//Define the query
$query = "UPDATE sponsors SET name='$name', website='$website', element='$element', since='$since', photo='$photo' WHERE sponsor_id={$sponsor_id}";
//run the query
if (@mysql_query ($query)) {
?>
<p>The sponsor was successfully updated.</p>
<?php
//Writes the photo to the server
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target) or die (mysql_error())) {
//Tells you if its all ok
echo "<p>The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and the information has been updated in the directory</p>";
} else {
//Gives an error if its not
echo "<p>Sorry, there was a problem uploading your file.</p>";
}
} else {
?>
<p>The sponsor could not be saved.</p>
<?php
mysql_close(); //Closes our SQL session
}
} else {
?>
<p>There is no data to process.</p>
<?php
}
?>
Thanks for any help!
Also, the page stops loading right at the:
- PHP: Select all
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target) or die (mysql_error())) {
If that offers any clues...
Thanks again!