Image chosen does not get replaced

I have a form where there are various fields one can edit and replace images as well. The problem is when one tries to replace the image they choose. It only replaces the path of the file on column "imageurl1" on the MySQL database and not on the one that was chosen. I'm also getting a "Invalid argument supplied for foreach()"

Here is the script

<?php
session_start();
include('SimpleImage.php');
$image = new SimpleImage();    
//error_reporting(E_ALL); 


	// image upload folder
    $image_folder = 'images/classified/'; 
	// fieldnames in form
	$all_file_fields = array('image1', 'image2' ,'image3', 'image4', 'image5', 'image6', 'image7', 'image8', 'image9', 'image10', 'image11', 'image12');
	// allowed filetypes
	$file_types = array('jpg','gif','png');
	// max filesize 5mb
	$max_size = 5000000;
	//echo'<pre>';print_r($_FILES);exit;
	
	$time = time();
	$count = 1;
	
	foreach($all_file_fields as $fieldname){ 
		if($_FILES[$fieldname]['name'] != ''){
			
			$type = substr($_FILES[$fieldname]['name'], -3, 3);
						
			// check filetype
			if(in_array(strtolower($type), $file_types)){
				
				//check filesize
				if($_FILES[$fieldname]['size']>$max_size){
					$error = "File too big. Max filesize is ".$max_size." MB";
				
				}else{
				
					// new filename	
					$filename = str_replace(' ','',$myusername).'_'.$time.'_'.$count.'.'.$type;
			
					// move/upload file
					$image->load($_FILES[$fieldname]['tmp_name']);
					if($image->getWidth() > 150) { //if the image is larger that 150.
						$image->resizeToWidth(500); //resize to 500.
					}
					$target_path = $image_folder.basename($filename); //image path.
					
					$image->save($target_path); //save image to a directory.					
				
					//save array with filenames
					$images[$count] = $image_folder.$filename;
					$count = $count+1;

				}//end if

			}else{ $error = "Please use jpg, gif, png files";
			
			}//end if
		}//end if
	}//end foreach



if($error != ''){ echo $error;	
}else{
//error_reporting(E_ALL);
//ini_set('display_errors','On');

$id = $_POST['id'];
$id = substr($id, 0,5);
if($id < 1 || $id > 99999) exit;

  $servername = "localhost";
  $username	= "";
  $password	= "";
	
	if(!$_POST["title"] || !$_POST["rent"] || !$_POST["fees"]){
		header('location: fields.php');
		
}else if (!(preg_match('#^\d+(\.(\d{2}))?$#',($_POST["rent"])))){
		header('location: rent.php');
	
	}else{	
	$conn =  mysql_connect($servername,$username,$password)or die(mysql_error());
	mysql_select_db("apts",$conn);

// validate id belongs to user
	$sql_check = "SELECT * FROM apts WHERE id = '".$id."' AND username = '".$myusername."'";
	$res = mysql_query($sql_check,$conn) or die(mysql_error());
	$count = mysql_num_rows($res);

if ($count > 0){

$sql_images = "";
foreach($images as $number => $imagedetail){
if($imagedetail != ''){
$sql_images .= "imageurl".$number." = '".mysql_real_escape_string($imagedetail)."',";
}
}
$sql = "UPDATE apartments SET title = '".mysql_real_escape_string($_POST['title'])."', description = '".mysql_real_escape_string($_POST['description'])."', cross_streets = 

'".mysql_real_escape_string($_POST['cross_streets'])."', county = '".mysql_real_escape_string($_POST['county'])."', town = '".mysql_real_escape_string($_POST['town'])."', service = 

'".mysql_real_escape_string($_POST['service'])."', phone = '".mysql_real_escape_string($_POST['phone'])."', contact = '".mysql_real_escape_string($_POST['contact'])."', office = 

'".mysql_real_escape_string($_POST['office'])."', pets = '".mysql_real_escape_string($_POST['pets'])."', email = '".mysql_real_escape_string($_POST['email'])."', rooms = 

'".mysql_real_escape_string($_POST['rooms'])."', bath = '".mysql_real_escape_string($_POST['bath'])."', square = '".mysql_real_escape_string($_POST['square'])."', rent = 

'".mysql_real_escape_string($_POST['rent'])."', fees = '".mysql_real_escape_string($_POST['fees'])."', service = '".mysql_real_escape_string($_POST['service'])."', feeornofee = 

'".mysql_real_escape_string($_POST['feeornofee'])."', lease = '".mysql_real_escape_string($_POST['lease'])."', video = '".mysql_real_escape_string($_POST['video'])."', zipcode = 

'".mysql_real_escape_string($_POST['zipcode'])."',".$sql_images." videotitle = '".mysql_real_escape_string($_POST['videotitle'])."' WHERE id = '".$id."'";


//replace info with the table name above
$result = mysql_query($sql,$conn) or die(mysql_error());
header('location: apartments.php');


}else{
header('location: somethingwrong.php');
}
}	
}
 
