Jump to content

Recommended Posts

Man that title is crap I know but simply don't know what you use for a good title on this one.

 

When I use the following script to add an entry to the database it works perfectly. But I seem to be doing something wrong with my UPDATE script.

 

The PROBLEM:

 

This script is supposed to update the database with new images if they are added or otherwise leave the other images as is but...

If an image is not chosen to replace the existing image then the existing image is removed (the link anyway). So if all you edit is the text area then all links to the images get removed.

 

I am guessing that it has something to do with me not echoing a value to the input type=file area? Which I would then check to see if the input was empty or not and take action based on that....but, but... then the buts start and I get confused.

 

I guess I want the script to ignore previous images that are fine, update images that have been chosen to update and now that I think of it there should be a way to delete an image (maybe a tick box).

 

Thanks for any help you can offer.

 

Morgs

 

 

<?php 
require_once("../includes/connection.php");
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);		

include 'includes/header.php';

?>

	<div id="content_wrap">
		<h2>Edit Post</h2>

	<?php

	if (isset ($_POST['submit'])) { // Handle this form

		$post_image= array_fill(0, 4, '');
		$thumb= array_fill(0, 4, '');
		$count= 0;
			foreach ($_FILES["new_image"]["error"] as $key => $error) {
					    if ($error == UPLOAD_ERR_OK) {
				if (isset ($_FILES['new_image'])){
						$imagename = $_FILES['new_image']['name'][$key];
						$randomdigit = rand(0000,9999);//create random digit
						$imagename = $randomdigit.$imagename;//make new file name with random digit
						$source = $_FILES['new_image']['tmp_name'][$key];
						$target = "../images/news/".$imagename;

						move_uploaded_file($source, $target);

						$imagepath = $imagename;
						$save = "../images/news/" . $imagepath; //This is the new file you saving
						$file = "../images/news/" . $imagepath; //This is the original file

						list($width, $height) = getimagesize($file) ; 
						if ($width <= $height) {

							$modheight = 500; 

				            $diff = $height / $modheight;

				            $modwidth = $width / $diff;
				            
						} else {

							$modwidth = 500; 

							$diff = $width / $modwidth;

							$modheight = $height / $diff;
						}

					//	$modwidth = 500; 

					//	$diff = $width / $modwidth;

					//	$modheight = $height / $diff;

						$tn = imagecreatetruecolor($modwidth, $modheight) ; 
						$image = imagecreatefromjpeg($file) ; 
						imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

						imagejpeg($tn, $save, 100) ; 

						$save = "../images/news/sml_" . $imagepath; //This is the new file you saving
						$file = "../images/news/" . $imagepath; //This is the original file

						list($width, $height) = getimagesize($file) ; 

						if ($width <= $height) {

							$modheight = 100; 

				            $diff = $height / $modheight;

				            $modwidth = $width / $diff;
				            
						} else {

							$modwidth = 100; 

							$diff = $width / $modwidth;

							$modheight = $height / $diff;
						}
						$tn = imagecreatetruecolor($modwidth, $modheight) ; 
						$image = imagecreatefromjpeg($file) ; 
						imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

						imagejpeg($tn, $save, 100) ; 
						// echo "Large image: <img src='../images/news/".$imagepath."'><br>"; 
						// echo "Thumbnail: <img src='../images/news/sml_".$imagepath."'>"; 

						$count = ++$count;
						$post_image[$count] = "images/news/".$imagepath."";
						$thumb[$count] = "images/news/sml_".$imagepath."";

						}
						}              
				}            

			$title = $_POST['title'];
			$comment = $_POST['comment'];
			$comment = stripslashes ($comment);


		// Define the query.
		$update_post = $sql = "UPDATE `artspoke_artspokbachhair`.`blog` SET 
		`blog_title` = '".mysql_real_escape_string(($title))."', 
		`blog_content` = '".mysql_real_escape_string(($comment))."', 
		`post_image1` = '".mysql_real_escape_string(($post_image[1]))."',
		`thumb1` = '".mysql_real_escape_string(($thumb[1]))."',
		`post_image2` = '".mysql_real_escape_string(($post_image[2]))."',
		`thumb2` = '".mysql_real_escape_string(($thumb[2]))."',
		`post_image3` = '".mysql_real_escape_string(($post_image[3]))."',
		`thumb3` = '".mysql_real_escape_string(($thumb[3]))."',
		`post_image4` = '".mysql_real_escape_string(($post_image[4]))."',
		`thumb4` = '".mysql_real_escape_string(($thumb[4]))."'
		WHERE  `artspoke_artspokbachhair`.`blog`.`blog_id` = '{$_POST['blog_id']}' LIMIT 1 ";			


			// execute the query.
			if (@mysql_query ($update_post)) {
				print "<p><b>The post ".  $title . 
			" has been updated.</b></p>";
			} else {
				print "<p>Could not update the post because: <strong>" . mysql_error() . "</strong>. The query was $update_post.</p>";
			} 

	} else {

	// Check for a valid page ID in the URL
	if (is_numeric ($_GET['blog_id'])) {

	// Define the query.
	$query = "SELECT * FROM blog WHERE blog_id='{$_GET['blog_id']}'";

	if ($r = mysql_query($query)) {  // run the query.

	$row = mysql_fetch_array($r, MYSQL_ASSOC);

	include 'includes/instructions.php';

	// Display the form.
		if ( empty($row['thumb1']) ) { $row['thumb1'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['thumb2']) ) { $row['thumb2'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['thumb3']) ) { $row['thumb3'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['thumb4']) ) { $row['thumb4'] = 'admin/images/no_image.jpg'; }
	echo '<div id="feedbackform">'
		. '<form action="editpost.php" method="post" accept-charset="utf-8" enctype="multipart/form-data">'
		. '<p><h4>News Title</h4><input type="text" size="40" name="title" value="' . $row['blog_title'] . '"/><label for="title"></label></p>'	
		. '<p><h4>News Content</h4><textarea name="comment" rows="15" cols="50">' . $row['blog_content'] . '</textarea></p>';

		echo '<p><h4>Change Image 1</h4><input type="file" name="new_image[]" value="' . $row['post_image1'] . '" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb1'] . '" /></p>
			<p><h4>Change Image 2</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb2'] . '" /></p>
			<p><h4>Change Image 3</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb3'] . '" /></p>
			<p><h4>Change Image 4</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb4'] . '" /></p>';
		echo '<input type="hidden" name="blog_id" value="' . $row['blog_id'] . '">';				
		echo '<br /><br />'
			. '<p><input type="submit" name="submit" value="Update"></p>'
			. '</form>'
			. '</div> <!-- end feedbackform -->';
			}

	} else { // No Page ID set.
	print '<p><b>You must have made a mistake in using this page.</b></p>';
	}

	}


	?>






	</div>
	<div class="clear"></div>
