Skip to main content
Known Participant
March 12, 2011
Answered

Best way to update images using PHP / MySQL?

  • March 12, 2011
  • 4 replies
  • 32054 views

HI
I want to update images using PHP/MySQL, so users can update their images and maybe some other files but mainly images.
thanks

This topic has been closed for replies.
Correct answer adobemasterps

hi you can try this "how to update an image in mysql"


4 replies

adobemasterpsCorrect answer
Participant
March 15, 2011
IFemanAuthor
Known Participant
March 16, 2011

Thank you adobemasterps

FINALLY
Thanks alot

IFemanAuthor
Known Participant
March 14, 2011

help!

IFemanAuthor
Known Participant
March 13, 2011

How messed up is this code?!

<?php
//connect to database
$con = mysql_connect("testhost","testuser","pass");

// file properies
$file = $_FILES['image']['tmp_name'];

if (!isset($file))

echo "please select an image";

else {
    $id = $_REQUEST['id_display'];
   
    $image = ($_FILES['image']['tmp_name']) ? file_get_contents ($_FILES['image']['tmp_name']) : '';
       
   
    $image_name = ($_FILES['image']['name']) ? addslashes($_FILES['image']['name']): '';
   
   
    $image_size = ($_FILES['image']['tmp_name']) ? getimagesize($_FILES['image']['tmp_name']): '';
   
    if ($image_size == FALSE)
    echo "You have not selected an image.";
   
    else {
        mysql_select_db("test_display", $con);
        mysql_query("UPDATE testtable SET image = '$image', imagename = '$image_name' WHERE id_display='$id'");
         }
    }
?>

And the upload page....

<?php require_once('Connections/testconn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $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;
}
}

$colname_Recordset1 = "-1";
if (isset($_GET['id_display'])) {
  $colname_Recordset1 = $_GET['id_display'];
}
mysql_select_db($database_testconn, $testconn);
$query_Recordset1 = sprintf("SELECT * FROM testtable WHERE id_display = %s ORDER BY id_display DESC", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $testconn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

/*Update code */
<?php
//connect to database
$con = mysql_connect("testhost","testuser","pass");

// file properies
$file = $_FILES['image']['tmp_name'];

if (!isset($file))

echo "please select an image";

else {
     $id = $_REQUEST['id_display'];
    
    $image = ($_FILES['image']['tmp_name']) ? file_get_contents ($_FILES['image']['tmp_name']) : '';
         
    
     $image_name = ($_FILES['image']['name']) ? addslashes($_FILES['image']['name']): '';
    
    
     $image_size = ($_FILES['image']['tmp_name']) ? getimagesize($_FILES['image']['tmp_name']): '';
    
     if ($image_size == FALSE)
     echo "You have not selected an image.";
    
     else {
          mysql_select_db("test_display", $con);
          mysql_query("UPDATE testtable SET image = '$image', imagename = '$image_name' WHERE id_display='$id'");
           }
     }
?> /*End update code */

<!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=utf-8" />
<title>upload an image</title>
</head>

<body>

<form action="image_upload.php" method="post" enctype="multipart/form-data">
  <p>
    <input name="name" type="text" id="name" value="<?php echo $row_Recordset1['name']; ?>" />
    <input name="hiddenField" type="hidden" id="hiddenField" value="<?php echo $row_Recordset1['id_display']; ?>" />
  </p>
  <p>
    <input name="image" type="file" id="image" />
    <input name="upload" type="submit" id="upload" value="upload" />
  </p>
</form>

</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

March 13, 2011

Restrict access to page, create a form on your page, link form to php script that uploads file and updates database table where id in database table = session variable for logged in user.

IFemanAuthor
Known Participant
March 13, 2011

link form to php script that uploads file and updates database table

Thanks for the help, can you please give example of the upload script?