We help IT Professionals succeed at work.

upload image script in tandem with update database script

phillystyle123
phillystyle123 used Ask the Experts™
on
I'm using update sql to update my db. This works fine until I try to incorporate upload image code - which also works fine by itself. It appears as though the upload image code isn't even being recognized - not sure what i'm doing wrong
<?php require_once('../../Connections/connSmartBenefit.php'); ?>
<?php $dir=$_GET['directory_name'];?>
<?
//BEGIN UPLOAD IMAGE SCRIPT
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = "/home/smarter/public_html/".$dir."/images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}
//END UPLOAD IMAGE SCRIPT
?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}
//BEGIN UPDATE DB SCRIPT
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "step2")) {
  $updateSQL = sprintf("UPDATE company_info SET logo=%s WHERE directory_name=%s",
                       GetSQLValueString($_POST['logo'], "text"),
                       GetSQLValueString($_POST['directory_name'], "text"));

  mysql_select_db($database_connSmartBenefit, $connSmartBenefit);
  $Result1 = mysql_query($updateSQL, $connSmartBenefit) or die(mysql_error());

  $updateGoTo = "step3.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
//END UPDATE IMAGE SCRIPT
 $crumb="new";?>
<?php $crumb2="step2";?>



<!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><?php include('../includes/browser_title.php');?></title>
<link href="../admin.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrap">

<div id="contentWrap">
<?php include('../includes/admin_title.php');?>
<h2 align="center"><a href="../main_menu.php">Main Menu</a> > Create New Account</h2>
<?php include('../includes/steps.php');?>
<form enctype="multipart/form-data" action="<?php echo $editFormAction; ?>"  id="step2" name="step2" method="POST">
<p>Upload account company logo. 
<div class="formRow">
<label><strong>Logo:</strong>
<input class="input1" name="logo" type="file" id="logo" />
</label>
<input name="directory_name" type="text" id="directory_name" value="<?php echo $_GET['directory_name'];?>" />
</div>

<div class="formRow">new image: <?php echo $newimage;?></div>

  <input class="submit"  type="submit"  value="Upload Image" name="action"/>
  <input type="hidden" name="MM_update" value="step2">
</form>

<?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>

</div>


</body>

</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NerdsOfTechTechnology Scientist
Commented:
add php to line 3 <?php
also see if you can put an echo line to debug the upload code
change line 3:

from:
<?

to:
<?php


change blank line 32 to:
echo 'test output';

Open in new window

Author

Commented:
thanks for helping - nothing changed yet - added php to <? and echo 'test output'; outputs absolutely nothing. it's like this upload script isn't being seen. when i use it by itself it uploads, works like a charm - when i use in tandom with my update code, it disappears
<?php require_once('../../Connections/connSmartBenefit.php'); ?>
<?php $dir=$_GET['directory_name'];?>
<?php
//BEGIN UPLOAD IMAGE SCRIPT
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = "/home/smarter/public_html/".$dir."/images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
echo 'test output';

}
//END UPLOAD IMAGE SCRIPT
?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}
//BEGIN UPDATE DB SCRIPT
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "step2")) {
  $updateSQL = sprintf("UPDATE company_info SET logo=%s WHERE directory_name=%s",
                       GetSQLValueString($_POST['logo'], "text"),
                       GetSQLValueString($_POST['directory_name'], "text"));

  mysql_select_db($database_connSmartBenefit, $connSmartBenefit);
  $Result1 = mysql_query($updateSQL, $connSmartBenefit) or die(mysql_error());

  $updateGoTo = "step3.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
//END UPDATE IMAGE SCRIPT
 $crumb="new";?>
<?php $crumb2="step2";?>



<!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><?php include('../includes/browser_title.php');?></title>
<link href="../admin.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrap">

<div id="contentWrap">
<?php include('../includes/admin_title.php');?>
<h2 align="center"><a href="../main_menu.php">Main Menu</a> > Create New Account</h2>
<?php include('../includes/steps.php');?>
<form enctype="multipart/form-data" action="<?php echo $editFormAction; ?>"  id="step2" name="step2" method="POST">
<p>Upload account company logo. 
<div class="formRow">
<label><strong>Logo:</strong>
<input class="input1" name="logo" type="file" id="logo" />
</label>
<input name="directory_name" type="text" id="directory_name" value="<?php echo $_GET['directory_name'];?>" />
</div>

<div class="formRow">new image: <?php echo $newimage;?></div>

  <input class="submit"  type="submit"  value="Upload Image" name="action"/>
  <input type="hidden" name="MM_update" value="step2">
</form>

<?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>

</div>


</body>

</html>

Open in new window

NerdsOfTechTechnology Scientist
Commented:
try:

bracketing the ifs
<?php 

require_once('../../Connections/connSmartBenefit.php'); 
$dir=$_GET['directory_name'];

//BEGIN UPLOAD IMAGE SCRIPT
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES)){
 $_FILES = $HTTP_POST_FILES;
}