</div>
<script type="text/javascript">
// to set WMD's options programatically, define a "wmd_options" object with whatever settings
// you want to override.  Here are the defaults:
wmd_options = {
// format sent to the server.  Use "Markdown" to return the markdown source.
output: "HTML",
// line wrapping length for lists, blockquotes, etc.
lineLength: 40,
// toolbar buttons.  Undo and redo get appended automatically.
buttons: "bold italic | link blockquote | ol ul",
// option to automatically add WMD to the first textarea found.  See apiExample.html for usage.
autostart: true
};
</script>
<script type="text/javascript" src="wmd/wmd.js"></script>
</body>
</html>

No, you simply need to check that a new image was selected to overwrite the original.

 

// a new image has been entered
if(strlen($_FILES['new_image']['name'])) {
// delete the original and replace with this image
}
else {
// keep the existing image - do not overwrite the existing database record
}

No, you simply need to check that a new image was selected to overwrite the original.

 

// a new image has been entered
if(strlen($_FILES['new_image']['name'])) {
// delete the original and replace with this image
}
else {
// keep the existing image - do not overwrite the existing database record
}

 

Ok, that is making sense. I am guessing now that I need to feed that idea into the loop so that it either returns a new result (new image to update with) or no(?) result? I question the no result because I have a feeling that that is what is happening now which means the blank result is overwriting the already saved info.

 

Could add a delete feature like I mentioned by using a check box in some way and then that way causing a blank to overwrite the saved data?

 

Thanks heaps for your help so far.

This is the path I am trying to take. Just want to see if I am on track or going totally the wrong way. Please have a look. Thanks.

 

Look for these comments:

// delete the original and replace with this image

// keep the existing image - do not overwrite the existing database record

 

<?php 
require_once("../includes/connection.php");
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);		

include 'includes/header.php';

?>

	<div id="content_wrap">
		<h2>Edit Post</h2>

	<?php

	if (isset ($_POST['submit'])) { // Handle this form		
		$post_image= array_fill(0, 4, '');
		$thumb= array_fill(0, 4, '');
		$count= 0;
			foreach ($_FILES["new_image"]["error"] as $key => $error) {
				if ($error == UPLOAD_ERR_OK) {						
					if(strlen($_FILES['new_image']['name'])) { // delete the original and replace with this image
						if (isset ($_FILES['new_image'])){
							$imagename = $_FILES['new_image']['name'][$key];
							$randomdigit = rand(0000,9999);//create random digit
							$imagename = $randomdigit.$imagename;//make new file name with random digit
							$source = $_FILES['new_image']['tmp_name'][$key];
							$target = "../images/news/".$imagename;

							move_uploaded_file($source, $target);

							$imagepath = $imagename;
							$save = "../images/news/" . $imagepath; //This is the new file you saving
							$file = "../images/news/" . $imagepath; //This is the original file

							list($width, $height) = getimagesize($file) ; 
							if ($width <= $height) {

								$modheight = 500; 

								$diff = $height / $modheight;

								$modwidth = $width / $diff;

							} else {

								$modwidth = 500; 

								$diff = $width / $modwidth;

								$modheight = $height / $diff;
							}

						//	$modwidth = 500; 

						//	$diff = $width / $modwidth;

						//	$modheight = $height / $diff;

							$tn = imagecreatetruecolor($modwidth, $modheight) ; 
							$image = imagecreatefromjpeg($file) ; 
							imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

							imagejpeg($tn, $save, 100) ; 

							$save = "../images/news/sml_" . $imagepath; //This is the new file you saving
							$file = "../images/news/" . $imagepath; //This is the original file

							list($width, $height) = getimagesize($file) ; 

							if ($width <= $height) {

								$modheight = 100; 

								$diff = $height / $modheight;

								$modwidth = $width / $diff;

							} else {

								$modwidth = 100; 

								$diff = $width / $modwidth;

								$modheight = $height / $diff;
							}
							$tn = imagecreatetruecolor($modwidth, $modheight) ; 
							$image = imagecreatefromjpeg($file) ; 
							imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

							imagejpeg($tn, $save, 100) ; 
							// echo "Large image: <img src='../images/news/".$imagepath."'><br>"; 
							// echo "Thumbnail: <img src='../images/news/sml_".$imagepath."'>"; 

							$count = ++$count;
							$post_image[$count] = "images/news/".$imagepath."";
							$thumb[$count] = "images/news/sml_".$imagepath."";

							}
						} else { // keep the existing image - do not overwrite the existing database record
							$query = "SELECT * FROM blog WHERE blog_id='{$_POST['blog_id']}'";

							if ($r = mysql_query($query)) {  // run the query.

							$row = mysql_fetch_array($r, MYSQL_ASSOC);
							$count = ++$count;
							$post_image[$count] = $row['postimage' . $count . ''];
							}
						}
					}											  
				}            

			$title = $_POST['title'];
			$comment = $_POST['comment'];
			$comment = stripslashes ($comment);


		// Define the query.
		$add_new_image = $sql = "UPDATE `artspoke_artspokbachhair`.`blog` SET 
		`blog_title` = '".mysql_real_escape_string(($title))."', 
		`blog_content` = '".mysql_real_escape_string(($comment))."', 
		`post_image1` = '".mysql_real_escape_string(($post_image[1]))."',
		`thumb1` = '".mysql_real_escape_string(($thumb[1]))."',
		`post_image2` = '".mysql_real_escape_string(($post_image[2]))."',
		`thumb2` = '".mysql_real_escape_string(($thumb[2]))."',
		`post_image3` = '".mysql_real_escape_string(($post_image[3]))."',
		`thumb3` = '".mysql_real_escape_string(($thumb[3]))."',
		`post_image4` = '".mysql_real_escape_string(($post_image[4]))."',
		`thumb4` = '".mysql_real_escape_string(($thumb[4]))."'
		WHERE  `artspoke_artspokbachhair`.`blog`.`blog_id` = '{$_POST['blog_id']}' LIMIT 1 ";			


			// execute the query.
			if (@mysql_query ($add_new_image)) {
				print "<p><b>The post ".  $title . 
			" has been updated.</b></p>";
			} else {
				print "<p>Could not update the post because: <strong>" . mysql_error() . "</strong>. The query was $query.</p>";
			} 

	} else {

	// Check for a valid page ID in the URL
	if (is_numeric ($_GET['blog_id'])) {

	// Define the query.
	$query = "SELECT * FROM blog WHERE blog_id='{$_GET['blog_id']}'";

	if ($r = mysql_query($query)) {  // run the query.

	$row = mysql_fetch_array($r, MYSQL_ASSOC);

	include 'includes/instructions.php';

	// Display the form.
		if ( empty($row['thumb1']) ) { $row['thumb1'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['thumb2']) ) { $row['thumb2'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['thumb3']) ) { $row['thumb3'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['thumb4']) ) { $row['thumb4'] = 'admin/images/no_image.jpg'; }
	echo '<div id="feedbackform">'
		. '<form action="editpost.php" method="post" accept-charset="utf-8" enctype="multipart/form-data">'
		. '<p><h4>News Title</h4><input type="text" size="40" name="title" value="' . $row['blog_title'] . '"/><label for="title"></label></p>'	
		. '<p><h4>News Content</h4><textarea name="comment" rows="15" cols="50">' . stripslashes(($row['blog_content'])) . '</textarea></p>';

		echo '<p><h4>Change Image 1</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb1'] . '" /></p>
			<p><h4>Change Image 2</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb2'] . '" /></p>
			<p><h4>Change Image 3</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb3'] . '" /></p>
			<p><h4>Change Image 4</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb4'] . '" /></p>';
		echo '<input type="hidden" name="blog_id" value="' . $row['blog_id'] . '">';				
		echo '<br /><br />'
			. '<p><input type="submit" name="submit" value="Update"></p>'
			. '</form>'
			. '</div> <!-- end feedbackform -->';
			}

	} else { // No Page ID set.
	print '<p><b>You must have made a mistake in using this page.</b></p>';
	}
}
	?>

	</div>
	<div class="clear"></div>
