Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Image chosen does not get replaced

Posted on 2012-03-24
10
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 111

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 111

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 111

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
 

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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 count occurrences of each item in an array.

705 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