How to an Update Image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smanager3
Forum Newbie
Posts: 3
Joined: Mon Feb 25, 2008 6:56 am

How to an Update Image

Post by smanager3 »

~pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Php is new to me but im getting there.... i think.

I have created a mysql tabel, which contains infomation about a property including an image.

I have made my control pannel and can insert all the infomation and the photo into the table, and echo the information on the relivant page again including the image. My problem comes where i try to update the image in the table.

In my form i click browes and select my file from its location in the host server. However once i have selected the file, the location changes from the one i chose to a location in my temp folder. All the other fields update fine. 

here is my code... sorry if its a bit messy.

Code: Select all

<?php require_once('../../Connections/burnell_properties.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";
 
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 
 
  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}
 
$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "editform")) {
  $updateSQL = sprintf("UPDATE properties SET location=%s, streetname=%s, bedrooms=%s, `description`=%s, image=%s WHERE ID=%s",
                       GetSQLValueString($_POST['location'], "text"),
                       GetSQLValueString($_POST['streetname2'], "text"),
                       GetSQLValueString($_POST['bedrooms'], "text"),
                       GetSQLValueString($_POST['description'], "text"),
                       GetSQLValueString($_POST['file'], "text"),
                       GetSQLValueString($_POST['ID'], "int"));
 
  mysql_select_db($database_burnell_properties, $burnell_properties);
  $Result1 = mysql_query($updateSQL, $burnell_properties) or die(mysql_error());
}
 
$colname_editsearch = "-1";
if (isset($_POST['ID'])) {
  $colname_editsearch = $_POST['ID'];
}
mysql_select_db($database_burnell_properties, $burnell_properties);
$query_editsearch = sprintf("SELECT * FROM properties WHERE ID = %s", GetSQLValueString($colname_editsearch, "int"));
$editsearch = mysql_query($query_editsearch, $burnell_properties) or die(mysql_error());
$row_editsearch = mysql_fetch_assoc($editsearch);
$totalRows_editsearch = mysql_num_rows($editsearch);
 
$currentPage = $_SERVER["PHP_SELF"];
 
$image = "" . $_FILES['file']['name'] . ""; 
 
$queryString_burnell_properties = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_burnell_properties") == false && 
        stristr($param, "totalRows_burnell_properties") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_burnell_properties = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_burnell_properties = sprintf("&totalRows_burnell_properties=%d%s", $totalRows_burnell_properties, $queryString_burnell_properties);
 
//perform quit (closes mysql)
$result_quit = mysql_query("quit");
 
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.centrestyle {position: relative;
    width: 1000px;
    margin-right: auto;
    margin-left: auto;
    background-repeat: no-repeat;
    height: 810px;
}
.titlestyle {font-family: Batang;
    text-align: center;
    color: #000080;
    border-top-style: inset;
    border-bottom-style: inset;
    border-top-color: #0000FF;
    border-bottom-color: #000080;
    font-size: 45px;
    text-transform: uppercase;
    line-height: 1px;
    margin-right: auto;
    margin-left: auto;
}
.titlestyleCopy2 {font-family: Batang;
    text-align: center;
    color: #000080;
    border-top-style: inset;
    border-bottom-style: inset;
    border-top-color: #0000FF;
    border-bottom-color: #000080;
    font-size: 22px;
    text-transform: uppercase;
}
#apDiv1 {
    position:absolute;
    left:57px;
    top:120px;
    width:195px;
    height:354px;
    z-index:5;
}
#apDiv2 {position:absolute;
    left:50px;
    top:18px;
    width:997px;
    height:1079px;
    z-index:1;
}
#apDiv3 {position:absolute;
    left:49px;
    top:85px;
    width:1002px;
    height:23px;
    z-index:2;
}
#apDiv6 {
    position:absolute;
    left:294px;
    top:118px;
    width:557px;
    height:180px;
    z-index:6;
}
.tablestyleedit {
    font-family: Arial, Helvetica, sans-serif;
    color: #000080;
    text-align: justify;
    vertical-align: text-top;
    font-size: 12px;
}
#apDiv4 {
    position:absolute;
    left:323px;
    top:124px;
    width:690px;
    height:600px;
    z-index:6;
}
-->
</style>
<script src="../Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
<script src="../../SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="../../SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<link href="../../SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
</head>
 
<body>
<div class="centrestyle" id="apDiv2">
  <div class="titlestyle">
    <p>burnell property services </p>
  </div>
  <p><img src="../Images/wolCapture.JPG" alt="Burnell Properites" width="997" height="671" /></p>
</div>
<div class="titlestyleCopy2" id="apDiv3">LETTINGS ~ MANAGEMENT ~ REFURBS</div>
<div id="apDiv1">
  <div>
    <p align="center">&nbsp;</p>
    <ul id="MenuBar1" class="MenuBarHorizontal">
      <li><a href="add_properties.php">Add Property</a> </li>
      <p>&nbsp;</p>
      <li><a href="search_properties.php">Update Property</a></li>
      <p>&nbsp;</p>
      <li><a href="delete_search_properties.php">Delete Property</a> </li>
      <p>&nbsp;</p>
      <li><a href="burnell_properties.php">View Properties</a></li>
    </ul>
    <p align="center">&nbsp;</p>
    <p align="center">&nbsp;</p>
    <p align="center">&nbsp;</p>
    <p align="center">&nbsp;</p>
    <p align="center">&nbsp;</p>
    <p align="center">&nbsp; </p>
  </div>
</div>
<div id="apDiv4">
  <div>
    <p align="center">Edit Properties</p>
    <div>
      <p>
        <label for="textarea"></label>
      </p>
    
            <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="editform" id="editform">
              <table width="400" border="3" align="center" cellpadding="3" cellspacing="2">
                <tr>
                  <th scope="row">ID</th>
                  <td><label for="ID"></label>
<select name="ID" id="ID" title="<?php echo $row_editsearch['ID']; ?>">
  <?php
do {  
?>
  <option value="<?php echo $row_editsearch['ID']?>"<?php if (!(strcmp($row_editsearch['ID'], $row_editsearch['ID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_editsearch['ID']?></option>
  <?php
} while ($row_editsearch = mysql_fetch_assoc($editsearch));
  $rows = mysql_num_rows($editsearch);
  if($rows > 0) {
      mysql_data_seek($editsearch, 0);
      $row_editsearch = mysql_fetch_assoc($editsearch);
  }
?>
                    </select></td>
                </tr>
                <tr>
                  <th scope="row">Property Location</th>
                  <td><label for="location"></label>
                      <select name="location" id="location" >
                        <option value="Select">Select</option>
                        <option value="Bishop Auckland">Bishop Auckland</option>
                        <option value="Brandon">Brandon</option>
                        <option value="Crook">Crook</option>
                        <option value="Esh Winning">Esh Winning</option>
                        <option value="Howden-le-Wear">Howden-le-Wear</option>
                        <option value="New Brancepeth">New Brancepeth</option>
                        <option value="Tow Law">Tow Law</option>
                        <option value="Ushaw Moor">Ushaw Moor</option>
                    </select></td>
                </tr>
                <tr>
                  <th scope="row">Street Name</th>
                  <td><label for="streetname"></label>                      <label for="streetname"></label>
                    <input name="streetname2" type="text" value="<?php echo $row_editsearch['streetname']; ?>" size="32" /></td>
                </tr>
                <tr>
                  <th scope="row">No. OF Bed Rooms</th>
                  <td><label for="bedrooms"></label>
                      <select name="bedrooms" id="bedrooms">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                      </select>                  </td>
                </tr>
                <tr>
                  <th scope="row">Description</th>
                  <td><label for="description"></label>
                  <textarea name="description" id="description" cols="45" rows="5"><?php echo $row_editsearch['description']; ?></textarea></td>
                </tr>
                <tr>
                  <th scope="row">Photo Upload</th>
                  <td><label for="file"></label>
                  <input name="file" type="file" id="file" value="<?php echo $row_editsearch['image']; ?>" /></td>
                </tr>
                <tr>
                  <th scope="row">&nbsp;</th>
                  <td><input type="submit" name="submit" value="Submit" /></td>
                </tr>
              </table>
              <div align="center"></div>
              <p>&nbsp;</p>
              <p>&nbsp;</p>
              <p>&nbsp;</p>
              <p>&nbsp;</p>
              <p>&nbsp;</p>
              <p>&nbsp;</p>
              
                      
                      
                      <input type="hidden" name="MM_update" value="editform" />
            </form>
      <p>&nbsp;</p>
    
                        <form method="post" name="form1" id="form1">
      </form>
    </div>
    <p>&nbsp;</p>
  </div>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"../../SpryAssets/SpryMenuBarDownHover.gif", imgRight:"../../SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>
</body>
</html>
<?php
mysql_free_result($editsearch);
?>
Any help is much appreciated.


~pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to an Update Image

Post by pickle »

To clarify for myself: The problem is that when you choose a new image to upload, the path to the image that gets stored, isn't to the new image, but to some temporary location?

What does your query look like. Echo it before you execute it to see if it's what you expect.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: How to an Update Image

Post by yacahuma »

you have to use move_uploaded_file and save the file to wherever you want.
smanager3
Forum Newbie
Posts: 3
Joined: Mon Feb 25, 2008 6:56 am

Re: How to an Update Image

Post by smanager3 »

Thanks for your quick replys

Answer to Pickel's question.

All the photos that will be used to 'update' my pages are already saved on the server. Using my UPDATE form i click browse and select which ever photo is required via my network location, which should change the file name in that specific row in the database.

I select the file however and the path changes in the file field to C:\Users\BILL\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\.... blar blar blar but doesn't chage in my actual web page as the image path is set to the correct folder. And i doesn't update the database with the new file name.

again thanks and i'll investigate into the move_upload_file and echoing the mysql_query....... anymore ideas will be most appreciated.
smanager3
Forum Newbie
Posts: 3
Joined: Mon Feb 25, 2008 6:56 am

Re: How to an Update Image

Post by smanager3 »

Hummm well i think i have found an alternitive solution to my problem. I have just removed the file field in my form and made it into a file name drop down list. which holds the file names as values. and works perfect. just need to update my file list if i add any alternitive images to my host.

Thanks for the help.
Post Reply