I've tried the following code, which executes without error, but yet I see nothing in phpMyAdmin to reflect the inserted data: -
<?php
-
// Require files for server-side includes for fpdf library, functions script, and database connection info.
-
require $_SERVER['DOCUMENT_ROOT']."/scripts/fpdf/fpdf.php";
-
require $_SERVER['DOCUMENT_ROOT']."/scripts/functions.php";
-
require $_SERVER['DOCUMENT_ROOT']."/scripts/connect.php";
-
//dbinfo is a function defined in functions.php which returns the database connection varriables
-
// for $host, $user, $pass, and $dbname.
-
dbinfo();
-
//Connect to database.
-
$con = mysqli_connect($host, $user, $pass, $dbname);
-
//check for database connection errors.
-
if ( mysqli_connect_errno()) {
-
die('Failed to connect to SQL:' . mysqli_connect_error());
-
}
-
//Set PHP vars to write to database.
-
$postusername = 'testusername';
-
$postip = getIP();
-
$postdate = '02/27/21';
-
$dbfilename = 'test.pdf';
-
$mime = 'pdf';
-
$pdffile = fopen($_SERVER['DOCUMENT_ROOT']."/pdf/file.pdf", "rb");
-
$postcomments = 'insert comments here';
-
//Prepare and Execute SQL statement.
-
$stmt = $con->prepare('INSERT INTO pdf (username, ip, date, filename, mime,file, comments) VALUES (?, ?, ?, ?, ?, ?, ?)');
-
$stmt->bind_param("sssssbs", $postusername, $postip, $postdate, $dbfilename, $mime, $pdffile, $comments);
-
$stmt->execute();
-
//Close Database connection.
-
$stmt->close();
-
$con->close();
-
// Give Feedback to User and Exit this script.
-
echo 'write to database sucessful';
-
exit;
-
?>
-
is this code correct, or should this be done differently?
Yes, that's how you would do it.
Assign it to the $var variable and use $var in the SQL statement. Just as you've done.
I've modified the example, thanks for pointing that out.
3 4054
Try using - $file = file_get_contents($_SERVER['DOCUMENT_ROOT']."/pdf/file.pdf")
Use the $file value in the insert SQL statment.
instead of - $pdffile = fopen($_SERVER['DOCUMENT_ROOT']."/pdf/file.pdf", "rb");
also if you go the fopen route, you are missing the fread & fclose - $filename = "/usr/local/something.pdf";
-
$handle = fopen($filename, "rb");
-
$contents = fread($handle, filesize($filename));
-
fclose($handle);
yes but then how do you pass the file_get_contents( ) to the SQL statement? or would file_get_contents automatically pass to SQL?
It would seem we would effectively need something along the premises of: -
$var = file_get_contents($_SERVER['DOCUMENT_ROOT']."/pdf/file.pdf")
-
.....
-
INSERT INTO table foo, foo, blob, foo VALUES ( $foo, $foo, $var, $foo ...);
-
Yes, that's how you would do it.
Assign it to the $var variable and use $var in the SQL statement. Just as you've done.
I've modified the example, thanks for pointing that out.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Norman Fritag |
last post by:
Hi there,
The Problem:
I can't write data to an access database in a webfolder of a windows Prof
2000 machine, whereas the same web application runs fine under Windows XP.
I looked into the...
|
by: Mamuninfo |
last post by:
Hello all,
I want to write java store procedure in db2 universal database
by jdbc.
After creating the java class, How can i load this class to the
database by the sqlj.install_jar() command in...
|
by: Mike John |
last post by:
I want to write to text file, the follow text " My Name Is
mike"
Now, I have used the following sysntax:
dim strFilePath as string = "C:\"
dim strFileName as string = "MyFile.txt"
...
|
by: Ken Yu |
last post by:
Hi,
I have one database table to store file (example : PDF, DOC, XLS
...... ) DateType (BLOB),
How can download one or more file (same time) from database by asp.net
webform ???
tks a lot...
|
by: Alan T |
last post by:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="DataBaseConnection"
connectionString="server=localhost;database=SalesDB;User
|
by: chrisf01 |
last post by:
I am trying to write to a text file and the database at the same time. I have a For statement that loops through all the repeated fields in my form and writes it to a text file. That part works 100%...
|
by: dmjpro |
last post by:
Can one explain me in details how to store a text file in a table.For example I have a text file named as aa.txt.I want to store it in a table.Or i can also create a table in order to store it.How...
|
by: ganeshshegde |
last post by:
Hi..
Can we store whole file in database? ie it should store in database in same format. .pdf should store in .pdf fomat and .doc should store in .doc fomat.
|
by: simonyong |
last post by:
Hello, anyone
I had search for few days with how to save file when user choose a file name from listbox and i will search the file from database and user can save it into their desktop
what I had...
|
by: hassal |
last post by:
Hello,
I'm developping a new joomla component, so i want to store pictures in database table in field type : blob. i'm wondery how to proceed to do this.
I'm using this to get file from the form...
|
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: 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: 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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |