Page 1 of 1

Image update to database problem.

Posted: Thu Jan 20, 2005 4:10 am
by mmc01ms
i've created a edit form where you can edit any details of a particular vinyl. Everything works fine and everything can be changed in the database except the image. The form doesn't seem to replace the image i want to replace.It doesn't upload the new image to the host. code is below any help would be great. cheers


update_delete_fns.php

Code: Select all

<?php
	
	//script written with the help of chapter 26 PHP & Web Development by Luke Welling 06/01/2005
	
	
	require_once('display_vinyl_form.php');
	
	
	
	function update_vinyl($oldcatno, $catno, $title, $artist, $genre, $price, $qty, $label, $format , $year, $month, $comments, $status, $info)
		
		
		
		//updates the required data into the database. Mostly used to update stock levels.
	&#123;
		$link_id = db_connect();
		
		
		$query = "update vinyls
             set cat_no='$catno',
			 title ='$title',
             artist_id = '$artist',
             genre_ref = '$genre',
             price = '$price',
 			 stock_level = '$qty',
			 label = '$label',
			 format_type = '$format',
             year = '$year',
			 month = '$month',
			 comments = '$comments',
			 status_code = '$status',
			 vinyl_information = '$info'
             where cat_no='$oldcatno'";
			
			echo $query.'<br />'; //debugging 
		$result = mysql_query($query, $link_id);
		
		if(!$result) &#123;
			print "Database error when executing query error was: ";
			print mysql_error();
			exit;
		&#125;
		
		if ((isset($_FILES&#1111;'picture']&#1111;'name']) && is_uploaded_file($_FILES&#1111;'picture']&#1111;'tmp_name'])))
		&#123;
			if (!isset($story)) 
				$story = mysql_insert_id();
			
			$type = basename($_FILES&#1111;'picture']&#1111;'type']);
			
			switch ($type) &#123;
			case 'jpeg':
			case 'pjpeg':	$filename = "images/$catno.jpg";
				move_uploaded_file($_FILES&#1111;'picture']&#1111;'tmp_name'], $filename);
				$query = "update vinyls set image = '$filename' where cat_no = $catno";
				$result = mysql_query($query, $link_id);
				break;
			default:		print 'Invalid Picture Format: '.
				$_FILES&#1111;'picture']&#1111;'type'];
			&#125;
		&#125;
		
		
	&#125;
	
	function delete_vinyl($catno)
		// deletes vinyl identfied by it's cat_no from the database
	&#123;
		$link_id = db_connect();
		
		$query = "delete from vinyls
             where cat_no='$catno'";
			$result = @mysql_query($query, $link_id);
		if (!$result)
			return false;
		else
			return true;
	&#125;
	
	function filled_out($form_vars)
	&#123;
		// function used to test that each variable has a value if not the form is not filled out.
		foreach ($form_vars as $key => $value)
		&#123;
			if (!isset($key) || ($value == '')) 
				return false;
		&#125; 
		return true;
	&#125;
	
	
?>
display_vinyl_form.fns.php

Code: Select all

<?php 
	
	//display_vinyl_form.php
	
	require_once('shopping_cart_fns.php');
	
	?>
	
	
<script> 
		 function checkIt()&#123; 
			if(confirm("Delete Vinyl from Shop"))&#123; 
				document.MyForm.submit(); 
			&#125; 
		&#125; 
</script> 
		
		
		
		
		
		<?php
		
		function display_vinyl_form($vinyl = '')
	&#123;
		
		$edit = is_array($vinyl);
?>
<center><form align="center" name="newrecord" method="post" action="<?php echo $edit?'edit_vinyl.php' : 'insert_vinyl.php';?>">
		<table bordercolor="#FFFFFF" border="1" bgcolor="#7b9815">
			<font color="#000000" size="2" face="Courier New, Courier, mono">
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Cat_No: </font></td>
					<td><input name="cat_no" type="text" maxlength="6" value="<?php echo $vinyl&#1111;'cat_no']; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Title: </font></td>
					<td><input name="title" type="text" maxlength="40" value="<?php echo $edit?$vinyl&#1111;'title']:' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Artist: </font></td>
					<td><select name="artist_id">
							<?php
		$artist_array=get_artists();
		foreach ($artist_array as $thisartist)
		&#123;
			echo '<option value="';
			echo $thisartist&#1111;'artist_id'];
			echo '"';
			if ($edit && $thisartist&#1111;'artist_id'] == $vinyl&#1111;'artist_id'])
				echo 'selected';
			echo '>';
			echo $thisartist&#1111;'artist_id'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Genre: </font></td>
					<td><select name="genre_ref">
							<?php
		$genre_array=get_genres();
		foreach ($genre_array as $thisgenre)
		&#123;
			echo '<option value="';
			echo $thisgenre&#1111;'genre_ref'];
			echo '"';
			if ($edit && $thisgenre&#1111;'genre_ref'] == $vinyl&#1111;'genre_ref'])
				echo 'selected';
			echo '>';
			echo $thisgenre&#1111;'genre_ref'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Formats: </font></td>
					<td><select name="format">
							<?php
		$format_array=get_formats();
		foreach ($format_array as $thisformat)
		&#123;
			echo '<option value="';
			echo $thisformat&#1111;'format_type'];
			echo '"';
			if ($edit && $thisformat&#1111;'format_type'] == $vinyl&#1111;'format_type'])
				echo 'selected';
			echo '>';
			echo $thisformat&#1111;'format_type'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Month: </font></td>
					<td><select name="month"><?php 
		$nums = range(1, 12);
		foreach ($nums as $num) 
		&#123;
			echo '<option value="';
			echo $num;
			echo '"';
			echo '>';
			echo $num;
			echo '</option>';
			echo "\n";
		&#125; ?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Year: </font></td>
					<td><select name="year"><?php 
		$nums = range(1994, 2006);
		foreach ($nums as $num) 
		&#123;
			echo '<option value="';
			echo $num;
			echo '"';
			echo '>';
			echo $num;
			echo '</option>';
			echo "\n";
		&#125; ?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Vinyl Status: </font></td>
					<td><select name="status_code">
							<?php
		$status_array=get_status();
		foreach ($status_array as $thisstatus)
		&#123;
			echo '<option value="';
			echo $thisstatus&#1111;'status_code'];
			echo '"';
			echo '>';
			echo $thisstatus&#1111;'status_description'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Price: </font></td>
					<td><input name="price" type="text" maxlength="5" id="price" value="<?php echo $edit?$vinyl&#1111;'price']: ' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Qty: </font></td>
					<td><input name="qty" type="text" maxlength="3" id="qty" value="<?php echo $edit?$vinyl&#1111;'stock_level']: ' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Label: </font></td>
					<td><input name="label" type="text" maxlength="3" id="qty" value="<?php echo $edit?$vinyl&#1111;'label']: ' '; ?>"></td>
				</tr>
				
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Comments: </font></td>
					<td><input name="short_comments" type="text" maxlength="30" id="short_comments" value="<?php echo $edit?$vinyl&#1111;'comments']: ' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Vinyl Information:</font></td><td><textarea name="comments" maxlength="225" cols="60" rows="10"><?php echo $vinyl&#1111;'vinyl_information']?></textarea></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Image Link:</font></td>
				<td align="left"><input type="file" name="picture" size="40"></td>
			</tr>
			
			<?php
						
						if($vinyl&#1111;'image']) &#123;
							$size = getImageSize($vinyl&#1111;'image']);
							$width = $size&#1111;0];
							$height = $size&#1111;1];
			?>
			
			<tr>
					<td>Image: </td>
				<td align="center">
					<?php echo '<img src"'.$vinyl&#1111;image].'"';?>
						width="<?php print $width;?>" height="<?php print $height;?>">
				</td>
			</tr>
			<?php &#125; ?>
				<tr>
					<td <?php if(!$edit) echo 'colspan="2"'; ?> align="center">
						<?php
		if ($edit)
			echo '<input type="hidden" name="oldcatno" value="'.$vinyl&#1111;'cat_no'].'">';
		
						?>
						<td><input type="submit" value="<?php echo $edit?'Update' : 'Add';?>Record"></form><?php if($edit)
		&#123;
			echo '';
			echo '<form name="MyForm" action="delete_vinyl.php"> ';
			echo '<input type="button" value="Delete Vinyl" onClick="checkIt();"> ';
			echo '<input type="hidden" name="cat_no" value="'.$vinyl&#1111;'cat_no'].'">';
			echo '</form></td>';
		&#125; ?>
					</tr>
					<table></form>	</center>
				
				<div align="left"></div></td>
		</tr>
	</table></td>

			</tr>
			</table>
			<?
	&#125;
		
		do_html_footer();
					?>
edit_vinyl.php

Code: Select all

<?php
	session_start();
	
	require('page.inc');
	require_once('update_delete_fns.php');
	
	$editform = new Page();
	
	$editform -> Display();
	
	$catno = $_POST&#1111;'cat_no'];
	$title = $_POST&#1111;'title'];
	$artist = $_POST&#1111;'artist_id'];
	$genre = $_POST&#1111;'genre_ref'];
	$price = $_POST&#1111;'price'];
	$qty = $_POST&#1111;'qty'];
	$status = $_POST&#1111;'status_code'];
	$info = $_POST&#1111;'comments'];
	$image = $_POST&#1111;'picture'];
	$year = $_POST&#1111;'year'];
	$month = $_POST&#1111;'month'];
	$comments = $_POST&#1111;'short_comments'];
	$label = $_POST&#1111;'label'];
	$format = $_POST&#1111;'format'];
	$oldcatno = $_POST&#1111;'oldcatno'];
	
	
	/*if(empty($title)) error_message("No Title Entered");
	if(empty($price)) error_message("No Price Entered");
	if(empty($qty)) error_message("No Qty Entered");
	if(empty($info)) error_message("Please Enter some Information about the vinyl");
	if (strlen($catno) != 6 || !ctype_digit($catno)) error_message("Catalogue value needs to be 6 digits"); */
	

	
	
	
	if (filled_out($_POST))
	&#123;
		
		$catno = $_POST&#1111;'cat_no'];
		$title = $_POST&#1111;'title'];
		$artist = $_POST&#1111;'artist_id'];
		$genre = $_POST&#1111;'genre_ref'];
		$price = $_POST&#1111;'price'];
		$qty = $_POST&#1111;'qty'];
		$status = $_POST&#1111;'status_code'];
		$info = $_POST&#1111;'comments'];
		$image = $_POST&#1111;'picture'];
		$year = $_POST&#1111;'year'];
		$month = $_POST&#1111;'month'];
		$comments = $_POST&#1111;'short_comments'];
		$label = $_POST&#1111;'label'];
		$format = $_POST&#1111;'format'];
		$oldcatno = $_POST&#1111;'oldcatno'];
		
		if (update_vinyl($oldcatno, $catno, $title, $artist, $genre, $price, $qty,  $label, $format , $year, $month, $comments, $status, $info))
		&#123;
			echo 'vinyl was updated.<br />';
		&#125;
		
		else
			echo 'vinyl could not be updated.<br />';
	&#125;

		
		
		

	
	
	do_html_footer();
	
	
	
?>

Posted: Thu Jan 20, 2005 8:54 am
by feyd
you're using $_POST['picture'], you need to use $_FILES['picture']. Additionally, you didn't set the enc-type attribute of the form.

http://www.php.net/manual/en/features.file-upload.php