Link to home
Start Free TrialLog in
Avatar of mimoser
mimoserFlag for United States of America

asked on

PHP MySql Picture Upload Script

The coding below is to allow members of my site the oppertunity to upload 12 pictures to thier profile. It did work well, for the first few days, now it's telling every member that they have reached there upload limit, even though they havn't uploaded any at all.

Please help, I can't see why it worked and then stopped, when no coding was changed.
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 
$user_id = $_GET['user_id'];
$res = mysql_query("select count(*) from gallery_photos WHERE user_id = '$user_id'");
if(!$res) die(mysql_error());
$count = mysql_result($res,0);
if($count >= 12) {
  echo "You Have Reached Your Twelve Picture Limit";
  ?>
  To Delete Pics <a href="code.php?user_id=<?php echo $user_id; ?>">Click Here</a> 
  <?php
} else {
  	$photo_upload_fields = "";
	$counter = 1;
	$number_of_fields = 3; 
	if( $_GET['number_of_fields'] )
	$number_of_fields = (int)($_GET['number_of_fields']);
	$result = mysql_query( "SELECT category_id,category_name FROM gallery_category" );
	while( $row = mysql_fetch_array( $result ) )
	{
$photo_category_list .=<<<__HTML_END
	<option value="$row[0]">$row[1]</option>\n
__HTML_END;
	}
	mysql_free_result( $result );	
	while( $counter <= $number_of_fields )
	{
$photo_upload_fields .=<<<__HTML_END
<tr>
	<td>
	     Photo {$counter}:
	    <input name=' photo_filename[]' type='file' />
	</td>
</tr>
<tr>
	<td>
	     Caption:
	    <textarea name='photo_caption[]' cols='30' rows='1'></textarea>
	</td>
</tr>
__HTML_END;
	$counter++;
	}
echo <<<__HTML_END
<html>
<head>
	<title>Lets upload Photos</title>
</head>
<body>
<form enctype='multipart/form-data' action='upload.php?user_id=$user_id' method='post' name='upload_form'>
<table width='90%' border='0' align='center' style='width: 90%;'>
 
<tr>
	<td>
		<p>&nbsp;</p>
	</td>
</tr>
 
$photo_upload_fields
<tr>
	<td>
	        <input type='submit' name='submit' value='Add Photos' />
	</td>
</tr>
</table>
</form>
 
</body>
</html>
__HTML_END;
}

Open in new window

Avatar of christophermccann
christophermccann
Flag of United Kingdom of Great Britain and Northern Ireland image

I take it you have user's table and user_id is the primary key for that table?

Have you queried the database directly to see what the result is?

Avatar of mimoser

ASKER

I have not queried the database, as I not sure what I would need to do. My tables setup is below

photo_id - index
photo_name
photo_caption
photo_catagory
user_id

Its setup so the same user_id can be in up to 12 times like
photo_id = 105
photo_filename = 105.jpg
photo_caption = new pic
photo_catagory = 0
user_id = 23

kind of like that.
Here is also my uplaod page, maybe the problem is in there

upload.php
 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 
