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. - <?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
-
$dbLink = new mysqli('127.0.0.1', '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`
-
)
-
VALUES (
-
'{$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
-
$dbLink->close();
-
}
-
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>';
-
?>
6 2153 Luuk 1,047
Expert 1GB
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.
Luuk 1,047
Expert 1GB
"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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
-
<title>Admin Panel</title>
-
</head>
-
<body>
-
<form method="post" enctype="multipart/form-data">
-
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
-
<tr>
-
<td width="246">
-
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
-
<input name="userfile" type="file" id="userfile">
-
</td>
-
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
-
</tr>
-
</table>
-
</form>
-
-
-
-
-
<body>
-
-
</body>
-
</html>
-
-
-
-
-
-
<?php
-
# Make sure an ID was passed
-
if(isset($_GET['id']))
-
{
-
# 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
-
header("Content-Type:".$row['FileMime']);
-
header("Content-Length:".$row['FileSize']);
-
header("Content-Disposition:attachment;filename=".$row['FileName']);
-
-
# Print data
-
echo $row['FileData'];
-
}
-
else
-
{
-
echo "Error! No image exists with that ID.";
-
}
-
-
# Free the mysqli resources
-
@mysql_free_result($result);
-
@mysql_close($dbLink);
-
-
}
-
else
-
{
-
echo "Error! No ID was passed.";
-
}
-
?>
-
-
-
</body>
-
</html>
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.
Luuk 1,047
Expert 1GB
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: siliconmike |
last post by:
Unfortunately when I was installing FreeBSD, its handbook told me that
/var should be around 256 MB or so.
Now, since /var is near to the edge of the hdd, it is fast and now I
can't store my 2...
|
by: Dave |
last post by:
I have a system that basically stores a database within a database (I'm
sure lots have you have done this before in some form or another).
At the end of the day, I'm storing the actual data...
|
by: donpro |
last post by:
Hi,
I have a varchar field in a MySQL database that contains a line of text
like so:
"This is a line if text"
The double quotes are included in the database field.
I cannot seem to...
|
by: tejas |
last post by:
Hi everyone.i am working on VB6.0
i want to make a software using VB 6.0 as front end which retrives selected columns from as excel file and puts that in the database table.
the excel file should...
|
by: akashkhasgiwala |
last post by:
i am developing i web application in which every user will have an Xml File containing mostly text and date/time stamps
this application will use MySql for storing data
so pl tell me if i can STORE...
|
by: eholz1 |
last post by:
Hello Group,
Perhaps you can help me. I have a mysql db, that holds images.
Images are encoded using base64_decode/encode, etc. Image data seems
fine.
I have a view.php page that is supposed...
|
by: dakshayini |
last post by:
Hi all,
i need to store data having subscript and super script like chemical equations symbols...values in to mysql database and fetch it and display.
for that i need to convert that text into...
|
by: secutos |
last post by:
I have the option of storing data online as text files, or storing data online in a MySQL database. If the information is stored as a text file, all I have to do is a HttpWebRequest to the text file...
|
by: ravi951 |
last post by:
hi all,
i want to store an image in MYSQL database.please tell me whether the below sql syntax i have written is correct or not.
CREATE TABLE IF NOT EXISTS `products`(
`serial` int(11)...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
| |