Uploading images to a database - help needed
- gromituk2k
- Newbie


- Joined: Jun 29, 2009
- Posts: 14
- Loc: West Yorkshire, England
- Status: Offline
I am trying to upload images to a MySQL database, along with some other data.
The HTML page, containing the upload from looks like this:
And the PHP file for adding the data is like it:
I have had the form uploading all data except the image, but currently it is not uploading anything
Any ideas?
The HTML page, containing the upload from looks like this:
Code: [ Select ]
<!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>Untitled Document</title>
</head>
<body>
<form method="post" action="insert.php" enctype="multipart/form-data">
<p>Your name:<br>
<input type="text" name="form_visitor" size="40">
<br>Loved ones name:<br>
<input type="text" name="form_lovedone" size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload/store in database:<br>
<input type="file" name="form_data" size="40">
<br>Comments:<br>
<input type="text" name="form_comment" size="50">
</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="insert.php" enctype="multipart/form-data">
<p>Your name:<br>
<input type="text" name="form_visitor" size="40">
<br>Loved ones name:<br>
<input type="text" name="form_lovedone" size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload/store in database:<br>
<input type="file" name="form_data" size="40">
<br>Comments:<br>
<input type="text" name="form_comment" size="50">
</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
- <!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>Untitled Document</title>
- </head>
- <body>
- <form method="post" action="insert.php" enctype="multipart/form-data">
- <p>Your name:<br>
- <input type="text" name="form_visitor" size="40">
- <br>Loved ones name:<br>
- <input type="text" name="form_lovedone" size="40">
- <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
- <br>File to upload/store in database:<br>
- <input type="file" name="form_data" size="40">
- <br>Comments:<br>
- <input type="text" name="form_comment" size="50">
- </p>
- <input type="submit" name="submit" value="submit">
- </form>
- </body>
- </html>
And the PHP file for adding the data is like it:
Code: [ Select ]
<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
if(!$data = addslashes(@fread(@fopen($dir.'/'.$file, "r"), @filesize($dir.'/'.$file)))){
die('Cannot create $data');
}
@fclose($dir.'/'.$file);
$sql="INSERT INTO commentbook (visitor , lovedone, bin_data, filename, filesize, filetype, comment, id)
VALUES
('$_POST[form_visitor]','$_POST[form_lovedone]','$_POST[data]','$_POST[form_data_name]','$_POST[form_data_size]','$_POST[form_data_type]','$_POST[form_comment]','')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
if(!$data = addslashes(@fread(@fopen($dir.'/'.$file, "r"), @filesize($dir.'/'.$file)))){
die('Cannot create $data');
}
@fclose($dir.'/'.$file);
$sql="INSERT INTO commentbook (visitor , lovedone, bin_data, filename, filesize, filetype, comment, id)
VALUES
('$_POST[form_visitor]','$_POST[form_lovedone]','$_POST[data]','$_POST[form_data_name]','$_POST[form_data_size]','$_POST[form_data_type]','$_POST[form_comment]','')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
- <?php
- $con = mysql_connect("localhost","user","pass");
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- mysql_select_db("database_name", $con);
- if(!$data = addslashes(@fread(@fopen($dir.'/'.$file, "r"), @filesize($dir.'/'.$file)))){
- die('Cannot create $data');
- }
- @fclose($dir.'/'.$file);
- $sql="INSERT INTO commentbook (visitor , lovedone, bin_data, filename, filesize, filetype, comment, id)
- VALUES
- ('$_POST[form_visitor]','$_POST[form_lovedone]','$_POST[data]','$_POST[form_data_name]','$_POST[form_data_size]','$_POST[form_data_type]','$_POST[form_comment]','')";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
- echo "1 record added";
- mysql_close($con)
- ?>
I have had the form uploading all data except the image, but currently it is not uploading anything
Any ideas?
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
July 3rd, 2009, 1:54 am
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
- gromituk2k
- Newbie


