Jump to content
arden

how to upload an image to mysql?

Recommended Posts

Echo the image data into a query that INSERTs it into a field of data type BLOB.

Share this post


Link to post
Share on other sites
Echo the image data into a query that INSERTs it into a field of data type BLOB.
can u give me a sample script?..

Share this post


Link to post
Share on other sites

don't double post, we have already seen your other post and the question has been asked in other one of your threads too. I would be a bit more patient when requesting help.

Share this post


Link to post
Share on other sites
don't double post, we have already seen your other post and the question has been asked in other one of your threads too. I would be a bit more patient when requesting help.
oops.. sorry..

Share this post


Link to post
Share on other sites

of course, as JSG put it so eloquently in a recent thread...

if there was only some sort of world-wide network of computers where you could search for any topic imaginable.... (highly paraphrased, of course)
:)So, what have you tried so far, or what do you have to show us that lets us know you've got something going...?

Share this post


Link to post
Share on other sites

creat database kid

create database kid;use kid;create table image_kid(imageId int auto_increment not null,imageTitle varchar(50),imageData longblob,imageType varchar(50),primary key(imageId),unique id(imageId));

getfiles.php

<html><head><title> Upload a File </title></head><body bgcolor="#FFFFFF"><form enctype="multipart/form-data" name="frmUploadFile" action="grabfile.php" method="post"><table border="0" cellpadding="0" cellspacing="0" bordercolor="#111111" width="100%"><tr><td width="100%" bgcolor="#FF9900" height="22" colspan="2"><p style="margin-left: 10"><b><font face="Verdana" size="2" color="#FFFFFF">Upload a File</font></b></td></tr><tr><td width="100%" bgcolor="#FFE3BB" colspan="2"><p style="margin-left: 10; margin-right: 10"><font face="Verdana" size="2"><br>Please select a file from your local computer to upload to our web serverfor saving in our database. This file can be of any type you like. Once youhave chosen a file, please click on the "Upload this file" button below.  <br> </font></td></tr><tr><td width="15%" bgcolor="#FFE3BB"><p style="margin-left: 10"><font face="Verdana" size="2">File Description:</font></td><td width="85%" bgcolor="#FFE3BB"><input type="text" name="strDesc" size="20" maxlength="50"></td></tr><tr><td width="15%" bgcolor="#FFE3BB"><p style="margin-left: 10"><font face="Verdana" size="2">File Location:</font></td><td width="85%" bgcolor="#FFE3BB"><font face="Verdana" size="2"><input type="file" name="fileUpload" size="20"></font></td></tr><tr><td width="33%" bgcolor="#FFE3BB"><p style="margin-left: 10"><font face="Verdana" size="2"><br><br> </font></td><td width="67%" bgcolor="#FFE3BB"><font face="Verdana" size="2"><input type="submit" value="Upload this file" name="cmdSubmit"></font></td></tr></table></form></body></html>

<?phpglobal $strDesc;global $fileUpload;global $fileUpload_name;global $fileUpload_size;global $fileUpload_type;if(empty($strDesc) || $fileUpload == "none")die("You must enter both a description and file");// Database connection variables$dbServer = "localhost";$dbDatabase = "kid";$dbUser = "root";$dbPass = "pass";$fileHandle = fopen($fileUpload, "r");$fileContent = fread($fileHandle, $fileUpload_size);$fileContent = addslashes($fileContent);$sConn = mysql_connect($dbServer, $dbUser, $dbPass)or die("Couldn't connect to database server");$dConn = mysql_select_db($dbDatabase, $sConn)or die("Couldn't connect to database $dbDatabase");$dbQuery = "INSERT INTO image_kid VALUES ";$dbQuery .= "(0, '$strDesc', '$fileContent', '$fileUpload_type')";mysql_query($dbQuery) or die("Couldn't add file to database");echo "<h1>File Uploaded</h1>";echo "The details of the uploaded file are shown below:<br><br>";echo "<b>File name:</b> $fileUpload_name <br>";echo "<b>File type:</b> $fileUpload_type <br>";echo "<b>File size:</b> $fileUpload_size <br>";echo "<b>Uploaded to:</b> $fileUpload <br><br>";echo "<a href='uploadfile.php'>Add Another File</a>";?>

got an error message"You must enter both a description and file"even if i fill it up properly..

Edited by arden

Share this post


Link to post
Share on other sites

You have to tell mysql what fields you'll be filling in:INSERT INTO image_kid (fieldname, fieldname, fieldname) VALUES ('value', 'value', 'value');

Share this post


Link to post
Share on other sites

Read your code.

<?phpglobal $strDesc;global $fileUpload;global $fileUpload_name;global $fileUpload_size;global $fileUpload_type;if(empty($strDesc) || $fileUpload == "none")die("You must enter both a description and file");

Share this post


Link to post
Share on other sites

can i upload an image directly to the database using the INSERT script? i mean like this one:

<?php$con = mysql_connect("localhost","root","pass");if (!$con)  {  die('Could not connect: ' . mysql_error());  }// Create databaseif (mysql_query("CREATE DATABASE image_db",$con))  {  echo "Database created";  }else  {  echo "Error creating database: " . mysql_error();  }// Create tablemysql_select_db("image_db", $con);$sql = "CREATE TABLE image_table(imageId int auto_increment,imageData BLOB,primary key (imageId))";// Execute querymysql_query($sql,$con);mysql_close($con);?> 

<?php$con = mysql_connect("localhost","root","pass");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("image_db", $con);mysql_query("INSERT INTO image_table (imageId, imageData)VALUES ('0', ' IMAGE' ) "); // IMAGE = the image i want to upload. idk how to put it there.mysql_close($con);?> 

im using the PHP MySQL Insert Into,,,as a reference...*is it posible to upload an image using that code?..

Share this post


Link to post
Share on other sites

The easiest way to insert items into a database is to use an INSERT query. Do you understand what was wrong with your previous code?

Share this post


Link to post
Share on other sites
Read your code.
<?phpglobal $strDesc;global $fileUpload;global $fileUpload_name;global $fileUpload_size;global $fileUpload_type;if(empty($strDesc) || $fileUpload == "none")die("You must enter both a description and file");

The easiest way to insert items into a database is to use an INSERT query. Do you understand what was wrong with your previous code?
the global part?..or my if statement?..btw..is it possible to upload an image using the INSERT query?..like the code i made last..

Share this post


Link to post
Share on other sites

You've asked the same question 5 times in this one thread. Yes, it is possible to add any binary data, such as an uploaded file, to a MySQL table. You're not even processing the form correctly though, review the PHP forms section on the site. You should be using $_POST to access the data from the form. There are several examples online of handling file uploads with PHP, including on the w3schools site and also at php.net. Once you have the name of the uploaded file on the server, you can use the file_get_contents function to get the data which goes in the database.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×