updateing a picture in mysql using php

i just wanted to find out how do i go about updateing a picture in mysql database, using php, i have been tryiing to do this but in have no managed to get it working so far, i have two files for this, the first is to echo all the picture details into a form with text fields, the second is what will update the picture details. below are the two scripts that i have been working on. thanks

<?php
foreach($HTTP_POST_VARS as $varname => $value)
{
        $formVars[$varname]=$value;
}
require_once("config.php");


if (!$link)
{
      echo 'Error: Could not connect to database.';
      exit;

}
else
{
       echo 'Connected OK to db.';
}


mysql_select_db ('m215519');

$query="SELECT * FROM product WHERE itemId = \"".$formVars["itemId"]."\"";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$formVars = array();
$formVars["itemName"]=$row["itemName"];
$formVars["itemTitle"]=$row["itemTitle"];
$formVars["date"]=$row["date"];
$formVars["gen"]=$row["gen"];
$formVars["type1"]=$row["type1"];
$formVars["itemDesc"]=$row["itemDesc"];
$formVars["itemPrice"]=$row["itemPrice"];
$formVars["itemId"]=$row["itemId"];
mysql_close($link);
?>




<html>
<head>
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    border: 1px solid #000000;
}
-->
</style>
</head>

<body>
<form action="postupdate2.php" method="post" enctype="multipart/form-data" name="uploadform">
  <table width="350" border="0" align="center" cellpadding="1" cellspacing="1" class="box">
    <tr>
      <td>Artist</td>
      <td><input name="itemName" type="text" id="itemName" value="<? echo $formVars["itemName"]; ?>"></td>
    </tr>
    <tr>
      <td>title</td>
      <td><input name="itemTitle" type="text" id="itemTitle" value="<? echo $formVars["itemTitle"]; ?>">      </td>
    </tr>
    <tr>
      <td>Release date </td>
      <td><input name="date" type="text" id="date" value="<? echo $formVars["date"]; ?>"></td>
    </tr>
    <tr>
      <td>genre</td>
      <td><input name="gen" type="text" id="gen" value="<? echo $formVars["gen"]; ?>"></td>
    </tr>
    <tr>
      <td>type</td>
      <td><input name="type1" type="text" id="type1" value="<? echo $formVars["type1"]; ?>"></td>
    </tr>
    <tr>
      <td>description</td>
      <td><textarea name="itemDesc" id="itemDesc"><? echo $formVars["itemDesc"]; ?></textarea></td>
    </tr>
    <tr>
      <td>price</td>
      <td><input name="itemPrice" type="text" id="itemPrice" value="<? echo $formVars["itemPrice"]; ?>"></td>
    </tr>
    <tr>
      <td>name</td>
      <td><input name="name" type="text" id="name" value="<? echo $formVars["name"]; ?>"></td>
    </tr>
    <tr>
      <td>type</td>
      <td><input name="type" type="text" id="type" value="<? echo $formVars["type"]; ?>"></td>
    </tr>
    <tr>
      <td>size</td>
      <td><input name="size" type="text" id="size" value="<? echo $formVars["size"]; ?>"></td>
    </tr>
    <tr>
      <td>content</td>
      <td><input name="content" type="text" id="content" value="<? echo $formVars["content"]; ?>"></td>
    </tr>
    <tr>
      <td>upload picture </td>
      <td><input name="userfile" type="file" class="box" id="userfile"></td>
    </tr>
    <tr>
      <td width="97"><input type="hidden" name="MAX_FILE_SIZE" value="2000000">
      </td>
      <td width="244"><input name="upload" type="submit" class="box" id="upload" value="Update"></td>
    </tr>
  </table>
</form>
<p>
  <?
  //check if the upload button has been pressed
if(isset($_POST['upload']))
{
            
        $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];
         
        $fp = fopen($tmpName, 'r');
        $content = fread($fp, $fileSize);
        $content = addslashes($content);
        fclose($fp);
         
        if(!get_magic_quotes_gpc())
        {
            $fileName = addslashes($fileName);
        }
       
      //get the connection to the database
      //output message if this connection failed
      
            //insert the pucture with its data into the database
            //and output error message if query failed
       
            //$query = "INSERT INTO items (itemName, itemTitle, date, gen, type1, itemDesc, itemPrice, name, size, type, content ) ".
                 //"VALUES ('$itemName', '$itemTitle', '$date', '$gen', '$type1', '$itemDesc', '$itemPrice', '$fileName', '$fileSize', '$fileType', '$content')";

        //mysql_query($query) or die('Error, query failed');                    
        //include 'library/closedb.php';
         
             //send a message to show that the file has been uploaded
        echo "<br>File $fileName uploaded<br>";
}        
?>
</p>
<p>&nbsp;</p>
</body>
</html>


second file

<html>
<head>
<title> Update</title>
<link rel="stylesheet" href="emx_nav_left.css" type="text/css">

</head>
<body>
Update:
<?php
$debugging = array(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE) ;
$debugging[0] = FALSE ;  #      Display query
foreach($HTTP_POST_VARS as $varname => $value)
{
    $formVars[$varname]=$value;
#    echo "$value<br>" ;
}


mysql_select_db("m215519");
$query = "UPDATE items set " ;
$query .= " itemName= '" . $formVars["itemName"] ."', " ;
$query .= " itemTitle= '" . $formVars["itemTitle"] . "', " ;
$query .= " date= '" . $formVars["date"] . "', " ;
$query .= "gen= '" . $formVars["gen"] . "', " ;
$query .= "type1= '" . $formVars["tpye1"] . "', " ;
$query .= "itemDesc= '" . $formVars["itemDesc"] . "', " ;
$query .= "itemPrice= '" . $formVars["itemPrice"] . "', " ;
$query .= "type= '" . $formVars["type"] . "', " ;
$query .= "size= '" . $formVars["size"] . "', " ;
$query .= "content= '" . $formVars["content"] . "' " ;
$query .= " WHERE itemId = '" . $formVars["itemId"] . "'" ;

if ($debugging[0])
{
      echo "<br><br>The MySQL query is:<br>$query<br>\n" ;
}
$result = mysql_query($query) ;

echo "Record updated:<br>The new record is:<br>\n" ;

$showquery = "select itemName, itemTitle, date " ;
$showquery .= " from items " ;
$showquery .= " WHERE itemId = '" . $formVars["itemId"] . "'" ;

$showresult = mysql_query($showquery) ;
$showrow = mysql_fetch_row($showresult) ;
echo "Title = $showrow[0]<br>\n" ;
echo "Artist = $showrow[1]<br>\n" ;
echo "Date = $showrow[2]<br>\n" ;


echo "<br><br><a href=\"update.htm\">click here</a> to update another record<br>";

mysql_close();



jayjjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vorbeCommented:
The scripts are a bit messed. Not sure when one starts and ohter ends, and their names - to see what form calls which script.

Anyway, first thing I noticed is that you display the contnet field - which is the picture binary data - no need to do that. In the update script you should recreate the content field from the uploaded file again, like in the insert script.

if (isset($_FILES) && isset($_FILES['userfile'])) {
        $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];
         
        $fp = fopen($tmpName, 'r');
        $content = fread($fp, $fileSize);
        $content = addslashes($content);
        fclose($fp);

        $query .= "content= '" . $formVars["content"] . "' " ;
}

Otherwise -DO NOT- include content into the updating query (i.e. if the user didn't upload a file).

P.S. Addslashes() should probably work for inserting binary data, but you better make sure the field type is blob. Maybe you should use mysql_real_escape_string().

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jayjjAuthor Commented:
sorry about that, the first code is the one used to echo back the data found and is called postupdate1.php the second file is the one which actually update the data after is has been sent and is called postupdate2.php this starts after where i said second file. I will use you suggestion  and see what happends, thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.