asked on
$prodId = (int)$_POST['id'];
$grabImage = sprintf("SELECT image_id, image, prod_id
FROM prod_images
WHERE prod_id = %d
ORDER BY image_id", $prodId);
$gotImage = mysql_query($grabImage);
$p = mysql_fetch_array($gotImage);
$k=0;
while($prod_det=mysql_fetch_array($gotImage))
{
$oldimg[$k]=$prod_det['image'];
$imgid[$k]=$prod_det['image_id'];
$k++;
}
if($_FILES['image']['name'] != "")
{
if($oldimg[0] != "")
{
echo $oldimg[0];
@unlink("../images/items/".$oldimg[0]);
$target_path = '../images/items/' .basename($_FILES['image']['name']);
funResizeImagesUserside($_FILES['image']['tmp_name'], $target_path,'height',430,340);
$action = basename($target_path);
#--------------------------------- Update Image
$updateImages = sprintf("UPDATE prod_images
SET image = '%s'
WHERE image_id = %d", $action, $imgid[0]);
$imagesUpdated = mysql_query($updateImages) or die("Images were not updated because: " . mysql_error());
#---------------------------------- Update Color
$updatecolor = sprintf("UPDATE prod_colors
SET color = '%s'
WHERE image_id = %d", $color, $imgid[0]);
mysql_query($updatecolor);
}
else if($oldimg[0] == "")
{
echo "Got here 2";
$target_path = '../images/items/' . basename($_FILES['image']['name']);
funResizeImagesUserside($_FILES['image']['tmp_name'], $target_path,'height',430,340);
$action = basename($target_path);
#--------------------------------- Update Image
$updateImages = sprintf("UPDATE prod_images
SET image = '%s'
WHERE image_id = %d", $action, $imgid[0]);
$imagesUpdated = mysql_query($updateImages) or die("Images were not updated because: " . mysql_error());
#---------------------------------- Update Color
$updatecolor = sprintf("UPDATE prod_colors
SET color = '%s'
WHERE image_id = %d", $color, $imgid[0]);
mysql_query($updatecolor);
}
else
{
echo "Got here 3";
$target_path = '../images/items/' . basename($_FILES['image']['name']);
#----------------------- Insert Image
funResizeImagesUserside($_FILES['image']['tmp_name'], $target_path,'height',430,340);
$action = basename($target_path1);
$updateImage = sprintf("INSERT INTO
prod_images
(image, prod_id)
VALUES('%s',%d)", $action, $p['prod_id']);
$imagesUpdated = mysql_query($updateImages) or die("Images were not updated because: " . mysql_error());
$igid=mysql_insert_id();
#--------------
$insqry=sprintf("INSERT INTO prod_colors
(color, image_id, prod_id)
VALUES('%s',%d,%d)", $color, $igid, $p['prod_id']);
mysql_query($insqry);
}
}
ASKER
ASKER
ASKER
ASKER
if(mysql_num_rows($imageFound) > 0)
{
if($image[0]!='')
echo "<p><img src='../images/items/$image[0]' width='90' height=80 border='0'> <a href='editproduct.php?pos={$imageID[0]}'>Delete</a></p>";
echo " <p> <label for='image'>Image</label> <input type='file' name='image' /></p>
<p> <label for='color'>Colors</label><input type='text' name='color' value='{$clr[0]}' /></p>";
if($image[1]!='')
echo "<p><img src='../images/items/$image[1]' width='90' height=80 border='0'> <a href='editproduct.php?pos={$imageID[1]}'>Delete</a></p>";
echo "<p> <label for='image1'>Image 1</label> <input type='file' name='image1' /> </p>
<p> <label for='color'>Color1</label><input type='text' name='color1' value='{$clr[1]}' /></p>";
if($image[2]!='')
echo "<p><img src='../images/items/$image[2]' width='90' height=80 border='0'> <a href='editproduct.php?pos={$imageID[2]}'>Delete</a></p>";
echo "<p> <label for='image2'>Image 2</label> <input type='file' name='image2' /> </p>
<p> <label for='color'>Color2</label> <input type='text' name='color2' value='{$clr[2]}' /></p>";
if($image[3]!='')
echo "<p><img src='../images/items/$image[3]' width='90' height=80 border='0'> <a href='editproduct.php?pos={$imageID[3]}'>Delete</a></p>";
echo "<p> <label for='image3'>Image 3</label> <input type='file' name='image3' /></p>
<p> <label for='color'>Color3</label><input type='text' name='color3' value='{$clr[3]}' /></p>";
if($image[4]!='')
echo "<p><img src='../images/items/$image[4]' width='90' height=80 border='0'> <a href='editproduct.php?pos={$imageID[4]}'>Delete</a></p>";
// SET A VARIABLE TO AN INTEGER VALUE OR ZERO IF POST-id IS NOT SET
$prodId = (int)$_POST['id'];
// CONFIGURE A QUERY STRING
$grabImage = sprintf("SELECT image_id, image, prod_id
FROM prod_images
WHERE prod_id = %d
ORDER BY image_id", $prodId);
// RUN THE QUERY AND ASSIGN A RESOURCE OR FALSE TO THE gotImage VARIABLE
$gotImage = mysql_query($grabImage);
// REMOVE THE FIRST ROW OF THE RESULTS
$p = mysql_fetch_array($gotImage);
$k=0;
// USE THE WHILE ITERATOR TO ACCESS THE OTHER ROWS OF THE RESULTS SET
while($prod_det=mysql_fetch_array($gotImage))
{
$oldimg[$k]=$prod_det['image'];
$imgid[$k]=$prod_det['image_id'];
$k++;
}
//First check if there was a record already for that image.
if(array_key_exists('0', $oldimg)) {
if(!$oldimg[0]) {
//The entry is blank, update record accordingly.
} else {
//The entry contains old image name, update record accordingly
}
} else {
//There are no records for this image index, insert the record in the table.
}
ASKER
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.
TRUSTED BY
Is the empty value actually a blank string "" or a NULL?
If its NULL or anything else like " " (single or more spaces) then statement, if($oldimg[0] != ""), will be true even for the NULL.
You should use instead is_null or === for this check.
e.g.
if($oldimg[0] != "" || !is_null($oldimg[0]))
similarly, if($oldimg[0] == "" || is_null($oldimg[0]))
Hope this helps.