Hi:
I try to write a update script for web admin that allows them to replace the current data in the database and replace them with new data and do the same as well on the upload folder for files.
Problem:
The script is actually replace the data in database, but it does not replace the file in the upload folder.
Can anybody shed me a light here.
Here is my script
Editing script-form
// edit album
if ($rvar_action == 'edit_album')
{
$album_id = $_GET['album_id'];
$artist_query = "SELECT artist.artist_id, artist.artist_name, artist.english_name, artist.gender, artist.region, lyric.lyric_id, lyric.artist_id, lyric.album_id, lyric.song_title, lyric.lyric, lyric.mixed_artist_id, album.album_id, album.artist_id, album.album_image_id, album.album_name, album.album_image_type, album.album_image_size, album.image_date, album.album_image, album.album_info, album.album_yr, album.album_company FROM artist LEFT OUTER JOIN lyric on (artist.artist_id = lyric.artist_id) LEFT OUTER JOIN album on (lyric.artist_id = album.artist_id) WHERE album.album_id = $album_id";
$artist_result=mysql_query($artist_query) or die(mysql_error());
while($row=mysql_fetch_array($artist_result))
{
$artist_id = $row['artist_id'];
$artist_name = $row['artist_name'];
$english_name = $row['english_name'];
$gender = $row['gender'];
$region = $row['region'];
$album_name = $row['album_name'];
$album_yr = $row['album_yr'];
$album_company = $row['album_company'];
$album_info = $row['album_info'];
$album_image = $row['album_image'];
}
echo '
<form enctype="multipart/form-data" action="admin-album.php?action=submit_edit_album" method="POST">
<INPUT NAME="album_id" VALUE="'.$album_id.'" size=40><br \>
<b>Artist ID: '.$artist_id.'</b>
<INPUT NAME="artist_id" VALUE="'.$artist_id.'" size=40><br \>
<b>Artist Name: '.$artist_name.'</b>
<INPUT NAME="artist_name" VALUE="'.$artist_name.'" size=40><br \>
<b>English Name: '.$english_name.'</b>
<INPUT NAME="artist_name" VALUE="'.$english_name.'" size=40><br \><br \>
<b>Old Album Name: '.$album_name.'</b><br \>
<b>New Album Name: </b><INPUT NAME="album_name" VALUE="'.$album_name.'" size=40><br \>
<br \><b>Album Year: </b><select name="album_yr">';
$i = 1960;
$recent_year = date("Y");
while($i <= $recent_year)
{
echo '<option value="' . $i . '"';
if ($i == $album_yr) {echo ' selected';}
echo '>' . $i . '</option>';
$i++;
}
echo '</select><br \>
<br \><b>Old Album Info:</b>
<br \>'.$album_info.'
<br \><br \><b>New Album Info:</b>
<br \><TEXTAREA NAME="album_info" VALUE="album_info" cols=40 rows=5>'.$album_info.'</TEXTAREA></td><br \>
<b>Current Album Photo:</b><br \><br \>
<img src="'.$album_image.'"><br \>
<b>Edit Album Photo:</b><input name="uploaded" type="file" /><br \>
<br \><input type="Submit" value="Update">
</form>
';
}
//edit album
Here is the result page
// submit Edit Album
if ($rvar_action == 'submit_edit_album')
{
$album_id = $_POST['album_id'];
$artist_query = "SELECT artist.artist_id, artist.artist_name, artist.english_name, artist.gender, artist.region, lyric.lyric_id, lyric.artist_id, lyric.album_id, lyric.song_title, lyric.lyric, lyric.mixed_artist_id, album.album_id, album.artist_id, album.album_image_id, album.album_name, album.album_image_type, album.album_image_size, album.image_date, album.album_image, album.album_info, album.album_yr, album.album_company FROM artist LEFT OUTER JOIN lyric on (artist.artist_id = lyric.artist_id) LEFT OUTER JOIN album on (lyric.artist_id = album.artist_id) WHERE album.album_id = $album_id";
$row = mysql_fetch_array(mysql_query($artist_query)) or die(mysql_error());
$album_image = $row['album_image'];
$file = "$album_image";
if(file_exists($file))
{
unlink($file);
}
$target = "upload/";
$target = $target . basename($_FILES['uploaded']['name']);
$ok=1;
$query = ("UPDATE album SET album_name='$album_name',album_yr='$album_yr',album_info='$album_info',album_image='$target' WHERE album_id = '$album_id'");
$Result = mysql_query($query) or die('Error in query: ' . mysql_error());
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The File ". basename($_FILES['uploaded']['name']). " Has Been Uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
chmod("$target", 755);
echo '<br \>Finishing Adding album.';
}
// submit Edit Album