?>

Open in new window

genesisvhAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
"Invalid argument supplied for foreach()" usually means that the first argument passed to foreach() was not an array or an object. You can use var_dump() to print out the contents of the argument.  The error message would have a line number.  You can look in the source code at this line number and determine the name of the variable you want to print.  Put the var_dump() statement immediately before the foreach() statement.
0
genesisvhAuthor Commented:
This what shows:
Wrong parameter count for var_dump()
0
Ray PaseurCommented:
Please post the script as you have it now, along with the complete text of the error messages, including the line numbers, thanks.
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

genesisvhAuthor Commented:
Errors:

Warning: Wrong parameter count for var_dump() in /home/genesis/public_html/test/user/aptmodifyform.php on line 92

Warning: Invalid argument supplied for foreach() in /home/genesis/public_html/test/user/aptmodifyform.php on line 93

Warning: Cannot modify header information - headers already sent by (output started at /home/genesis/public_html/test/user/aptmodifyform.php:92) in /home/genesis/public_html/test/user/aptmodifyform.php on line 103

<?php
session_start();
include('SimpleImage.php');
$image = new SimpleImage();    
//error_reporting(E_ALL); 


	// image upload folder
    $image_folder = 'images/classified/'; 
	// fieldnames in form
	$all_file_fields = array('image1', 'image2' ,'image3', 'image4', 'image5', 'image6', 'image7', 'image8', 'image9', 'image10', 'image11', 'image12');
	// allowed filetypes
	$file_types = array('jpg','gif','png');
	// max filesize 5mb
	$max_size = 5000000;
	//echo'<pre>';print_r($_FILES);exit;
	
	$time = time();
	$count = 1;
	
	foreach($all_file_fields as $fieldname){ 
		if($_FILES[$fieldname]['name'] != ''){
			
			$type = substr($_FILES[$fieldname]['name'], -3, 3);
						
			// check filetype
			if(in_array(strtolower($type), $file_types)){
				
				//check filesize
				if($_FILES[$fieldname]['size']>$max_size){
					$error = "File too big. Max filesize is ".$max_size." MB";
				
				}else{
				
					// new filename	
					$filename = str_replace(' ','',$myusername).'_'.$time.'_'.$count.'.'.$type;
			
					// move/upload file
					$image->load($_FILES[$fieldname]['tmp_name']);
					if($image->getWidth() > 150) { //if the image is larger that 150.
						$image->resizeToWidth(500); //resize to 500.
					}
					$target_path = $image_folder.basename($filename); //image path.
					
					$image->save($target_path); //save image to a directory.					
				
					//save array with filenames
					$images[$count] = $image_folder.$filename;
					$count = $count+1;

				}//end if

			}else{ $error = "Please use jpg, gif, png files";
			
			}//end if
		}//end if
	}//end foreach



if($error != ''){ echo $error;	
}else{
//error_reporting(E_ALL);
//ini_set('display_errors','On');

$id = $_POST['id'];
$id = substr($id, 0,5);
if($id < 1 || $id > 99999) exit;

  $servername = "localhost";
  $username	= "";
  $password	= "";
	
	if(!$_POST["title"] || !$_POST["rent"] || !$_POST["fees"]){
		header('location: fields.php');
		
}else if (!(preg_match('#^\d+(\.(\d{2}))?$#',($_POST["rent"])))){
		header('location: rent.php');
	
	}else{	
	$conn =  mysql_connect($servername,$username,$password)or die(mysql_error());
	mysql_select_db("apts",$conn);

// validate id belongs to user
	$sql_check = "SELECT * FROM apts WHERE id = '".$id."' AND username = '".$myusername."'";
	$res = mysql_query($sql_check,$conn) or die(mysql_error());
	$count = mysql_num_rows($res);

if ($count > 0){

$sql_images = "";
foreach($images as $number => $imagedetail){
if($imagedetail != ''){
$sql_images .= "imageurl".$number." = '".mysql_real_escape_string($imagedetail)."',";
}
}
$sql = "UPDATE apartments SET title = '".mysql_real_escape_string($_POST['title'])."', description = '".mysql_real_escape_string($_POST['description'])."', cross_streets = 

'".mysql_real_escape_string($_POST['cross_streets'])."', county = '".mysql_real_escape_string($_POST['county'])."', town = '".mysql_real_escape_string($_POST['town'])."', service = 

'".mysql_real_escape_string($_POST['service'])."', phone = '".mysql_real_escape_string($_POST['phone'])."', contact = '".mysql_real_escape_string($_POST['contact'])."', office = 

'".mysql_real_escape_string($_POST['office'])."', pets = '".mysql_real_escape_string($_POST['pets'])."', email = '".mysql_real_escape_string($_POST['email'])."', rooms = 

'".mysql_real_escape_string($_POST['rooms'])."', bath = '".mysql_real_escape_string($_POST['bath'])."', square = '".mysql_real_escape_string($_POST['square'])."', rent = 

'".mysql_real_escape_string($_POST['rent'])."', fees = '".mysql_real_escape_string($_POST['fees'])."', service = '".mysql_real_escape_string($_POST['service'])."', feeornofee = 

'".mysql_real_escape_string($_POST['feeornofee'])."', lease = '".mysql_real_escape_string($_POST['lease'])."', video = '".mysql_real_escape_string($_POST['video'])."', zipcode = 

'".mysql_real_escape_string($_POST['zipcode'])."',".$sql_images." videotitle = '".mysql_real_escape_string($_POST['videotitle'])."' WHERE id = '".$id."'";


//replace info with the table name above
$result = mysql_query($sql,$conn) or die(mysql_error());
header('location: apartments.php');


}else{
header('location: somethingwrong.php');
}
}	
}
 
?>

Open in new window

0
Ray PaseurCommented:
Here is the message:

Warning: Wrong parameter count for var_dump() in /home/genesis/public_html/test/user/aptmodifyform.php on line 92

Here is line 92:

