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.
-
# Get the ID
-
$id = $_GET['id'];
-
.....
-
echo "<p>Replace CMR Document: <input type=\"file\" name=\"uploaded_file\" size=\"43\">";
-
echo "<a href='replaceCMR.php?id=$id}'>Replace";
FORM2 - the php script that will update mysql.
- <?php
-
# Check if a file has been uploaded
-
if(isset($_FILES['uploaded_file']))
-
{
-
# Make sure the file was sent without errors
-
if($_FILES['uploaded_file']['error'] == 0)
-
{
-
# Connect to the database
-
.....
-
-
# Gather all required data
-
$name = mysql_real_escape_string($_FILES['uploaded_file']['name'], $dbLink);
-
$mime = mysql_real_escape_string($_FILES['uploaded_file']['type'], $dbLink);
-
$size = $_FILES['uploaded_file']['size'];
-
$data = mysql_real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']), $dbLink);
-
-
-
# INSERT SQL query
-
$query = "
-
UPDATE fileStorage
-
SET FileName= '{$name}',
-
FileMime ='{$mime}',
-
FileSize = {$size},
-
FileData = '{$data}'
-
WHERE FileID = {$id}";
-
-
-
# Execute the INSERT QUERY
-
$result = mysql_query($query, $dbLink);
-
-
# Check if it was successfull
-
if($result)
-
{
-
echo "<b>Success! ";
-
}
-
else
-
{
-
echo "Error! Failed to updatethe file";
-
echo "<pre>". mysql_error($dbLink) ."</pre>";
-
}
-
}
-
else
-
{
-
echo "Error!
-
An error accured while the file was being uploaded.
-
Error code: ". $_FILES['uploaded_file']['error'];
-
}
-
-
}
-
else
-
{
-
echo "Error! A file was not sent!";
-
}
-
-
-
?>