Forum Moderators: coopster
I have a database where i want to be able to replace an image file in a folder and the associated database entry.
I want to keep the rest of the data in the row. Here is the current FORM SUBMISSION CODE that I use to submit to the database.
*** I would like to be able to delete the image file and the column "file" and replace it using a form.
Currently I can look at the list and modify each music review but I cannot edit the 'file' field for the image - which is a FileField.
THE SUBMISSION SCRIPT
<?php
include("../inc/db.mr.inc.php");
// IMAGE UPLOAD CONFIG
$serverpath = "/home/****/public_html/images/review_photos/";// Path to where images should be uploaded to on the server.
$urltoimages = "http://www.my-website.com/images/review_photos/"; // Web address to where the images are accessible from.
$maxsize = "180000"; // Example - 20000 is the same as 20kb
// CONFIG END
if ($photo == "1") {
$file = $_FILES['file']['name'];
// If you add your own file types don't forget to add an uppercase version.
$allowedfiles[] = "gif";
$allowedfiles[] = "jpg";
$allowedfiles[] = "jpeg";
$allowedfiles[] = "png";
$allowedfiles[] = "GIF";
$allowedfiles[] = "JPG";
$allowedfiles[] = "JPEG";
$allowedfiles[] = "PNG";
if($_FILES['file']['size'] > $maxsize)
{
print "File size is too big - please reduce file size and try again.";
}
else {
$path = "$serverpath/$file";
foreach($allowedfiles as $allowedfile) {
if ($done <> "yes") {
if (file_exists($path)) {
echo "A file with this name already exists - please rename the file and reupload.";
exit;
}
}
if (substr($file, -3) == $allowedfile) {
move_uploaded_file($_FILES['file']['tmp_name'], "$path");
$done = "yes";
echo "<table><tr><td class=p>Your image has been successfully uploaded to our server and can be accessed using the URL provided below.</td></tr>";
echo "<tr><td></td></tr>";
echo "<tr><td><a href='$urltoimages/$file' target='_blank'><strong>$file</strong></a></td></tr>";
echo "<tr><td></td></tr>";
echo "<tr><td><img src=$urltoimages/$file border=0 width=250></td></tr></table>";
}
}
if ($done <> "yes") { print "<p><b>Error:</b> Your image as not been uploaded becuase it is not a recognised image file. Please try again.</p>"; }
}
}
$insert_date = date("Y-m-d H:i:s");
$review=ereg_replace(10,"<br>",$review);
$sql_secure_check = mysql_query("SELECT secure FROM review
WHERE secure='$secure'");
$secure_check = mysql_num_rows($sql_secure_check);
if($secure_check > 0){
echo "<h1>OOPS! Please fix the following errors:</h1> <br /><br>";
if($secure_check > 0){
echo "<strong><h3>Your Review has already been entered
into our database. <br><br>Please submit a new Music Review!</h3></strong><br />";
unset($secure);
}
exit(); // exit the script so that we do not create this account!
}
if ($photo == 1){
$sql = "INSERT INTO review (insert_date, rating, artist, album, release_date, label, country, genre, http_1, url_1, review, reviewer, secure, file, photo) VALUES ('$insert_date','$rating','$artist','$album','$release_date','$label','$country','$genre','$http_1','$url_1','$review','$reviewer','$secure','$file','1')";
} else {
$sql = "INSERT INTO review (insert_date, rating, artist, album, release_date, label, country, genre, http_1, url_1, review, reviewer, secure) VALUES ('$insert_date','$rating','$artist','$album','$release_date','$label','$country','$genre','$http_1','$url_1','$review','$reviewer','$secure')";
}
$result = mysql_query($sql);
?>
Can anyone help me with a PHP script that will allow me to delete and replace an image file and the associated database column entry ('file')?
** the form field 'photo' is a check box with a value of '1'.
$SQL = 'UPDATE review SET file=\'' . $_POST['file'] . '\', photo=\'' . $_POST['photo'] . '\' WHERE review_id=' . $unique_row_identifier;
mysql_query($SQL);
You have to identify a unique row identifier (usually the tables primary key column as a numerical id) and use that to locate your row for updating.
I'm not advocating inserting POST data directly into your database (as per the example), I'm assuming you will do some validation on the data first.
UPDATE SCRIPT
<?php
include("../inc/db.mr.inc.php");
$insert_date = date("Y-m-d H:i:s");
$table = "review";
if ($submit) {
// here if no ID then adding else we're editing
if ($id) {
$sql = "UPDATE $table SET insert_date='$insert_date',rating='$rating',artist='$artist',album='$album',release_date='$release_date',label='$label',country='$country',genre='$genre',http_1='$http_1',url_1='$url_1',review='$review',reviewer='$reviewer',secure='$secure' WHERE id=$id";
} else {
$sql = "INSERT INTO $table (insert_date, rating, artist, album, release_date, label, country, genre, http_1, url_1, review, reviewer, secure) VALUES ('$insert_date','$rating','$artist','$album','$release_date','$label','$country','$genre','$http_1','$url_1','$review','$reviewer','$secure')";
}
.........
But where do I fit this UPLOAD SCRIPT in with the UPDATE SCRIPT?
UPLOAD SCRIPT
// IMAGE UPLOAD CONFIG
$serverpath = "/home/******/public_html/images/review_photos/";// Path to where images should be uploaded to on the server.
$urltoimages = "http://www.my-website.com/images/review_photos/"; // Web address to where the images are accessible from.
$maxsize = "180000"; // Example - 20000 is the same as 20kb
// CONFIG END
if ($photo == "1") {
$file = $_FILES['file']['name'];
// If you add your own file types don't forget to add an uppercase version.
$allowedfiles[] = "gif";
$allowedfiles[] = "jpg";
$allowedfiles[] = "jpeg";
$allowedfiles[] = "png";
$allowedfiles[] = "GIF";
$allowedfiles[] = "JPG";
$allowedfiles[] = "JPEG";
$allowedfiles[] = "PNG";
if($_FILES['file']['size'] > $maxsize)
{
print "File size is too big - please reduce file size and try again.";
}
else {
$path = "$serverpath/$file";
foreach($allowedfiles as $allowedfile) {
if ($done <> "yes") {
if (file_exists($path)) {
echo "A file with this name already exists - please rename the file and reupload.";
exit;
}
}
if (substr($file, -3) == $allowedfile) {
move_uploaded_file($_FILES['file']['tmp_name'], "$path");
$done = "yes";
echo "<table><tr><td class=p>Your image has been successfully uploaded to our server and can be accessed using the URL provided below.</td></tr>";
echo "<tr><td></td></tr>";
echo "<tr><td><a href='$urltoimages/$file' target='_blank'><strong>$file</strong></a></td></tr>";
echo "<tr><td></td></tr>";
echo "<tr><td><img src=$urltoimages/$file border=0 width=250></td></tr></table>";
}
}
if ($done <> "yes") { print "<p><b>Error:</b> Your image as not been uploaded becuase it is not a recognised image file. Please try again.</p>"; }
}
}