if(!isset($_FILES['image_file'])){
 $error["image_file"] = "An image was not found.";
}


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename)){
$error["imagename"] = "The name of the image was not found.";
}

if(empty($error))
{
$newimage = "/home/smarter/public_html/".$dir."/images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
echo 'test output';

}
//END UPLOAD IMAGE SCRIPT

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}
//BEGIN UPDATE DB SCRIPT
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "step2")) {
  $updateSQL = sprintf("UPDATE company_info SET logo=%s WHERE directory_name=%s",
                       GetSQLValueString($_POST['logo'], "text"),
                       GetSQLValueString($_POST['directory_name'], "text"));

  mysql_select_db($database_connSmartBenefit, $connSmartBenefit);
  $Result1 = mysql_query($updateSQL, $connSmartBenefit) or die(mysql_error());

  $updateGoTo = "step3.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
//END UPDATE IMAGE SCRIPT
 $crumb="new";
 $crumb2="step2";
?>

<!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><?php include('../includes/browser_title.php');?></title>
<link href="../admin.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrap">

<div id="contentWrap">
<?php include('../includes/admin_title.php');?>
<h2 align="center"><a href="../main_menu.php">Main Menu</a> > Create New Account</h2>
<?php include('../includes/steps.php');?>
<form enctype="multipart/form-data" action="<?php echo $editFormAction; ?>"  id="step2" name="step2" method="POST">
<p>Upload account company logo. 
<div class="formRow">
<label><strong>Logo:</strong>
<input class="input1" name="logo" type="file" id="logo" />
</label>
<input name="directory_name" type="text" id="directory_name" value="<?php echo $_GET['directory_name'];?>" />
</div>

<div class="formRow">new image: <?php echo $newimage;?></div>

  <input class="submit"  type="submit"  value="Upload Image" name="action"/>
  <input type="hidden" name="MM_update" value="step2">
</form>

<?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>

</div>
</body>
</html>

Open in new window

Author

Commented:
i'm getting 2 errors - this at the top of the page:

Warning: Cannot modify header information - headers already sent by (output started at /home/smarter/public_html/admin/new/step2.php:4) in /home/smarter/public_html/admin/new/step2.php on line 48
test output

and this at the bottom:

An image was not found.
The name of the image was not found.
NerdsOfTechTechnology Scientist
Commented:
see if your requierd files are outputing <head> etc html tags...

try this first though:
<?php 

require_once('../../Connections/connSmartBenefit.php'); 
$dir=$_GET['directory_name'];

//BEGIN UPLOAD IMAGE SCRIPT
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES)){
 $_FILES = $HTTP_POST_FILES;
}

if(!isset($_FILES['image_file'])){
 $error["image_file"] = "An image was not found.";
}


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename)){
$error["imagename"] = "The name of the image was not found.";
}