</div>
<script type="text/javascript">
// to set WMD's options programatically, define a "wmd_options" object with whatever settings
wmd_options = {
// toolbar buttons.  Undo and redo get appended automatically.
buttons: "bold italic | link blockquote | ol ul",
};
</script>
<script type="text/javascript" src="wmd/wmd.js"></script>
</body>
</html>

Ok, it was suggested in IRC by sirPengi that I work with the UPLOAD_ERR_OK details here:

http://us3.php.net/manual/en/features.file-upload.errors.php

 

Again I need help to see if I am approaching it the right way. I think I am getting closer.

 

Now if no image is uploaded I can get it to send "do not touch' to the database but only on the first loop. So it seems that if the first loop fails then it stops there. In some ways I feel like I am getting closer but it is doing strange things now. Maybe the way I am handling the arrays is a bit inconsistent (the results are)?

 

Also I obviously don't want to send "do not touch" to the database. Ideally I just want to send nothing but for that nothing not to effect what is already there. Can I do this?

 

<?php 
require_once("../includes/connection.php");
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);		

include 'includes/header.php';

?>

	<div id="content_wrap">
		<h2>Edit Post</h2>

	<?php

	if (isset ($_POST['submit'])) { // Handle this form		
		$post_image= array_fill(0, 4, '');
		$thumb= array_fill(0, 4, '');
		$count= 0;
			foreach ($_FILES["new_image"]["error"] as $key => $error) {
				if ($error == UPLOAD_ERR_OK) {		// delete the original and replace with this image						
						if (isset ($_FILES['new_image'])){
							$imagename = $_FILES['new_image']['name'][$key];
							$randomdigit = rand(0000,9999);//create random digit
							$imagename = $randomdigit.$imagename;//make new file name with random digit
							$source = $_FILES['new_image']['tmp_name'][$key];
							$target = "../images/news/".$imagename;

							move_uploaded_file($source, $target);

							$imagepath = $imagename;
							$save = "../images/news/" . $imagepath; //This is the new file you saving
							$file = "../images/news/" . $imagepath; //This is the original file

							list($width, $height) = getimagesize($file) ; 
							if ($width <= $height) {

								$modheight = 500; 

								$diff = $height / $modheight;

								$modwidth = $width / $diff;

							} else {

								$modwidth = 500; 

								$diff = $width / $modwidth;

								$modheight = $height / $diff;
							}

						//	$modwidth = 500; 

						//	$diff = $width / $modwidth;

						//	$modheight = $height / $diff;

							$tn = imagecreatetruecolor($modwidth, $modheight) ; 
							$image = imagecreatefromjpeg($file) ; 
							imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

							imagejpeg($tn, $save, 100) ; 

							$save = "../images/news/sml_" . $imagepath; //This is the new file you saving
							$file = "../images/news/" . $imagepath; //This is the original file

							list($width, $height) = getimagesize($file) ; 

							if ($width <= $height) {

								$modheight = 100; 

								$diff = $height / $modheight;

								$modwidth = $width / $diff;

							} else {

								$modwidth = 100; 

								$diff = $width / $modwidth;

								$modheight = $height / $diff;
							}
							$tn = imagecreatetruecolor($modwidth, $modheight) ; 
							$image = imagecreatefromjpeg($file) ; 
							imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

							imagejpeg($tn, $save, 100) ; 
							// echo "Large image: <img src='../images/news/".$imagepath."'><br>"; 
							// echo "Thumbnail: <img src='../images/news/sml_".$imagepath."'>"; 

							$count = ++$count;
							$post_image[$count] = "images/news/".$imagepath."";
							$thumb[$count] = "images/news/sml_".$imagepath."";

							} 
						} else { // keep the existing image - do not overwrite the existing database record
							$count= 0;
							$count = ++$count;
							$post_image[$count] = 'dont touch it';
							$thumb[$count] = "dont touch the thumb path";
						}										  
				}            

			$title = $_POST['title'];
			$comment = $_POST['comment'];
			$comment = stripslashes ($comment);


		// Define the query.
		$add_new_image = $sql = "UPDATE `artspoke_artspokbachhair`.`blog` SET 
		`blog_title` = '".mysql_real_escape_string(($title))."', 
		`blog_content` = '".mysql_real_escape_string(($comment))."', 
		`post_image1` = '".mysql_real_escape_string(($post_image[1]))."',
		`thumb1` = '".mysql_real_escape_string(($thumb[1]))."',
		`post_image2` = '".mysql_real_escape_string(($post_image[2]))."',
		`thumb2` = '".mysql_real_escape_string(($thumb[2]))."',
		`post_image3` = '".mysql_real_escape_string(($post_image[3]))."',
		`thumb3` = '".mysql_real_escape_string(($thumb[3]))."',
		`post_image4` = '".mysql_real_escape_string(($post_image[4]))."',
		`thumb4` = '".mysql_real_escape_string(($thumb[4]))."'
		WHERE  `artspoke_artspokbachhair`.`blog`.`blog_id` = '{$_POST['blog_id']}' LIMIT 1 ";			


			// execute the query.
			if (@mysql_query ($add_new_image)) {
				print "<p><b>The post ".  $title . 
			" has been updated.</b></p>";
			print_r($_FILES);
			} else {
				print "<p>Could not update the post because: <strong>" . mysql_error() . "</strong>. The query was $query.</p>";
			} 

	} else {

	// Check for a valid page ID in the URL
	if (is_numeric ($_GET['blog_id'])) {

	// Define the query.
	$query = "SELECT * FROM blog WHERE blog_id='{$_GET['blog_id']}'";

	if ($r = mysql_query($query)) {  // run the query.

	$row = mysql_fetch_array($r, MYSQL_ASSOC);

	include 'includes/instructions.php';

	// Display the form.
		if ( empty($row['thumb1']) ) { $row['thumb1'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['thumb2']) ) { $row['thumb2'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['thumb3']) ) { $row['thumb3'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['thumb4']) ) { $row['thumb4'] = 'admin/images/no_image.jpg'; }
	echo '<div id="feedbackform">'
		. '<form action="editpost.php" method="post" accept-charset="utf-8" enctype="multipart/form-data">'
		. '<p><h4>News Title</h4><input type="text" size="40" name="title" value="' . $row['blog_title'] . '"/><label for="title"></label></p>'	
		. '<p><h4>News Content</h4><textarea name="comment" rows="15" cols="50">' . stripslashes(($row['blog_content'])) . '</textarea></p>';

		echo '<p><h4>Change Image 1</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb1'] . '" /></p>
			<p><h4>Change Image 2</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb2'] . '" /></p>
			<p><h4>Change Image 3</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb3'] . '" /></p>
			<p><h4>Change Image 4</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['thumb4'] . '" /></p>';
		echo '<input type="hidden" name="blog_id" value="' . $row['blog_id'] . '">';				
		echo '<br /><br />'
			. '<p><input type="submit" name="submit" value="Update"></p>'
			. '</form>'
			. '</div> <!-- end feedbackform -->';
			}

	} else { // No Page ID set.
	print '<p><b>You must have made a mistake in using this page.</b></p>';
	}
}
	?>

	</div>
	<div class="clear"></div>
</div>
<script type="text/javascript">
// to set WMD's options programatically, define a "wmd_options" object with whatever settings
wmd_options = {
// toolbar buttons.  Undo and redo get appended automatically.
buttons: "bold italic | link blockquote | ol ul",
};
</script>
<script type="text/javascript" src="wmd/wmd.js"></script>
</body>
</html>

Based on a suggestion I have tried to simplify the script as mush as I can. It works to upload images but if I just hit update without choosing replacement images the database is overwritten with blank data in effect removing what was there which was meant to be untouched.

 

And say I have 2 images and I try to update the second - the first image is removed, the second becomes the first and what becomes the final 3 images areas are removed too.

 

Please help. Thanks

 

 

<?php 
require_once("../includes/connection.php");
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);		

include 'includes/header.php';

?>

	<div id="content_wrap">
		<h2>Edit Post</h2>

	<?php

	if (isset ($_POST['submit'])) { // Handle this form		
		$image= array_fill(0, 4, '');	// HAVE I DONE THIS BIT CORRECTLY?????		
		$count= 0;
			foreach ($_FILES["new_image"]["error"] as $key => $error) {
				if ($error == UPLOAD_ERR_OK) {		// delete the original and replace with this image						
						if (isset ($_FILES['new_image'])){
							$imagename = $_FILES['new_image']['name'][$key];
							$randomdigit = rand(0000,9999);//create random digit
							$imagename = $randomdigit.$imagename;//make new file name with random digit
							$source = $_FILES['new_image']['tmp_name'][$key];
							$target = "../images/test/".$imagename;

							move_uploaded_file($source, $target);

							$imagepath = $imagename;
							$save = "../images/test/" . $imagepath; //This is the new file you saving
							$file = "../images/test/" . $imagepath; //This is the original file

							$count = ++$count;
							$image[$count] = "images/test/".$imagepath."";

							} 

						} elseif ($error == UPLOAD_ERR_NO_FILE) { // keep the existing image - do not overwrite the existing database record

							if (!isset ($_FILES['new_image'])){
								$image[$count] = 'dont touch it';

							}
						}										  
					}            

			$title = $_POST['title'];
			$comment = $_POST['comment'];
			$comment = stripslashes ($comment);


		// Define the query.
		$add_new_image = $sql = "UPDATE `artspoke_artspokbachhair`.`test` SET 
		`title` = '".mysql_real_escape_string(($title))."', 
		`comment` = '".mysql_real_escape_string(($comment))."', 
		`image1` = '".mysql_real_escape_string(($image[1]))."',			
		`image2` = '".mysql_real_escape_string(($image[2]))."',			
		`image3` = '".mysql_real_escape_string(($image[3]))."',			
		`image4` = '".mysql_real_escape_string(($image[4]))."'		
		WHERE  `artspoke_artspokbachhair`.`test`.`id` = '{$_POST['id']}' LIMIT 1 ";			


			// execute the query.
			if (@mysql_query ($add_new_image)) {
				print "<p><b>The post ".  $title . 
			" has been updated.</b></p>";
			print_r($_FILES);
			} else {
				print "<p>Could not update the post because: <strong>" . mysql_error() . "</strong>. The query was $query.</p>";
			} 

	} else {

	// Check for a valid page ID in the URL
	if (is_numeric ($_GET['id'])) {

	// Define the query.
	$query = "SELECT * FROM test WHERE id='{$_GET['id']}'";

	if ($r = mysql_query($query)) {  // run the query.

	$row = mysql_fetch_array($r, MYSQL_ASSOC);

	include 'includes/instructions.php';

	// Display the form.
		if ( empty($row['image1']) ) { $row['image1'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['image2']) ) { $row['image2'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['image3']) ) { $row['image3'] = 'admin/images/no_image.jpg'; }
		if ( empty($row['image4']) ) { $row['image4'] = 'admin/images/no_image.jpg'; }
		echo '<div id="feedbackform">'
		. '<form action="test.php" method="post" accept-charset="utf-8" enctype="multipart/form-data">'
		. '<p><h4>News Title</h4><input type="text" size="40" name="title" value="' . $row['title'] . '"/><label for="title"></label></p>'	
		. '<p><h4>News Content</h4><textarea name="comment" rows="15" cols="50">' . $row['comment'] . '</textarea></p>';

		echo '<p><h4>Change Image 1</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['image1'] . '" height="50" width="50" /></p>
			<p><h4>Change Image 2</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['image2'] . '" height="50" width="50" /></p>
			<p><h4>Change Image 3</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['image3'] . '" height="50" width="50" /></p>
			<p><h4>Change Image 4</h4><input type="file" name="new_image[]" class="bginput">
			<br /><img src="http://bachbeta.artspokenhosting.com/' . $row['image4'] . '" height="50" width="50" /></p>';
		echo '<input type="hidden" name="id" value="' . $row['id'] . '">';				
		echo '<br /><br />'
			. '<p><input type="submit" name="submit" value="Update"></p>'
			. '</form>'
			. '</div> <!-- end feedbackform -->';
			}

	} else { // No Page ID set.
	print '<p><b>You must have made a mistake in using this page.</b></p>';
	}
}
	?>

	</div>
	<div class="clear"></div>
