Page 1 of 1

BLOB Updating

Posted: Thu Mar 18, 2004 8:53 am
by Gente21_OK
HELLO THERE!

I´m trying to update an image field from an update statement and a form. the code is:

SET TEXT PARAMETERS AND SCAPE SPECIAL CHARACTERS

Code: Select all

<?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;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
 
?> $updateSQL = sprintf("UPDATE pro SET cliente=%s, clave=%s, foto=%s, precio=%s WHERE id=%s",
                       GetSQLValueString($_POST['cliente'], "text"),
                       GetSQLValueString($_POST['clave'], "text"),
                       GetSQLValueString(file_get_contents($_FILES['foto']['tmp_name']), "text"),
                       GetSQLValueString($_POST['precio'], "double"),
                       GetSQLValueString($_POST['id'], "int"));

  mysql_select_db($database_se, $se);
  $Result1 = mysql_query($updateSQL, $se) or die(mysql_error());

  $updateGoTo = "test.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
DEFINE DATABASE AND SELECT TABLES

Code: Select all

$colname_RecordsetPro = "1";
if (isset($_GET['id'])) {
  $colname_RecordsetPro = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
}
mysql_select_db($database_se, $se);
$query_RecordsetPro = sprintf("SELECT * FROM pro WHERE id = %s", $colname_RecordsetPro);
$RecordsetPro = mysql_query($query_RecordsetPro, $se) or die(mysql_error());
$row_RecordsetPro = mysql_fetch_assoc($RecordsetPro);
$totalRows_RecordsetPro = mysql_num_rows($RecordsetPro);

AND THEN, THE FORM SO USERS CAN SEND:

Code: Select all

&lt;form action="&lt;?php echo $editFormAction; ?&gt;" method="post" enctype="multipart/form-data" name="form1"&gt;
        &lt;input type="hidden" name="MAX_FILE_SIZE" value="30000"&gt;
        

Id: &lt;?php echo $row_RecordsetPro&#1111;'id']; ?&gt; 


Cliente: &lt;input name="cliente" type="text" class="form" value="&lt;?php echo $row_RecordsetPro&#1111;'cliente']; ?&gt;" size="32"&gt;


Clave: &lt;input name="clave" type="text" class="form" value="&lt;?php echo $row_RecordsetPro&#1111;'clave']; ?&gt;" size="32"&gt;

Foto: &lt;input name="foto" type="file" class="form" size="32" maxlength="30000"&gt; 

Precio: &lt;input name="precio" type="text" class="form" value="&lt;?php echo $row_RecordsetPro&#1111;'precio']; ?&gt;" size="32"&gt;&lt;


&lt;input type="submit" class="form" value="Update record"&gt;


        &lt;input type="hidden" name="MM_update" value="form1"&gt;
        &lt;input type="hidden" name="id" value="&lt;?php echo $row_RecordsetPro&#1111;'id']; ?&gt;"&gt;  &lt;/form&gt;
EVERYTHING works fine :D , except that I get no image uploaded to the server, I get no errors, and I´m a little bit of stuck.
I appreciate your help

THANX!

Re: BLOB Updating

Posted: Thu Mar 18, 2004 11:37 am
by TheBentinel.com
Gente21_OK wrote:Foto: <input name="foto" type="file" class="form" size="32" maxlength="30000">
30000 is pretty small for a picture, are you sure that's big enough for the files you're testing?

Posted: Thu Mar 18, 2004 11:42 am
by Gente21_OK
You're right, I´m making it bigger, tough that was not the problem, I had an error in my SQL, it should've been like this

Code: Select all

$updateSQL = sprintf("UPDATE pro SET cliente=%s, clave=%s, foto='%s', precio=%s WHERE id=%s", 
                       GetSQLValueString($_POST['cliente'], "text"), 
                       GetSQLValueString($_POST['clave'], "text"), 
                       mysql_escape_string(file_get_contents($_FILES['foto']['tmp_name'])), 
                       GetSQLValueString($_POST['precio'], "double"), 
                       GetSQLValueString($_POST['id'], "int"));
Thanks to DRZOID@sitepoint.com

THANKS ANYWAY!"