Storing Data in Mysql Database – PHP



Hello, I’m working on a website that will need to store data like pdf documents and doc documents in mqsql database without storing them in a file Like the code below. I need help with the php code and mysql code to use.

// 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
$dbLink = new mysqli('', 'user', 'pwd', 'myTable');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
// Gather all required data
$name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
$mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
$data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
$size = intval($_FILES['uploaded_file']['size']);
// Create the SQL query
$query = "
INSERT INTO `file` (
`name`, `mime`, `size`, `data`, `created`
'{$name}', '{$mime}', {$size}, '{$data}', NOW()
// Execute the query
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
// Close the mysql connection
else {
echo 'Error! A file was not sent!';
// Echo a link back to the main page
echo '<p>Click <a href="index.html">here</a> to go back</p>';


Could you give more details on:
What is going wrong?


Well i’m a newbie to php. I’m Teaching myself for a school project. I needed to know if there was any other code i could use apart from the code given above. I tried it some weeks ago but was getting errors. I was following the comments made on the errors for the first code that was posted but still ended up with errors.


"but was getting errors"
I you have error, please add them to this request.

Or should we try the code, and find other errors?

What errors did you have, and which of them where you not able to solve? (and how did you try to solve them)

"but still ended up with errors."
Yes , but which errors? 😉


This is how far i have gone-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Admin Panel</title>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
# Make sure an ID was passed
# Get the ID
$id = $_GET['id'];
# Make sure the ID is in fact a valid ID
if(!is_numeric($id) || ($id <= 0)) {
die("The ID is invalid!");
# Connect to the database
$dbLink = mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("contents", $dbLink) or die(mysql_error());
# Fetch the file information
$query = "
SELECT FileMime, FileName, FileSize, FileData
FROM filestorage
WHERE FileID = {$id}";
$result = @mysql_query($query,$dbLink)
or die("Error! Query failed: <pre>". mysqli_error($dbLink) ."</pre>");
# Make sure the result is valid
if(mysql_num_rows($result) == 1)
# Get the row
$row = mysql_fetch_assoc($result);
# Print headers
# Print data
echo $row['FileData'];
echo "Error! No image exists with that ID.";
# Free the mysqli resources
echo "Error! No ID was passed.";

The error i’m getting is "ERROR ! No Id passed" no matter how much i try to change the code it gives me that error. I also changed the upload form as well.


Your form (line#9) does do a ‘POST’,
line#35 is looking at ‘$_GET’ toe get the value for ID

Maybe you should Google something about the difference between POST and GET
(hint: look here)


Thanks for the link to the site it was very helpful.