Image update to database problem.
Posted: Thu Jan 20, 2005 4:10 am
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
display_vinyl_form.fns.php
edit_vinyl.php
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.
{
$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) {
print "Database error when executing query error was: ";
print mysql_error();
exit;
}
if ((isset($_FILESї'picture']ї'name']) && is_uploaded_file($_FILESї'picture']ї'tmp_name'])))
{
if (!isset($story))
$story = mysql_insert_id();
$type = basename($_FILESї'picture']ї'type']);
switch ($type) {
case 'jpeg':
case 'pjpeg': $filename = "images/$catno.jpg";
move_uploaded_file($_FILESї'picture']ї'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ї'picture']ї'type'];
}
}
}
function delete_vinyl($catno)
// deletes vinyl identfied by it's cat_no from the database
{
$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;
}
function filled_out($form_vars)
{
// function used to test that each variable has a value if not the form is not filled out.
foreach ($form_vars as $key => $value)
{
if (!isset($key) || ($value == ''))
return false;
}
return true;
}
?>Code: Select all
<?php
//display_vinyl_form.php
require_once('shopping_cart_fns.php');
?>
<script>
function checkIt(){
if(confirm("Delete Vinyl from Shop")){
document.MyForm.submit();
}
}
</script>
<?php
function display_vinyl_form($vinyl = '')
{
$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ї'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ї'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)
{
echo '<option value="';
echo $thisartistї'artist_id'];
echo '"';
if ($edit && $thisartistї'artist_id'] == $vinylї'artist_id'])
echo 'selected';
echo '>';
echo $thisartistї'artist_id'];
echo'</option>';
echo"\n";
}
?>
</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)
{
echo '<option value="';
echo $thisgenreї'genre_ref'];
echo '"';
if ($edit && $thisgenreї'genre_ref'] == $vinylї'genre_ref'])
echo 'selected';
echo '>';
echo $thisgenreї'genre_ref'];
echo'</option>';
echo"\n";
}
?>
</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)
{
echo '<option value="';
echo $thisformatї'format_type'];
echo '"';
if ($edit && $thisformatї'format_type'] == $vinylї'format_type'])
echo 'selected';
echo '>';
echo $thisformatї'format_type'];
echo'</option>';
echo"\n";
}
?>
</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)
{
echo '<option value="';
echo $num;
echo '"';
echo '>';
echo $num;
echo '</option>';
echo "\n";
} ?>
</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)
{
echo '<option value="';
echo $num;
echo '"';
echo '>';
echo $num;
echo '</option>';
echo "\n";
} ?>
</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)
{
echo '<option value="';
echo $thisstatusї'status_code'];
echo '"';
echo '>';
echo $thisstatusї'status_description'];
echo'</option>';
echo"\n";
}
?>
</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ї'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ї'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ї'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ї'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ї'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ї'image']) {
$size = getImageSize($vinylї'image']);
$width = $sizeї0];
$height = $sizeї1];
?>
<tr>
<td>Image: </td>
<td align="center">
<?php echo '<img src"'.$vinylїimage].'"';?>
width="<?php print $width;?>" height="<?php print $height;?>">
</td>
</tr>
<?php } ?>
<tr>
<td <?php if(!$edit) echo 'colspan="2"'; ?> align="center">
<?php
if ($edit)
echo '<input type="hidden" name="oldcatno" value="'.$vinylї'cat_no'].'">';
?>
<td><input type="submit" value="<?php echo $edit?'Update' : 'Add';?>Record"></form><?php if($edit)
{
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ї'cat_no'].'">';
echo '</form></td>';
} ?>
</tr>
<table></form> </center>
<div align="left"></div></td>
</tr>
</table></td>
</tr>
</table>
<?
}
do_html_footer();
?>Code: Select all
<?php
session_start();
require('page.inc');
require_once('update_delete_fns.php');
$editform = new Page();
$editform -> Display();
$catno = $_POSTї'cat_no'];
$title = $_POSTї'title'];
$artist = $_POSTї'artist_id'];
$genre = $_POSTї'genre_ref'];
$price = $_POSTї'price'];
$qty = $_POSTї'qty'];
$status = $_POSTї'status_code'];
$info = $_POSTї'comments'];
$image = $_POSTї'picture'];
$year = $_POSTї'year'];
$month = $_POSTї'month'];
$comments = $_POSTї'short_comments'];
$label = $_POSTї'label'];
$format = $_POSTї'format'];
$oldcatno = $_POSTї'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))
{
$catno = $_POSTї'cat_no'];
$title = $_POSTї'title'];
$artist = $_POSTї'artist_id'];
$genre = $_POSTї'genre_ref'];
$price = $_POSTї'price'];
$qty = $_POSTї'qty'];
$status = $_POSTї'status_code'];
$info = $_POSTї'comments'];
$image = $_POSTї'picture'];
$year = $_POSTї'year'];
$month = $_POSTї'month'];
$comments = $_POSTї'short_comments'];
$label = $_POSTї'label'];
$format = $_POSTї'format'];
$oldcatno = $_POSTї'oldcatno'];
if (update_vinyl($oldcatno, $catno, $title, $artist, $genre, $price, $qty, $label, $format , $year, $month, $comments, $status, $info))
{
echo 'vinyl was updated.<br />';
}
else
echo 'vinyl could not be updated.<br />';
}
do_html_footer();
?>