Solved

Image chosen does not get replaced

Posted on 2012-03-24
10
254 Views
Last Modified: 2012-04-05
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

0
Comment
Question by:genesisvh
  • 5
  • 5
10 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
"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
 

Author Comment

by:genesisvh
Comment Utility
This what shows:
Wrong parameter count for var_dump()
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Please post the script as you have it now, along with the complete text of the error messages, including the line numbers, thanks.
0
 

Author Comment

by:genesisvh
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:genesisvh
Comment Utility
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
 

Author Comment

by:genesisvh
Comment Utility
<?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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:genesisvh
Comment Utility
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now