Upload an image to database using sql and php

31 views
Skip to first unread message

Chathuranga Lakmal

unread,
Aug 5, 2016, 10:28:37 AM8/5/16
to techkatha
Hello friends,

I need to sava an image file in a database using sql and php. How should I code my process.php file?

My form.php

<html>
     <body>
<form method="POST" action="process.php" enctype="multipart/form-data">
<input type="file" name="myimage">
<input type="submit" name="submit_image" value="Upload">
</form>

     </body>
</html>

My database name is images

Table structure

CREATE TABLE tbl_image (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
img_name VARCHAR(20) NOT NULL,
image LONGBLOB
        );

--

නොදන්න දේ ඉගෙනගන්න, දන්නා දේ බෙදාගන්න.
http://lattalottaroom.blogspot.com/



Gayan Tharaka

unread,
Nov 6, 2016, 1:00:14 PM11/6/16
to techkatha@googlegroups com
First upload image. if successfully uploaded save image title to a variable. then use that variable while saving data about image. 

<form method="POST" action="process.php" enctype="multipart/form-data">
<input type="file" name="file">
<input type="text" name="image_name">
<input type="submit" name="submit_image" value="Upload">
</form>


$target_path = "../images/";
$file_move=false;
       if ($_FILES["file"]["name"] != "") {
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path . basename($_FILES['file']['name']))) {
$file_move=true;
}
if($file_move) {
$sql = "INSERT INTO tbl_image (img_name ,image) VALUES('" $_FILES["file"]["name"] . "','". $_POST['title']."')";
if(!mysql_query($sql))
echo mysql_error();
}
}


if you need to save image directly in db use base64

--
ටෙක්කතා සමූහය සඳහා ආචාර ධර්ම http://www.techkatha.com/rules-and-regulations/
 
ඔබට මෙම පණිවිඩය ලැබෙන්නේ ඔබ Teckකතා හි සාමාජිකයෙක් බැවිනි.
ලිවීම සඳහා tech...@googlegroups.com වෙත ලියන්න.
Teckකතා හින් ඉවත්වීම සඳහා http://googlegroups.com/group/techkatha/subscribe වෙත යන්න.
 
වෙබ් අඩවිය http://www.techkatha.com
---
You received this message because you are subscribed to the Google Groups "Techකතා" group.
To unsubscribe from this group and stop receiving emails from it, send an email to techkatha+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Best Regards
K.P.Gayan Tharaka
http://about.me/kpgtharaka

[sinhala-blog]
www.kpgtharaka.blogspot.com

Chathuranga Lakmal

unread,
Nov 17, 2016, 10:46:34 AM11/17/16
to techkatha
Thnks gayan :) I'll try this method.
Reply all
Reply to author
Forward
0 new messages