473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Storing Data in Mysql Database

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.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Check if a file has been uploaded
  3. if(isset($_FILES['uploaded_file'])) {
  4.     // Make sure the file was sent without errors
  5.     if($_FILES['uploaded_file']['error'] == 0) {
  6.         // Connect to the database
  7.         $dbLink = new mysqli('127.0.0.1', 'user', 'pwd', 'myTable');
  8.         if(mysqli_connect_errno()) {
  9.             die("MySQL connection failed: ". mysqli_connect_error());
  10.         }
  11.  
  12.         // Gather all required data
  13.         $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
  14.         $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
  15.         $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
  16.         $size = intval($_FILES['uploaded_file']['size']);
  17.  
  18.         // Create the SQL query
  19.         $query = "
  20.             INSERT INTO `file` (
  21.                 `name`, `mime`, `size`, `data`, `created`
  22.             )
  23.             VALUES (
  24.                 '{$name}', '{$mime}', {$size}, '{$data}', NOW()
  25.             )";
  26.  
  27.         // Execute the query
  28.         $result = $dbLink->query($query);
  29.  
  30.         // Check if it was successfull
  31.         if($result) {
  32.             echo 'Success! Your file was successfully added!';
  33.         }
  34.         else {
  35.             echo 'Error! Failed to insert the file'
  36.                . "<pre>{$dbLink->error}</pre>";
  37.         }
  38.     }
  39.     else {
  40.         echo 'An error accured while the file was being uploaded. '
  41.            . 'Error code: '. intval($_FILES['uploaded_file']['error']);
  42.     }
  43.  
  44.     // Close the mysql connection
  45.     $dbLink->close();
  46. }
  47. else {
  48.     echo 'Error! A file was not sent!';
  49. }
  50.  
  51. // Echo a link back to the main page
  52. echo '<p>Click <a href="index.html">here</a> to go back</p>';
  53. ?>
May 26 '14 #1
6 2153
Luuk
1,047 Expert 1GB
Could you give more details on:
What is going wrong?
May 27 '14 #2
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.
May 27 '14 #3
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? ;)
May 27 '14 #4
This is how far i have gone-


Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.  
  6.       <title>Admin Panel</title>
  7.        </head>
  8.        <body>
  9.        <form method="post" enctype="multipart/form-data">
  10. <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
  11. <tr>
  12. <td width="246">
  13. <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
  14. <input name="userfile" type="file" id="userfile">
  15. </td>
  16. <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
  17. </tr>
  18. </table>
  19. </form>
  20.  
  21.  
  22.  
  23.  
  24.        <body>
  25.  
  26.           </body>
  27.        </html>
  28.  
  29.  
  30.  
  31.  
  32.  
  33.     <?php
  34.     # Make sure an ID was passed
  35.     if(isset($_GET['id']))
  36.     {
  37.     # Get the ID
  38.     $id = $_GET['id'];
  39.  
  40.     # Make sure the ID is in fact a valid ID
  41.     if(!is_numeric($id) || ($id <= 0)) {
  42.     die("The ID is invalid!");
  43.     }
  44.  
  45.     # Connect to the database
  46.     $dbLink = mysql_connect("localhost", "root", "") or die (mysql_error());
  47.     mysql_select_db("contents", $dbLink) or die(mysql_error());
  48.  
  49.     # Fetch the file information
  50.     $query = "
  51.     SELECT FileMime, FileName, FileSize, FileData
  52.     FROM filestorage
  53.     WHERE FileID = {$id}";
  54.  
  55.     $result = @mysql_query($query,$dbLink)
  56.     or die("Error! Query failed: <pre>". mysqli_error($dbLink) ."</pre>");
  57.  
  58.     # Make sure the result is valid
  59.     if(mysql_num_rows($result) == 1)
  60.     {
  61.     # Get the row
  62.     $row = mysql_fetch_assoc($result);
  63.  
  64.     # Print headers
  65.     header("Content-Type:".$row['FileMime']);
  66.     header("Content-Length:".$row['FileSize']);
  67.     header("Content-Disposition:attachment;filename=".$row['FileName']);
  68.  
  69.     # Print data
  70.     echo $row['FileData'];
  71.     }
  72.     else
  73.     {
  74.     echo "Error! No image exists with that ID.";
  75.     }
  76.  
  77.     # Free the mysqli resources
  78.     @mysql_free_result($result);
  79.     @mysql_close($dbLink);
  80.  
  81.     }
  82.     else
  83.     {
  84.     echo "Error! No ID was passed.";
  85.     }
  86.     ?>
  87.  
  88.  
  89. </body>
  90. </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.
May 27 '14 #5
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)
May 28 '14 #6
Thanks for the link to the site it was very helpful.
May 29 '14 #7

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

Similar topics

0
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...
7
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...
2
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...
0
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...
1
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...
7
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...
3
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...
2
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...
1
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)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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,...

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.