$res = mysql_query("select count(*) from gallery_photos WHERE user_id = '$user_id'");
if(!$res) die(mysql_error());
$count = mysql_result($res,0);
if($count >= 10) {
  echo "You have reached your twelve picture limit";
} else {
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 
$images_dir = "pixx";
$sql = "SELECT user_id FROM voc2_who WHERE session='".$session."'";
$result = mysql_query($sql);
$id= '';
if ($row = mysql_fetch_array($result)) {
    $user_id= $row['user_id'];
}
 
	// initialization
	$result_final = "";
	$counter = 0;
 
	// List of our known photo types
	$known_photo_types = array( 
						'image/pjpeg' => 'jpg',
						'image/jpeg' => 'jpg',
						'image/gif' => 'gif',
						'image/bmp' => 'bmp',
						'image/x-png' => 'png'
					);
	
	// GD Function List
	$gd_function_suffix = array( 
						'image/pjpeg' => 'JPEG',
						'image/jpeg' => 'JPEG',
						'image/gif' => 'GIF',
						'image/bmp' => 'WBMP',
						'image/x-png' => 'PNG'
					);
 
	// Fetch the photo array sent by preupload.php
	$photos_uploaded = $_FILES['photo_filename'];
 
	// Fetch the photo caption array
	$photo_caption = $_POST['photo_caption'];
$user_id = $_GET['user_id'];
	while( $counter <= count($photos_uploaded) )
	{
		if($photos_uploaded['size'][$counter] > 0)
		{
			if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
			{
				$result_final .= "File ".($counter+1)." is not a photo<br />";
			}
			else
			{
 
	  	mysql_query( "INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`, `user_id`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."', '".$user_id."')" );
		$new_id = mysql_insert_id();
				$filetype = $photos_uploaded['type'][$counter];
				$extention = $known_photo_types[$filetype];
				$filename = $new_id.".".$extention;
 
				mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );
 
				// Store the orignal file
				copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename);
 
				// Let's get the Thumbnail size
				$size = GetImageSize( $images_dir."/".$filename );
				if($size[0] > $size[1])
				{
					$thumbnail_width = 100;
					$thumbnail_height = (int)(100 * $size[1] / $size[0]);
				}
				else
				{
					$thumbnail_width = (int)(100 * $size[0] / $size[1]);
					$thumbnail_height = 100;
				}
			
				// Build Thumbnail with GD 1.x.x, you can use the other described methods too
				$function_suffix = $gd_function_suffix[$filetype];
				$function_to_read = "ImageCreateFrom".$function_suffix;
				$function_to_write = "Image".$function_suffix;
 
				// Read the source file
				$source_handle = $function_to_read ( $images_dir."/".$filename ); 
				
				if($source_handle)
				{
					// Let's create an blank image for the thumbnail
				     	$destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height );
				
					// Now we resize it
			      	ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );
				}
 
				// Let's save the thumbnail
				$function_to_write( $destination_handle, $images_dir."/tb_".$filename );
				ImageDestroy($destination_handle );
				//
 
				$result_final .= "<img src='".$images_dir. "/".$filename."' / height=\"200\" width=\"200\"> File ".($counter+1)." Added<br />";
			}
		}
	$counter++;
	}
 
	// Print Result
echo <<<__HTML_END
 
<html>
<head>
	<title>Photos uploaded</title>
</head>
<body>
	$result_final
</body>
</html>
 
__HTML_END;
}

Open in new window

Avatar of mimoser

ASKER

Below is the user_info.php page, the form
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 
$user_id = $_GET['user_id'];
$res = mysql_query("select count(*) from gallery_photos WHERE user_id = '$user_id'");
if(!$res) die(mysql_error());
$count = mysql_result($res,0);
if($count >= 12) {
  echo "You Have Reached Your Twelve Picture Limit";
  ?>
  To Delete Pics <a href="code.php?user_id=<?php echo $user_id; ?>">Click Here</a> 
  <?php
} else {
  # continue processing here
 
 
 
 
	$photo_upload_fields = "";
	$counter = 1;
	$number_of_fields = 3; 
	if( $_GET['number_of_fields'] )
	$number_of_fields = (int)($_GET['number_of_fields']);
	$result = mysql_query( "SELECT category_id,category_name FROM gallery_category" );
	while( $row = mysql_fetch_array( $result ) )
	{
$photo_category_list .=<<<__HTML_END
	<option value="$row[0]">$row[1]</option>\n
__HTML_END;
	}
	mysql_free_result( $result );	
	while( $counter <= $number_of_fields )
	{
$photo_upload_fields .=<<<__HTML_END
<tr>
	<td>
	     Photo {$counter}:
	    <input name=' photo_filename[]' type='file' />
	</td>
</tr>
<tr>
	<td>
	     Caption:
	    <textarea name='photo_caption[]' cols='30' rows='1'></textarea>
	</td>
</tr>
__HTML_END;
	$counter++;
	}
echo <<<__HTML_END
<html>
<head>
	<title>Lets upload Photos</title>
</head>
<body>
<form enctype='multipart/form-data' action='upload.php?user_id=$user_id' method='post' name='upload_form'>
<table width='90%' border='0' align='center' style='width: 90%;'>
 
<tr>
	<td>
		<p>&nbsp;</p>
	</td>
</tr>
<!-Insert the photo fields here -->
$photo_upload_fields
<tr>
	<td>
	        <input type='submit' name='submit' value='Add Photos' />
	</td>
</tr>
</table>
</form>
 
</body>
</html>
__HTML_END;
}

Open in new window

I am reviewing your code just now and have just noticed in the upload page you have restricted the number of photos to 10.

Could user's have reached this limit or have they not been able to upload at all?

if($count >= 10) {
  echo "You have reached your twelve picture limit";
} else {

Open in new window

Avatar of mimoser

ASKER

not at all, it automatically tells them they have reached the limit, even if they have not uploaded.
ASKER CERTIFIED SOLUTION
Avatar of christophermccann
christophermccann
Flag of United Kingdom of Great Britain and Northern Ireland 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
Avatar of mimoser

ASKER

Thanks