Adding a image to a existing file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • panhandle
    New Member
    • Feb 2009
    • 10

    Adding a image to a existing file

    Is there a way to add a image to existing data in a batabase without corrupting.
    The database is stuctured for images
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    do you mean Database Transactions?

    PS: Welcome at Bytes

    Comment

    • panhandle
      New Member
      • Feb 2009
      • 10

      #3
      I am not sure what it is called to be honest with you. But I will certainly take your word on it.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        It's really hard to figure out what it is exactly that you are trying to do based on such a vague description.

        What do you mean by "add a image to existing data"?
        Could you elaborate please?

        It would help to see the exact table structures you are using.

        Comment

        • panhandle
          New Member
          • Feb 2009
          • 10

          #5
          Hi Atli
          What I have is a database built off of you post "Uploading files into a MySQL database using PHP ". The STRUCTURE is basicly the same with a few more
          colm. added to it. What I am trying to do is add a image to records that do not have one or change the ones that are already stored in the data base. If I try to use the code that you wrote for adding it will duplicate the entry giving me one record with a image and one without.

          Comment

          • panhandle
            New Member
            • Feb 2009
            • 10

            #6
            here is the build of the table
            [code=mysql]
            CREATE TABLE tooltable (
            FileID Int Unsigned Not Null Auto_Increment,
            FileName VarChar(255) Not Null Default 'Untitled.txt',
            FileMime VarChar(50) Not Null Default 'text/plain',
            FileSize BigInt Unsigned Not Null Default 0,
            FileData MediumBlob Not Null,
            Created DateTime Not Null,
            ToolNumber Text(30) Not Null,
            ToolLocation VarChar(30) Not Null,
            Description VarChar(30) Not Null,
            Quantity VarChar(30) Not Null,
            Category VarChar(30) Not Null,
            Owner VarChar(30) Not Null,
            Notes VarChar(30) Not Null,
            PRIMARY KEY (FileID)
            )[/code]

            hope this helps
            Last edited by Atli; Feb 28 '09, 10:48 PM. Reason: Added [code] tags.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Ahh ok.

              You need to use the UPDATE statement.
              It is used to change the data in an existing row.

              For example, if you had this table:
              Code:
              +----+--------+--------+
              | ID | Value  | Value2 |
              +----+--------+--------+
              | 1  | First  | Nr. 1  |
              | 2  | Second | Nr. 2  |
              | 3  | Third  | Nr. 3  |
              +----+--------+--------+
              And you did:
              Code:
              UPDATE `table`
              SET    `Value` = 'Row 2',
                     `Value2` = 'Second'
              WHERE  `ID` = 2
              LIMIT 1
              You would end up with:
              Code:
              +----+--------+--------+
              | ID | Value  | Value2 |
              +----+--------+--------+
              | 1  | First  | Nr. 1  |
              | 2  | Row 2  | Second |
              | 3  | Third  | Nr. 3  |
              +----+--------+--------+
              Be careful to back up your data before you start using this statement tho.
              It can do serious damage if the WHERE and LIMIT clauses are used incorrectly.

              Comment

              • panhandle
                New Member
                • Feb 2009
                • 10

                #8
                Ok I think I may have it. Here is the code I came up with:
                Code:
                <?php require_once('file:///C|/Unnamed%20Site%202/Connections/database.php'); ?>
                <?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"] == "form1")) {
                  $updateSQL = sprintf("UPDATE tooltable SET FileName=%s, FileMime=%s, FileSize=%s, FileData=%s, ToolNumber=%s WHERE FileID=%s",
                                       GetSQLValueString($_POST['file'], "name"),
                					   GetSQLValueString($_POST['file'], "type"),
                					   GetSQLValueString($_POST['file'], "size"),
                					   GetSQLValueString($_POST['file'], "tmp_name"),
                                       GetSQLValueString($_POST['textfield'], "text"),
                                       GetSQLValueString($_POST['textfield2'], "int"));
                
                  mysql_select_db($database_database, $database);
                  $Result1 = mysql_query($updateSQL, $database) or die(mysql_error());
                }
                ?><!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>
                </head>
                
                <body>
                <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" id="form1">
                  <label>
                  <input type="file" name="file", type="file", size="file", data="file" />
                  </label>
                  <p>
                    <label>
                    <input type="text" name="textfield" />
                    </label>
                    Tool Number 
                  </p>
                  <p>
                    <label>
                    <input type="text" name="textfield2" />
                    </label> 
                    ID Number
                </p>
                  <p>
                    <label>
                    <input type="submit" name="Submit" value="Submit" />
                    </label>
                  </p>
                  <input type="hidden" name="MM_update" value="form1">
                </form>
                </body>
                </html>
                Think it will work?

                Comment

                • panhandle
                  New Member
                  • Feb 2009
                  • 10

                  #9
                  Well It didn't work! Here is what I got
                  Code:
                  You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' FileMime=, FileSize=, FileData=, ToolNumber='CH-47914-3' WHERE FileID=43' at line 1PHP Notice: Undefined index: file in C:\Inetpub\wwwroot\newtools\1.php on line 39 PHP Notice: Undefined index: file in C:\Inetpub\wwwroot\newtools\1.php on line 40 PHP Notice: Undefined index: file in C:\Inetpub\wwwroot\newtools\1.php on line 41 PHP Notice: Undefined index: file in C:\Inetpub\wwwroot\newtools\1.php on line 42
                  Any advice?

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Files are passed via the FILES array. $_FILES not $_POST.

                    Comment

                    • panhandle
                      New Member
                      • Feb 2009
                      • 10

                      #11
                      Something mor like this?
                      Code:
                                             GetSQLValueString($_FILES['file'], ("name")),
                      					   GetSQLValueString($_FILES['file'], ("type")),
                      					   GetSQLValueString($_FILES['file'], ("size")),
                      					   GetSQLValueString($_FILES['file'], ("tmp_name")),
                                             GetSQLValueString($_POST['textfield'], ("text")),
                                             GetSQLValueString($_POST['textfield2'], ("int")));

                      Comment

                      • panhandle
                        New Member
                        • Feb 2009
                        • 10

                        #12
                        Tried the $FILES array and now get this
                        Code:
                        You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' FileMime=, FileSize=, FileData=, ToolNumber='CH-47914-3' WHERE FileID=43' at line 1PHP Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\Inetpub\wwwroot\newtools\1.php on line 8 PHP Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\Inetpub\wwwroot\newtools\1.php on line 8 PHP Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\Inetpub\wwwroot\newtools\1.php on line 8 PHP Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\Inetpub\wwwroot\newtools\1.php on line 8

                        Comment

                        • hoopy
                          New Member
                          • Feb 2009
                          • 88

                          #13
                          Its $_FILES not $FILES

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #14
                            Your GetSQLValueStri ng function will not accept an array as it's first argument, even if you set the second argument to 'array'. $_FILES['file'] is an array. So, changing the first argument to reference the data of $_FILES['file'] will correct your errors.

                            Code:
                            GetSQLValueString($_FILES['file']['name'], "text"),
                            GetSQLValueString($_FILES['file']'type'], "text"),
                            GetSQLValueString($_FILES['file']['size'], "int"),
                            GetSQLValueString($_FILES['file']['tmp_name'], "text"),
                            GetSQLValueString($_POST['textfield'], ("text")),
                            GetSQLValueString($_POST['textfield2'], ("int")))

                            Comment

                            • panhandle
                              New Member
                              • Feb 2009
                              • 10

                              #15
                              Did the modifications-all went well except it does not display my image when downloaded. Is it because it is set to "text"
                              Code:
                              GetSQLValueString($_FILES['file']['tmp_name'], "text"),

                              Comment

                              Working...