By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

update file on mysql using PHP

ddtpmyra
100+
P: 333
Im having trouble on updating the file inside mysql database using the php. Should I use <form> or im wrong on how I called the variable from previous page?

FORM 1 - that accepts the file pointer to upload.
Expand|Select|Wrap|Line Numbers
  1. # Get the ID
  2. $id = $_GET['id'];
  3. .....
  4. echo "<p>Replace CMR Document: <input type=\"file\" name=\"uploaded_file\" size=\"43\">";
  5. echo "<a href='replaceCMR.php?id=$id}'>Replace";
FORM2 - the php script that will update mysql.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. # Check if a file has been uploaded
  3. if(isset($_FILES['uploaded_file']))
  4. {
  5.     # Make sure the file was sent without errors
  6.     if($_FILES['uploaded_file']['error'] == 0)
  7.     {
  8.         # Connect to the database
  9.         .....
  10.  
  11.         # Gather all required data
  12.         $name = mysql_real_escape_string($_FILES['uploaded_file']['name'], $dbLink);
  13.         $mime = mysql_real_escape_string($_FILES['uploaded_file']['type'], $dbLink);
  14.         $size = $_FILES['uploaded_file']['size'];
  15.         $data = mysql_real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']), $dbLink);
  16.  
  17.  
  18.         # INSERT SQL query
  19.         $query = "
  20.            UPDATE fileStorage  
  21.             SET                     FileName= '{$name}',
  22.                                     FileMime ='{$mime}',
  23.                                     FileSize = {$size},
  24.                                     FileData = '{$data}'
  25.             WHERE                     FileID = {$id}";
  26.  
  27.  
  28.         # Execute the INSERT QUERY
  29.         $result = mysql_query($query, $dbLink);
  30.  
  31.         # Check if it was successfull
  32.         if($result)
  33.         {
  34.             echo "<b>Success! ";
  35.         }
  36.         else
  37.         {
  38.             echo "Error! Failed to updatethe file";
  39.             echo "<pre>". mysql_error($dbLink) ."</pre>";
  40.         }
  41.     }
  42.     else
  43.     {
  44.         echo "Error!
  45.                 An error accured while the file was being uploaded.
  46.                 Error code: ". $_FILES['uploaded_file']['error'];
  47.     }
  48.  
  49. }
  50. else
  51. {
  52.     echo "Error! A file was not sent!";
  53. }
  54.  
  55.  
  56. ?>
May 27 '09 #1
Share this Question
Share on Google+
4 Replies


ddtpmyra
100+
P: 333
to make a little bit simple.

form
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <form action="replaceCMR1.php" method="post"
  5. enctype="multipart/form-data">
  6. <label for="uploaded_file">Filename:</label>
  7. <input type="file" name="file" id="file" /> 
  8. <br />
  9. <input type="submit" name="submit" value="Submit" />
  10. </form>
  11.  
  12. </body>
  13. </html> 
uploader
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. # Check if a file has been uploaded
  3. if(isset($_FILES['uploaded_file']))
  4. {
  5.     # Make sure the file was sent without errors
  6.     if($_FILES['uploaded_file']['error'] == 0)
  7.     {
  8.         # Connect to the database
  9.        .....
  10.  
  11.         # Gather all required data
  12.         $name = mysql_real_escape_string($_FILES['uploaded_file']['name'], $dbLink);
  13.         $mime = mysql_real_escape_string($_FILES['uploaded_file']['type'], $dbLink);
  14.         $size = $_FILES['uploaded_file']['size'];
  15.         $data = mysql_real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']), $dbLink);
  16.  
  17.  
  18.  
  19.   # INSERT SQL query
  20.         $query = "
  21.             UPDATE fileStorage  
  22.             SET                     FileName= '{$name}',
  23.                                     FileMime ='{$mime}',
  24.                                     FileSize = {$size},
  25.                                     FileData = '{$data}'
  26.             WHERE                     FileID = {$id}";
  27.  
  28.  
  29.         # Execute the INSERT QUERY
  30.         $result = mysql_query($query, $dbLink);
  31.  
  32.         # Check if it was successfull
  33.         if($result)
  34.         {
  35.             echo "<b>Success! Your file was successfully added!<br>";
  36.             echo "Email alert was sent to reviewer's group</b>";
  37.  
  38.         }
  39.         else
  40.         {
  41.             echo "Error! Failed to insert the file";
  42.             echo "<pre>". mysql_error($dbLink) ."</pre>";
  43.         }
  44.     }
  45.     else
  46.     {
  47.         echo "Error!
  48.                 An error accured while the file was being uploaded.
  49.                 Error code: ". $_FILES['uploaded_file']['error'];
  50.     }
  51.  
  52. }
  53. else
  54. {
  55.     echo "Error! A file was not sent!";
  56. }
  57.  
  58. ?>        
May 28 '09 #2

ddtpmyra
100+
P: 333
Now i was able to update the records but it's blank it looks like
below code is not working. HELP!

Expand|Select|Wrap|Line Numbers
  1. # Gather all required data 
  2.         $name = mysql_real_escape_string($_FILES['uploaded_file']['name'], $dbLink); 
  3.         $mime = mysql_real_escape_string($_FILES['uploaded_file']['type'], $dbLink); 
  4.         $size = $_FILES['uploaded_file']['size']; 
  5.         $data = mysql_real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']), $dbLink);
May 28 '09 #3

ddtpmyra
100+
P: 333
I got it its the name on my first form should be name=uploaded_file

thanks!
May 28 '09 #4

P: 26
Hi, why does this error occur:

Error:
Error! Failed to insert the file

MySQL server has gone away
I don't know what to edit anymore..

Thanks..
Jun 19 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.