</div>
</body>
</html>

I beg of someone to take pity on me. I have been trying to do this for 2 days. I have been trying to impliment everyones suggestions. I try one thing and someone says I should be doing it a different way but then that leads to having to do something else differently, maybe.

 

I hate to ask as it is obviously uncool but I need someone to walk me through this. My books aren't helping, I don't know how ot google this one. No one will talk to me in #php anymore. #phpfreaks rarely has anyone in there. I am putting an honest effort into working this out but I literally need someone to hold my hand.

It's not pretty but I got it working.

 

The ugliest part is that I have just repeated the code below 4 times over and manually changed the 1's to 2,3 & 4's. Ugly as hell I know but now that I have it working I am not gain to risk breaking it.

 

$errorCode = $_FILES['new_image1']['error'];
		if ($errorCode == UPLOAD_ERR_OK) {
		if (!empty($_FILES['new_image1'])) { // delete the original and replace with this image
			$imagename = $_FILES['new_image1']['name'];
			$randomdigit = rand(0000,9999);//create random digit
			$imagename = $randomdigit.$imagename;//make new file name with random digit
			$source = $_FILES['new_image1']['tmp_name'];
			$target = "../images/news/".$imagename;

			move_uploaded_file($source, $target);

			$imagepath = $imagename;
			$save = "../images/news/" . $imagepath; //This is the new file you saving
			$file = "../images/news/" . $imagepath; //This is the original file

			list($width, $height) = getimagesize($file) ; 
			if ($width <= $height) {

				$modheight = 500; 

				$diff = $height / $modheight;

				$modwidth = $width / $diff;

			} else {

				$modwidth = 500; 

				$diff = $width / $modwidth;

				$modheight = $height / $diff;
			}

			$tn = imagecreatetruecolor($modwidth, $modheight) ; 
			$image = imagecreatefromjpeg($file) ; 
			imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

			imagejpeg($tn, $save, 100) ; 

			$save = "../images/news/sml_" . $imagepath; //This is the new file you saving
			$file = "../images/news/" . $imagepath; //This is the original file

			list($width, $height) = getimagesize($file) ; 

			if ($width <= $height) {

				$modheight = 100; 

				$diff = $height / $modheight;

				$modwidth = $width / $diff;

			} else {

				$modwidth = 100; 

				$diff = $width / $modwidth;

				$modheight = $height / $diff;
			}
			$tn = imagecreatetruecolor($modwidth, $modheight) ; 
			$image = imagecreatefromjpeg($file) ; 
			imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

			imagejpeg($tn, $save, 100) ; 

			$post_image1 = "images/news/".$imagepath."";
			$thumb1 = "images/news/sml_".$imagepath."";
				} 
			} elseif ($errorCode == UPLOAD_ERR_NO_FILE) {
					// Define the query.
				$query1 = "SELECT * FROM blog WHERE blog_id='{$_POST['blog_id']}'";

				if ($r1 = mysql_query($query1)) {  // run the query.

				$row1 = mysql_fetch_array($r1, MYSQL_ASSOC);
				$post_image1 = $row1['post_image1'];
				$thumb1 = $row1['thumb1'];
				}
			} // end if error code 

 

Use a loop for the number of items then you dont need any code duplication:

 

$images = array(1,2,3,4);
foreach($images as $imageNum) {
$imagename = $_FILES['new_image'.$imageNum]['name'];
}

 

Finish the rest off.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.