473,654 Members | 3,184 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

update file on mysql using PHP

ddtpmyra
333 Contributor
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
4 6299
ddtpmyra
333 Contributor
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
333 Contributor
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
333 Contributor
I got it its the name on my first form should be name=uploaded_f ile

thanks!
May 28 '09 #4
roseple
26 New Member
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

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

Similar topics

2
3835
by: Reply via newsgroup | last post by:
Folks, When performing an update in mysql (using PHP), can I find out how many records were matched? mysql_affected_rows() won't work... and I have the following problem that I thought I could resolve with a simple function: Example: I have 50records - I want to update a selection of the recods - some,
0
1412
by: Jonas Meurer | last post by:
hello, i develop a project with a mysql interface. one mysql table holds all the images for my project. everything works quite well so far, except i'm not able to upload images into the database. the following function does the mysql UPDATE, it requires the image and the image ID as arguments. additionally it dumps the image to my filesystem ('/tmp/image.gif') -
5
3998
by: red85 | last post by:
hello i have mysql 4.1 with win2000 SP3, i know that it is only an alpha and i don't know if someone else has already posted this problem: when i execute this sql UPDATE tableX SET fieldX=valueX WHERE id IN (SELECT tX.id FROM tableX AS tX WHERE tX.fieldY=valueY AND filedZ=valueZ);
0
5899
by: Fraser Hanson | last post by:
Hello, I have a table which has a foreign key relationship with itself. I want and expect my updates to cascade (deletes definitely cascade as expected) but instead I just get error 1217: foriegn key error. I have written example code to use in reproducing the problem: # Create the table
0
2209
by: Jim C Nguyen | last post by:
I have a table with ~2.2 million rows. Sometimes when I do an update to one single row it will instead update ALL of the rows using the same update. This happens every one in about 500,000 updates to a single row. I am running mysql 4.0.14 in WinXP. I am using PHP to do the update. I am thinking something in the update itself is causing mysql to do this. I don't have the exact SQL statement that causes the error but I do have...
5
3831
by: jayson_13 | last post by:
Hi, I need to implement a counter and i face problem of locking so hope that u guys can help me. I try to do test like this : 1st connection SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update; (when i execute this statement and i guess that this will lock the
2
3893
by: Carlos Eduardo Peralta | last post by:
Hello: Can i update a MySQL database with just copy the files MYI MYD and FRM in the right dir? I know this work. The question is how MySQL manage the user that are usig that database in that moment. Can i close all the connection to that database? How can i tell MySQL deamon that don´t use that database any more? Thank you in advance.
4
9404
by: Stephen | last post by:
Hello People, Using MS Access 2003 VBA I get the error 3020 Update or CancelUpdate without AddNew or Edit when I run through the following code. Can anyone help suggest anything to try? Thanks. On Error GoTo delete_failed Dim RS_DEL As DAO.Recordset
1
7722
by: roger.rigsby | last post by:
Ok I know how to get the data I want from mysql but I need to actually do something with this data and then send it back to mySQL. Company heads love the spreadsheet view so this is serious business. I've done what I could with tables and css and javascript and whatnot for a makeshift datagrid on a webpage but since I'm using PHP and not asp.net I have no idea how to create a datagrid. So if anyone knows how to update data in Excel...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8708
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2716
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.