- Joined: Jun 29, 2009
- Posts: 14
- Loc: West Yorkshire, England
- Status: Offline
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
have you done any work with PHP?
$query = "INSERT INTO ImageTable(imageData,imageType)
VALUES(\"" . addslashes($dataRead) . "\",
\"" . mysql_escape_string($fType) . "\");";
I had the slashes and dots in there, just seems that the manager I wrote initially took them out. So I have fixed the tutorial. Thanks for pointing it out
Code: [ Select ]
$query = "INSERT INTO ImageTable(imageData,imageType)
VALUES(\"" . addslashes($dataRead) . "\",
\"" . mysql_escape_string($fType) . "\");";
- $query = "INSERT INTO ImageTable(imageData,imageType)
- VALUES(\"" . addslashes($dataRead) . "\",
- \"" . mysql_escape_string($fType) . "\");";
I had the slashes and dots in there, just seems that the manager I wrote initially took them out. So I have fixed the tutorial. Thanks for pointing it out
Watch me grow
- gromituk2k
- Newbie


- Joined: Jun 29, 2009
- Posts: 14
- Loc: West Yorkshire, England
- Status: Offline
Hmm still not working, not even getting any errors now, just not uploading to the database.
This is the PHP that I am using:
<?php
$server = "localhost";
$username = "username";
$password = "password";
$dbHandle = mysql_connect($server,$username,$password)
or die("unable to conect to database server");
mysql_select_db("database_name")
or die ("Couldn't use Database");
$upload= (empty($_FILES['image'])) ? 0 : $_FILES['image'];
$fileName = $upload['tmp_name'];
$fType = $upload['type'];
$size = $upload['size'];
$timer = $size * 1024;
set_time_limit($timer);
$open = fopen($fileName,"r");
$data_read = fread($open, $size);
fclose($open);
$query = "INSERT INTO ImageTable(imageData,imageType)
VALUES(\"" . addslashes($dataRead) . "\",
\"" . mysql_escape_string($fType) . "\");";
?>
This is the PHP that I am using:
Code: [ Select ]
<?php
$server = "localhost";
$username = "username";
$password = "password";
$dbHandle = mysql_connect($server,$username,$password)
or die("unable to conect to database server");
mysql_select_db("database_name")
or die ("Couldn't use Database");
$upload= (empty($_FILES['image'])) ? 0 : $_FILES['image'];
$fileName = $upload['tmp_name'];
$fType = $upload['type'];
$size = $upload['size'];
$timer = $size * 1024;
set_time_limit($timer);
$open = fopen($fileName,"r");
$data_read = fread($open, $size);
fclose($open);
$query = "INSERT INTO ImageTable(imageData,imageType)
VALUES(\"" . addslashes($dataRead) . "\",
\"" . mysql_escape_string($fType) . "\");";
?>
- <?php
- $server = "localhost";
- $username = "username";
- $password = "password";
- $dbHandle = mysql_connect($server,$username,$password)
- or die("unable to conect to database server");
- mysql_select_db("database_name")
- or die ("Couldn't use Database");
- $upload= (empty($_FILES['image'])) ? 0 : $_FILES['image'];
- $fileName = $upload['tmp_name'];
- $fType = $upload['type'];
- $size = $upload['size'];
- $timer = $size * 1024;
- set_time_limit($timer);
- $open = fopen($fileName,"r");
- $data_read = fread($open, $size);
- fclose($open);
- $query = "INSERT INTO ImageTable(imageData,imageType)
- VALUES(\"" . addslashes($dataRead) . "\",
- \"" . mysql_escape_string($fType) . "\");";
- ?>
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
Can you echo out some data so we can debug this thing? Prehaps echo things like file size etc.
Also please make sure the they $POST index keys match the names you have given them in the form. I can see you have retained $POST['image'] where it should be $POST['form_data'].
You are going to have to adjust the script you found in the tutorial page to fit your requirements.
If you read the entire tutorial you would notice that the table structure is
and the form structure is
Also please make sure the they $POST index keys match the names you have given them in the form. I can see you have retained $POST['image'] where it should be $POST['form_data'].
You are going to have to adjust the script you found in the tutorial page to fit your requirements.
If you read the entire tutorial you would notice that the table structure is
Code: [ Select ]
[ImageTable]
id (bigint, primary key)
imageData (blob)
imageType (varchar(50))
id (bigint, primary key)
imageData (blob)
imageType (varchar(50))
- [ImageTable]
- id (bigint, primary key)
- imageData (blob)
- imageType (varchar(50))
and the form structure is
Code: [ Select ]
< form method="post" action="uploadImage.php"
enctype="multipart/form-data">
< input type="file" name="image" />
< input type="submit" value=" Upload " />
< /form>
enctype="multipart/form-data">
< input type="file" name="image" />
< input type="submit" value=" Upload " />
< /form>
- < form method="post" action="uploadImage.php"
- enctype="multipart/form-data">
- < input type="file" name="image" />
- < input type="submit" value=" Upload " />
- < /form>
Watch me grow
- gromituk2k
- Newbie


- Joined: Jun 29, 2009
- Posts: 14
- Loc: West Yorkshire, England
- Status: Offline
Hmm, I tried echoing the data and I am getting results with that, something like this;
size - 4388
temp name - /tmp/phpLl1740
type - image/gif
I have also changed something in the php file, as I noticed a couple of problems, so now using:
The databse is set out the same as yours, apart from the table name, which I have changed. So there seems to be a problem with the inserting into the table???
size - 4388
temp name - /tmp/phpLl1740
type - image/gif
I have also changed something in the php file, as I noticed a couple of problems, so now using:
Code: [ Select ]
<?php
$server = "localhost";
$username = "username";
$password = "pass";
$dbHandle = mysql_connect($server,$username,$password)
or die("Unable to conect to database server");
mysql_select_db("datbase-name")
or die ("Couldn't use Database");
$upload= (empty($_FILES['image'])) ? 0 : $_FILES['image'];
$fileName = $upload['tmp_name'];
$fType = $upload['type'];
$size = $upload['size'];
$timer = $size * 1024;
set_time_limit($timer);
$open = fopen($fileName,"r");
$data_read = fread($open, $size);
fclose($open);
$query = "INSERT INTO commentbook(imageData,imageType)
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\");";
echo "size - $size";
echo "</br>";
echo "temp name - $fileName";
echo "</br>";
echo "type - $fType";
?>
$server = "localhost";
$username = "username";
$password = "pass";
$dbHandle = mysql_connect($server,$username,$password)
or die("Unable to conect to database server");
mysql_select_db("datbase-name")
or die ("Couldn't use Database");
$upload= (empty($_FILES['image'])) ? 0 : $_FILES['image'];
$fileName = $upload['tmp_name'];
$fType = $upload['type'];
$size = $upload['size'];
$timer = $size * 1024;
set_time_limit($timer);
$open = fopen($fileName,"r");
$data_read = fread($open, $size);
fclose($open);
$query = "INSERT INTO commentbook(imageData,imageType)
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\");";
echo "size - $size";
echo "</br>";
echo "temp name - $fileName";
echo "</br>";
echo "type - $fType";
?>
- <?php
- $server = "localhost";
- $username = "username";
- $password = "pass";
- $dbHandle = mysql_connect($server,$username,$password)
- or die("Unable to conect to database server");
- mysql_select_db("datbase-name")
- or die ("Couldn't use Database");
- $upload= (empty($_FILES['image'])) ? 0 : $_FILES['image'];
- $fileName = $upload['tmp_name'];
- $fType = $upload['type'];
- $size = $upload['size'];
- $timer = $size * 1024;
- set_time_limit($timer);
- $open = fopen($fileName,"r");
- $data_read = fread($open, $size);
- fclose($open);
- $query = "INSERT INTO commentbook(imageData,imageType)
- VALUES(\"" . addslashes($data_read) . "\",
- \"" . mysql_escape_string($fType) . "\");";
- echo "size - $size";
- echo "</br>";
- echo "temp name - $fileName";
- echo "</br>";
- echo "type - $fType";
- ?>
The databse is set out the same as yours, apart from the table name, which I have changed. So there seems to be a problem with the inserting into the table???
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
HAHAHHAHAHA I just noticed now you not executing the query! You have to execute the query against the database to put it into the db
the $query variable is just a formatted sql statement.
if you check the tutorial page it links to a page that tells you how to execute queries against the mysql database
http://www.rabiddog.co.za/tutorial.php? ... qlDatabase
if you check the tutorial page it links to a page that tells you how to execute queries against the mysql database
Code: [ Select ]
// execute your query against the database here.
// Connecting to a MySql Database
// Connecting to a MySql Database
- // execute your query against the database here.
- // Connecting to a MySql Database
http://www.rabiddog.co.za/tutorial.php? ... qlDatabase
Watch me grow
- gromituk2k
- Newbie


- Joined: Jun 29, 2009
- Posts: 14
- Loc: West Yorkshire, England
- Status: Offline
Can you tell I am a little new to this
Thanks that seems to of worked fine.
I have now added a few other fields to the database and added these to the form. I have tried adding these to the INSERT query, the dtabase entries and the form entries, like this:
But no luck, the images are still getting added, but the other information is not.
Thanks that seems to of worked fine.
I have now added a few other fields to the database and added these to the form. I have tried adding these to the INSERT query, the dtabase entries and the form entries, like this:
Code: [ Select ]
$query = "INSERT INTO commentbook(imageData,imageType,visitor,lovedone,comment)
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\",
visitor,
lovedone,
comment);";
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\",
visitor,
lovedone,
comment);";
- $query = "INSERT INTO commentbook(imageData,imageType,visitor,lovedone,comment)
- VALUES(\"" . addslashes($data_read) . "\",
- \"" . mysql_escape_string($fType) . "\",
- visitor,
- lovedone,
- comment);";
But no luck, the images are still getting added, but the other information is not.
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
Code: [ Select ]
$query = "INSERT INTO commentbook(imageData,imageType,visitor,lovedone,comment)
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\",
visitor,
lovedone,
comment);";
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\",
visitor,
lovedone,
comment);";
- $query = "INSERT INTO commentbook(imageData,imageType,visitor,lovedone,comment)
- VALUES(\"" . addslashes($data_read) . "\",
- \"" . mysql_escape_string($fType) . "\",
- visitor,
- lovedone,
- comment);";
where are you assigning values to the visitor, lovedone and comment you want to insert? Prehaps I can help by explaining the structure of the query
Code: [ Select ]
$query = "INSERT INTO commentbook(imageData,imageType,visitor,lovedone,comment)
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\",
visitor,
lovedone,
comment);";
//commentbook(imageData,imageType,visitor,lovedone,comment)
//commentbook is the name of the table
//imageData, imageType, visitor, lovedone, comment are the names of
//the columns that data is going to be inserted into
//Anything between the VALUES brackets needs to be assigned a value
//so $data_read contains the byte array, $fType contains the file type value
// visitor should be declared something to the effect of $visitor etc
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\",
visitor,
lovedone,
comment);";
//commentbook(imageData,imageType,visitor,lovedone,comment)
//commentbook is the name of the table
//imageData, imageType, visitor, lovedone, comment are the names of
//the columns that data is going to be inserted into
//Anything between the VALUES brackets needs to be assigned a value
//so $data_read contains the byte array, $fType contains the file type value
// visitor should be declared something to the effect of $visitor etc
- $query = "INSERT INTO commentbook(imageData,imageType,visitor,lovedone,comment)
- VALUES(\"" . addslashes($data_read) . "\",
- \"" . mysql_escape_string($fType) . "\",
- visitor,
- lovedone,
- comment);";
- //commentbook(imageData,imageType,visitor,lovedone,comment)
- //commentbook is the name of the table
- //imageData, imageType, visitor, lovedone, comment are the names of
- //the columns that data is going to be inserted into
- //Anything between the VALUES brackets needs to be assigned a value
- //so $data_read contains the byte array, $fType contains the file type value
- // visitor should be declared something to the effect of $visitor etc
I can only help you if you post the form you are using so I can see the name values of your input
Just in closing on this post your query might be better off like this
Code: [ Select ]
$query = "INSERT INTO commentbook(imageData,imageType,visitor,lovedone,comment)
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\",
\"" . mysql_escape_string($visitor) . "\",
\"" . mysql_escape_string($lovedone) . "\",
\"" . mysql_escape_string($comment) . "\");";
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\",
\"" . mysql_escape_string($visitor) . "\",
\"" . mysql_escape_string($lovedone) . "\",
\"" . mysql_escape_string($comment) . "\");";
- $query = "INSERT INTO commentbook(imageData,imageType,visitor,lovedone,comment)
- VALUES(\"" . addslashes($data_read) . "\",
- \"" . mysql_escape_string($fType) . "\",
- \"" . mysql_escape_string($visitor) . "\",
- \"" . mysql_escape_string($lovedone) . "\",
- \"" . mysql_escape_string($comment) . "\");";
I could go into detail about creating a formatted string template then returning a formatted string as opposed to dynamically concating the entire string but I figured I would keep it simple or now
Watch me grow
- George L.
- Bronze Member


- Joined: Nov 05, 2007
- Posts: 2206
- Loc: Malaysia
- Status: Offline
gromituk2k wrote:
I am trying to upload images to a MySQL database, along with some other data.
The HTML page, containing the upload from looks like this:
And the PHP file for adding the data is like it:
I have had the form uploading all data except the image, but currently it is not uploading anything
Any ideas?
The HTML page, containing the upload from looks like this:
Code: [ Select ]
<!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>Untitled Document</title>
</head>
<body>
<form method="post" action="insert.php" enctype="multipart/form-data">
<p>Your name:<br>
<input type="text" name="form_visitor" size="40">
<br>Loved ones name:<br>
<input type="text" name="form_lovedone" size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload/store in database:<br>
<input type="file" name="form_data" size="40">
<br>Comments:<br>
<input type="text" name="form_comment" size="50">
</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="insert.php" enctype="multipart/form-data">
<p>Your name:<br>
<input type="text" name="form_visitor" size="40">
<br>Loved ones name:<br>
<input type="text" name="form_lovedone" size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload/store in database:<br>
<input type="file" name="form_data" size="40">
<br>Comments:<br>
<input type="text" name="form_comment" size="50">
</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
- <!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>Untitled Document</title>
- </head>
- <body>
- <form method="post" action="insert.php" enctype="multipart/form-data">
- <p>Your name:<br>
- <input type="text" name="form_visitor" size="40">
- <br>Loved ones name:<br>
- <input type="text" name="form_lovedone" size="40">
- <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
- <br>File to upload/store in database:<br>
- <input type="file" name="form_data" size="40">
- <br>Comments:<br>
- <input type="text" name="form_comment" size="50">
- </p>
- <input type="submit" name="submit" value="submit">
- </form>
- </body>
- </html>
And the PHP file for adding the data is like it:
Code: [ Select ]
<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
if(!$data = addslashes(@fread(@fopen($dir.'/'.$file, "r"), @filesize($dir.'/'.$file)))){
die('Cannot create $data');
}
@fclose($dir.'/'.$file);
$sql="INSERT INTO commentbook (visitor , lovedone, bin_data, filename, filesize, filetype, comment, id)
VALUES
('$_POST[form_visitor]','$_POST[form_lovedone]','$_POST[data]','$_POST[form_data_name]','$_POST[form_data_size]','$_POST[form_data_type]','$_POST[form_comment]','')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
if(!$data = addslashes(@fread(@fopen($dir.'/'.$file, "r"), @filesize($dir.'/'.$file)))){
die('Cannot create $data');
}
@fclose($dir.'/'.$file);
$sql="INSERT INTO commentbook (visitor , lovedone, bin_data, filename, filesize, filetype, comment, id)
VALUES
('$_POST[form_visitor]','$_POST[form_lovedone]','$_POST[data]','$_POST[form_data_name]','$_POST[form_data_size]','$_POST[form_data_type]','$_POST[form_comment]','')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
- <?php
- $con = mysql_connect("localhost","user","pass");
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- mysql_select_db("database_name", $con);
- if(!$data = addslashes(@fread(@fopen($dir.'/'.$file, "r"), @filesize($dir.'/'.$file)))){
- die('Cannot create $data');
- }
- @fclose($dir.'/'.$file);
- $sql="INSERT INTO commentbook (visitor , lovedone, bin_data, filename, filesize, filetype, comment, id)
- VALUES
- ('$_POST[form_visitor]','$_POST[form_lovedone]','$_POST[data]','$_POST[form_data_name]','$_POST[form_data_size]','$_POST[form_data_type]','$_POST[form_comment]','')";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
- echo "1 record added";
- mysql_close($con)
- ?>
I have had the form uploading all data except the image, but currently it is not uploading anything
Any ideas?
There is a simple solution to this. I am not sure if I am hearing right or you are doing a totally different thing.
Can you tell me, what exactly do you want to do with the image?
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
- George L.
- Bronze Member


- Joined: Nov 05, 2007
- Posts: 2206
- Loc: Malaysia
- Status: Offline
- gromituk2k
- Newbie


- Joined: Jun 29, 2009
- Posts: 14
- Loc: West Yorkshire, England
- Status: Offline
Seem to be getting the strangest of problems now. The extra fields that I have added, onlt the first character is getting added to the database.
The PHP I am using is:
<?php
$server = "localhost";
$username = "user";
$password = "pass";
$dbHandle = mysql_connect($server,$username,$password)
or die("Unable to conect to database server");
mysql_select_db("databse-name")
or die ("Couldn't use Database");
$upload= (empty($_FILES['image'])) ? 0 : $_FILES['image'];
$fileName = $upload['tmp_name'];
$fType = $upload['type'];
$size = $upload['size'];
$timer = $size * 1024;
set_time_limit($timer);
$visitor = $visitor['visitor'];
$lovedone = $lovedone['lovedone'];
$comment = $comment['comment'];
$open = fopen($fileName,"r");
$data_read = fread($open, $size);
fclose($open);
$query = "INSERT INTO commentbook(id,imageData,imageType,visitor,lovedone,comment)
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\",
\"" . mysql_escape_string($fType) . "\",
\"" . mysql_escape_string($visitor) . "\",
\"" . mysql_escape_string($lovedone) . "\",
\"" . mysql_escape_string($comment) . "\");";
mysql_query($query, $dbHandle);
if(mysql_affected_rows($dbHandle) > 0){
echo "Successfully inserted data </br>";
}else{
echo mysql_error();
}
echo "size - $size";
echo "</br>";
echo "temp name - $fileName";
echo "</br>";
echo "type - $fType";
?>
And the HTML form is:
<form method="post" action="insert.php"
enctype="multipart/form-data">
<p>
<input type="file" name="image" />
</p>
<p>
Name:
<input type="text" name="visitor" />
</p>
<p>
Loved one:
<input type="text" name="lovedone"/>
</p>
<p>
Comment:
<textarea name="comment" cols="45" rows="5"></textarea>
</p>
<p>
<input type="submit" value=" Upload " />
</p>
</form>
The extra entries in the table are two varchar(50) and one longtext.
The PHP I am using is:
Code: [ Select ]
<?php
$server = "localhost";
$username = "user";
$password = "pass";
$dbHandle = mysql_connect($server,$username,$password)
or die("Unable to conect to database server");
mysql_select_db("databse-name")
or die ("Couldn't use Database");
$upload= (empty($_FILES['image'])) ? 0 : $_FILES['image'];
$fileName = $upload['tmp_name'];
$fType = $upload['type'];
$size = $upload['size'];
$timer = $size * 1024;
set_time_limit($timer);
$visitor = $visitor['visitor'];
$lovedone = $lovedone['lovedone'];
$comment = $comment['comment'];
$open = fopen($fileName,"r");
$data_read = fread($open, $size);
fclose($open);
$query = "INSERT INTO commentbook(id,imageData,imageType,visitor,lovedone,comment)
VALUES(\"" . addslashes($data_read) . "\",
\"" . mysql_escape_string($fType) . "\",
\"" . mysql_escape_string($fType) . "\",
\"" . mysql_escape_string($visitor) . "\",
\"" . mysql_escape_string($lovedone) . "\",
\"" . mysql_escape_string($comment) . "\");";
mysql_query($query, $dbHandle);
if(mysql_affected_rows($dbHandle) > 0){
echo "Successfully inserted data </br>";
}else{
echo mysql_error();
}
echo "size - $size";
echo "</br>";
echo "temp name - $fileName";
echo "</br>";
echo "type - $fType";
?>
- <?php
- $server = "localhost";
- $username = "user";
- $password = "pass";
- $dbHandle = mysql_connect($server,$username,$password)
- or die("Unable to conect to database server");
- mysql_select_db("databse-name")
- or die ("Couldn't use Database");
- $upload= (empty($_FILES['image'])) ? 0 : $_FILES['image'];
- $fileName = $upload['tmp_name'];
- $fType = $upload['type'];
- $size = $upload['size'];
- $timer = $size * 1024;
- set_time_limit($timer);
- $visitor = $visitor['visitor'];
- $lovedone = $lovedone['lovedone'];
- $comment = $comment['comment'];
- $open = fopen($fileName,"r");
- $data_read = fread($open, $size);
- fclose($open);
- $query = "INSERT INTO commentbook(id,imageData,imageType,visitor,lovedone,comment)
- VALUES(\"" . addslashes($data_read) . "\",
- \"" . mysql_escape_string($fType) . "\",
- \"" . mysql_escape_string($fType) . "\",
- \"" . mysql_escape_string($visitor) . "\",
- \"" . mysql_escape_string($lovedone) . "\",
- \"" . mysql_escape_string($comment) . "\");";
- mysql_query($query, $dbHandle);
- if(mysql_affected_rows($dbHandle) > 0){
- echo "Successfully inserted data </br>";
- }else{
- echo mysql_error();
- }
- echo "size - $size";
- echo "</br>";
- echo "temp name - $fileName";
- echo "</br>";
- echo "type - $fType";
- ?>
And the HTML form is:
Code: [ Select ]
<form method="post" action="insert.php"
enctype="multipart/form-data">
<p>
<input type="file" name="image" />
</p>
<p>
Name:
<input type="text" name="visitor" />
</p>
<p>
Loved one:
<input type="text" name="lovedone"/>
</p>
<p>
Comment:
<textarea name="comment" cols="45" rows="5"></textarea>
</p>
<p>
<input type="submit" value=" Upload " />
</p>
</form>
- <form method="post" action="insert.php"
- enctype="multipart/form-data">
- <p>
- <input type="file" name="image" />
- </p>
- <p>
- Name:
- <input type="text" name="visitor" />
- </p>
- <p>
- Loved one:
- <input type="text" name="lovedone"/>
- </p>
- <p>
- Comment:
- <textarea name="comment" cols="45" rows="5"></textarea>
- </p>
- <p>
- <input type="submit" value=" Upload " />
- </p>
- </form>
The extra entries in the table are two varchar(50) and one longtext.
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
July 6th, 2009, 6:23 am
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 33 posts
- Users browsing this forum: No registered users and 119 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