if(empty($error))
{
$newimage = "/home/smarter/public_html/".$dir."/images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
echo 'test output';

}
//END UPLOAD IMAGE SCRIPT

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}
//BEGIN UPDATE DB SCRIPT
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "step2")) {
  $updateSQL = sprintf("UPDATE company_info SET logo=%s WHERE directory_name=%s",
                       GetSQLValueString($_POST['logo'], "text"),
                       GetSQLValueString($_POST['directory_name'], "text"));

  mysql_select_db($database_connSmartBenefit, $connSmartBenefit);
  $Result1 = mysql_query($updateSQL, $connSmartBenefit) or die(mysql_error());

  $updateGoTo = "step3.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
//END UPDATE IMAGE SCRIPT
 $crumb="new";
 $crumb2="step2";
?>

<link href="../admin.css" rel="stylesheet" type="text/css" />

<div id="wrap">

<div id="contentWrap">
<?php include('../includes/admin_title.php');?>
<h2 align="center"><a href="../main_menu.php">Main Menu</a> > Create New Account</h2>
<?php include('../includes/steps.php');?>
<form enctype="multipart/form-data" action="<?php echo $editFormAction; ?>"  id="step2" name="step2" method="POST">
<p>Upload account company logo. 
<div class="formRow">
<label><strong>Logo:</strong>
<input class="input1" name="logo" type="file" id="logo" />
</label>
<input name="directory_name" type="text" id="directory_name" value="<?php echo $_GET['directory_name'];?>" />
</div>

<div class="formRow">new image: <?php echo $newimage;?></div>

  <input class="submit"  type="submit"  value="Upload Image" name="action"/>
  <input type="hidden" name="MM_update" value="step2">
</form>

<?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>

</div>
</body>
</html>

Open in new window

Thanks so much for the help, NerdsOfTech - unfortunately I've gotta move on - I found an old script that has always worked perfectly for me to upload an image to the server and update the db. It's called FXInc - not even sure if it's still around.  I'd zip up the files and attach them here but I'm not sure if that's ok to do - I think I paid for it years ago. Anyway, I've attached the code that works with FxInc.
<?php require_once('../../Connections/connxxx.php'); ?>
<?php $dir=$_GET['directory_name'];?>
<?php
// *** Simply Upload ***
require_once("../FXInc/uploadAction.inc");
$errMsg = "";
$action = true;
$noPath = true;
$rename = true;
$delete = false;
$FX_successRedirect = "";
$FX_DirPath = "../../".$dir."/images/";
$FX_typearray = array("application","audio","image");
$FX_extarray = array();
$FX_size = "";
$FX_fields = array();
if ((isset($HTTP_POST_VARS["FX_upload"])) && ($HTTP_POST_VARS["FX_upload"] == "Form1")) {
  require_once("../FXInc/upload.inc");
}

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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"] == "step2")) {
  $updateSQL = sprintf("UPDATE company_info SET logo=%s WHERE directory_name=%s",
                       GetSQLValueString($_POST['logo'], "text"),
                       GetSQLValueString($_POST['directory_name'], "text"));

  mysql_select_db($database_connxxx, $connxxx);
  $Result1 = mysql_query($updateSQL, $connxxx) or die(mysql_error());

  $updateGoTo = "step3.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
 $crumb="new";?>
<?php $crumb2="step2";?>

<!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><?php include('../includes/browser_title.php');?></title>
<link href="../admin.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrap">

<div id="contentWrap">
<?php include('../includes/admin_title.php');?>
<h2 align="center"><a href="../main_menu.php">Main Menu</a> > Create New Account</h2>
<?php include('../includes/steps.php');?>
<form enctype="multipart/form-data" action="<?php echo $editFormAction; ?>"  id="step2" name="step2" method="POST"  onSubmit="FX_processPop();">
<p>Upload account company logo. 
<div class="formRow">
<label><strong>Logo:</strong>
<input name="directory_name" type="text" id="directory_name" value="<?php echo $_GET['directory_name'];?>" />
<input class="input1" name="logo" type="file" id="logo" />
</label>

</div>

<div class="formRow"></div>

  <input class="submit" name="" type="submit" value="Continue to Step 3 >>" />
  <input type="hidden" name="MM_update" value="step2">
  <input type="hidden" name="FX_upload" value="Form1">
</form>

<p>dir=<?php echo $dir;?></p>
</div>


</body>

</html>

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
An image upload is one thing, and a DB update is a different thing.  You might consider posting two different questions, then integrating the answers.

Image uploads typically put the uploaded file into a "save" directory on the server file system.  A reasonable design pattern would add these file names (URLs) to the data base, associated with the client who uploaded them, the DATETIME of the upload, etc.

HTH, ~Ray
Most Valuable Expert 2011
Top Expert 2016
Commented:
Regarding this:

$HTTP_POST_VARS["FX_upload"]

Please see the note here about that array "[Deprecated]".  Time to upgrade to current technology!
http://php.net/manual/en/reserved.variables.post.php