foreach($images as $number => $imagedetail){

The var_dump() function does not appear on line 92.  Please post the correct and complete script and the correct and complete message.  We need that to be able to help you.  Thanks, ~Ray
0
genesisvhAuthor Commented:
Sorry about that

Warning: Wrong parameter count for var_dump() in /home/genesis/public_html/test/user/aptmodifyform.php on line 92

Warning: Invalid argument supplied for foreach() in /home/genesis/public_html/test/user/aptmodifyform.php on line 93

Warning: Cannot modify header information - headers already sent by (output started at /home/genesis/public_html/test/user/aptmodifyform.php:92) in /home/genesis/public_html/test/user/aptmodifyform.php on line 103

Open in new window

0
genesisvhAuthor Commented:
<?php
session_start();
include('SimpleImage.php');
$image = new SimpleImage();    
//error_reporting(E_ALL); 


	// image upload folder
    $image_folder = 'images/classified/'; 
	// fieldnames in form
	$all_file_fields = array('image1', 'image2' ,'image3', 'image4', 'image5', 'image6', 'image7', 'image8', 'image9', 'image10', 'image11', 'image12');
	// allowed filetypes
	$file_types = array('jpg','gif','png');
	// max filesize 5mb
	$max_size = 5000000;
	//echo'<pre>';print_r($_FILES);exit;
	
	$time = time();
	$count = 1;
	
	foreach($all_file_fields as $fieldname){ 
		if($_FILES[$fieldname]['name'] != ''){
			
			$type = substr($_FILES[$fieldname]['name'], -3, 3);
						
			// check filetype
			if(in_array(strtolower($type), $file_types)){
				
				//check filesize
				if($_FILES[$fieldname]['size']>$max_size){
					$error = "File too big. Max filesize is ".$max_size." MB";
				
				}else{
				
					// new filename	
					$filename = str_replace(' ','',$myusername).'_'.$time.'_'.$count.'.'.$type;
			
					// move/upload file
					$image->load($_FILES[$fieldname]['tmp_name']);
					if($image->getWidth() > 150) { //if the image is larger that 150.
						$image->resizeToWidth(500); //resize to 500.
					}
					$target_path = $image_folder.basename($filename); //image path.
					
					$image->save($target_path); //save image to a directory.					
				
					//save array with filenames
					$images[$count] = $image_folder.$filename;
					$count = $count+1;

				}//end if

			}else{ $error = "Please use jpg, gif, png files";
			
			}//end if
		}//end if
	}//end foreach



if($error != ''){ echo $error;	
}else{
//error_reporting(E_ALL);
//ini_set('display_errors','On');

$id = $_POST['id'];
$id = substr($id, 0,5);
if($id < 1 || $id > 99999) exit;

  $servername = "localhost";
  $username	= "";
  $password	= "";
	
	if(!$_POST["title"] || !$_POST["rent"] || !$_POST["fees"]){
		header('location: fields.php');
		
}else if (!(preg_match('#^\d+(\.(\d{2}))?$#',($_POST["rent"])))){
		header('location: rent.php');
	
	}else{	
	$conn =  mysql_connect($servername,$username,$password)or die(mysql_error());
	mysql_select_db("genesis_apts",$conn);

// validate id belongs to user
	$sql_check = "SELECT * FROM apts WHERE id = '".$id."' AND username = '".$myusername."'";
	$res = mysql_query($sql_check,$conn) or die(mysql_error());
	$count = mysql_num_rows($res);

if ($count > 0){

$sql_images = "";
var_dump();
foreach($images as $number => $imagedetail){
if($imagedetail != ''){
$sql_images .= "imageurl".$number." = '".mysql_real_escape_string($imagedetail)."',";
}
}
$sql = "UPDATE apartments SET title = '".mysql_real_escape_string($_POST['title'])."', description = '".mysql_real_escape_string($_POST['description'])."', cross_streets = '".mysql_real_escape_string($_POST['cross_streets'])."', county = '".mysql_real_escape_string($_POST['county'])."', town = '".mysql_real_escape_string($_POST['town'])."', service = '".mysql_real_escape_string($_POST['service'])."', phone = '".mysql_real_escape_string($_POST['phone'])."', contact = '".mysql_real_escape_string($_POST['contact'])."', office = '".mysql_real_escape_string($_POST['office'])."', pets = '".mysql_real_escape_string($_POST['pets'])."', email = '".mysql_real_escape_string($_POST['email'])."', rooms = '".mysql_real_escape_string($_POST['rooms'])."', bath = '".mysql_real_escape_string($_POST['bath'])."', square = '".mysql_real_escape_string($_POST['square'])."', rent = '".mysql_real_escape_string($_POST['rent'])."', fees = '".mysql_real_escape_string($_POST['fees'])."', service = '".mysql_real_escape_string($_POST['service'])."', feeornofee = '".mysql_real_escape_string($_POST['feeornofee'])."', lease = '".mysql_real_escape_string($_POST['lease'])."', video = '".mysql_real_escape_string($_POST['video'])."', zipcode = '".mysql_real_escape_string($_POST['zipcode'])."',".$sql_images." videotitle = '".mysql_real_escape_string($_POST['videotitle'])."' WHERE id = '".$id."'";


//replace info with the table name above
$result = mysql_query($sql,$conn) or die(mysql_error());
header('location: apartments.php');


}else{
header('location: somethingwrong.php');
}
}	
}
 
?>

Open in new window

0
Ray PaseurCommented:
The PHP function var_dump() needs to be told what variable it is to print.  You tell it the variable name by putting the variable name inside the parentheses.  Example:

$x = 2;
var_dump($x);

This will tell us that $x is an integer with a value of 2.

All of the PHP functions are described in detail with user-contributed notes on the PHP web site.  If you do not know -- 100% sure -- what a function needs for input or produces for output, please always look it up on php.net.  Here is what var_dump() has to say to us.  
http://php.net/manual/en/function.var-dump.php

You can put the function name into the search box on the upper right of the php.net web page and search for the function documentation.

Best regards, ~Ray
0
genesisvhAuthor Commented:
Ok so I added this:

var_dump($sql_images);

and this in return:

string(0) "" 
Warning: Invalid argument supplied for foreach() in /home/genesis/public_html/test/user/aptmodifyform.php on line 93

Warning: Cannot modify header information - headers already sent by (output started at /home/genesis/public_html/test/user/aptmodifyform.php:92) in /home/genesis/public_html/test/user/aptmodifyform.php on line 103
0
Ray PaseurCommented:
Have you considered hiring a professional programmer to help with this?  If you believe as I do that time is money, you may want to exchange a little money for the huge savings of time that might be yours if you have a professional look at the details of this question.  In PHP and in most programming languages the instructions are executed in order from top to bottom, so if you have an instruction that says something like this...

$x = 2;

... the value of $x is not changed to 2 until after that instruction is executed.  It follows that this sequence may produce "interesting" output...

var_dump($x); // UNPREDICTABLE VALUE OF $x
$x = 2;   // SET $x to SOMETHING PREDICTABLE
var_dump($x); // NOW $x IS TWO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.