Link to home
Start Free TrialLog in
Avatar of genesisvh
genesisvh

asked on

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

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

"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.
Avatar of genesisvh
genesisvh

ASKER

This what shows:
Wrong parameter count for var_dump()
Please post the script as you have it now, along with the complete text of the error messages, including the line numbers, thanks.
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

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
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

<?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

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
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
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial