Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Best way to update images using PHP / MySQL?

Community Beginner ,
Mar 12, 2011 Mar 12, 2011

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

TOPICS
Code , How to
31.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

New Here , Mar 14, 2011 Mar 14, 2011
Translate
Guest
Mar 12, 2011 Mar 12, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 12, 2011 Mar 12, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 13, 2011 Mar 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);
?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 13, 2011 Mar 13, 2011

help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2011 Mar 14, 2011
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 16, 2011 Mar 16, 2011
LATEST

Thank you adobemasterps

FINALLY
Thanks alot

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines