welcome all viewers thanks for reading and learning by myprograming.com now I explain how to insert an image file in your database. and how this insert file in-store your folder.
now start how to insert an image in PHP ( how to insert an image in the database ).
first your localserver on then open localhost/phpMyAdmin in your browser now create a new database and create a new table.
- Create new Database
- Create new table
- Create index.php file
- Create connection.php file
- Create a new Image store Folder
Table of Contents
1.Create new Database
create new database in phpmyadmin . database name is uploadimage
2.Create new table
create new table in uploadimage database. table name is info
3.Create index.php file
now create index.php and write HTML structure then add PHP code also.
<!DOCTYPE html>
<html>
<head>
<title>img </title>
</head>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="pic"><br><br>
<textarea rows="2" cols="100" name="comment" placeholder="enter comment"></textarea>
<input type="submit" name="submit">
</form>
<?php
error_reporting(0);
include 'connection.php';
if (isset($_POST['submit'])) {
$filename=$_FILES['pic']['name'];
$tmp_name=$_FILES['pic']['tmp_name'];
$folder="images/".$filename;
move_uploaded_file($tmp_name,$folder);
$comment=$_POST['comment'];
$sql="insert into info values('$id','$folder','$comment')";
$con=mysqli_query($connect,$sql);
if ($con) {
echo "insert";
}else{
echo "error";
}
}
?>
<?php
include 'connection.php';
$q1="select * from info";
$data=mysqli_query($connect,$q1);
if ($res=mysqli_num_rows($data)) {
?><table border="2">
<tr>
<th>id</th>
<th>image</th>
<th>comment</th>
</tr>
<?php
while ($result=mysqli_fetch_array($data)) {
echo "
<tr>
<td>".$result['id']."</td>
<td><img src='".$result['pic']."' width='350px'</td>
<td>".$result['comment']."</td>
";
}
}
?>
</body>
</html>
4.Create connection.php file
now this is connection.php file add here.
<?php
// Enter your Host, username, password, database below.
// I left password empty because i do not set password on localhost.
$connect=mysqli_connect('localhost','root','','uploadimage') or die("connection failed : ".mysqli_connect_error());
if ($connect) {
// echo "Connection Successfully";
}
else{
echo "Sorry Some Mistakes is";
}
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
5.Create a new Image store Folder
Now open where the index.php file is.here Create a new folder named image.
output:-
How to image insert and view in one page?
this is also explained.
this tutorial blog is very easy so now so read this blog.