Solved

Image chosen does not get replaced

Posted on 2012-03-24
10
257 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
ID: 37761206
"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
ID: 37761217
This what shows:
Wrong parameter count for var_dump()
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37761405
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
ID: 37761593
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
ID: 37761668
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:genesisvh
ID: 37761677
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
ID: 37761678
<?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
ID: 37761727
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
ID: 37761740
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
ID: 37761826
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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…

943 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

7 Experts available now in Live!

Get 1:1 Help Now