If You want to learn web design and developing from 0 to 100% then this blog is only for you Here you will be also find source code of many web design and web developing source codes. html,css,javascript,php,mysql & database tutorial, projects, source code files and many blogging tips. Best css tips etc. html css javascript amazing creations

Subscribe Us

https://affiliate.fastcomet.com/scripts/2wc9ym?a_aid=5f4ca944dcb6d&a_bid=49785082

Breaking

Post Top Ad

Friday, April 10, 2020

how to uplaod and insert image into mysql database using php source code for begniners

how to uplaod and insert image into mysql database using php source code in very short & simple code with screen short

 in this artical I will show you how to upload and insert image into mysql database using php in very short and simple code.  I will be give you a very shory method by using this you will be learn properly . I will also insert screen short that you will be learn properly.
So lets gate start,

So first let's talk about upload or insert image in database. In this case we have to only upload the name of image in database.  And  we have to create a folder named by image.
This image folder we will be store the images.


note: to learn this session you should have download xampp or wampp server in your pc.

or the xampp server's mysql and system started. about the system

 talking about the feature of this project. We create a simple upload image function with database in php.  You can easily upload your image name in database. We will make a submit button if the user click the submit button a popup will be show to select image and then after the selection of the image the image will be stored in the Image folder and the name of the image will stored in database.
 We have to create a database.

 Name database xyz  in this case I will be use uploadimage named DATABASE   
Create a row name Upload.  Make 2 colums 'id' aur 'imagename'.
gives the value 'id' int(11) ,not null, auto increament.
gives the value 'imagename' var(255), not null





let's start the process


So the first step is this that make a imgupload name folder in the path of xampp/htdocs/imgupload

in imgupload folder make a folder name Image  and a file index.php

1. index.php

The image folder and index.php path should be in the same location

xampp/htdocs/imguplaod

your image path should be
create this file in xampp/htdocs/index.php

make a file name by index.php
and use simple html5 code like

<!DOCTYPE html>
 <html lang="en">
  <head>
   <title>tittle</title>
    <link rel="icon" type="image/jpg" href="image/team-6.jpg" />
     <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <!-- Latest compiled and minified CSS -->  
<!-- Optional theme -->

</head>
<body>


<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

</body>
</html>

2. type the html code to select an image

 In second step we will be make a code. by this code I will be able to select images only.
In this code section we will be create a buttin after click the button an image selecting popup will be seen. Then users can select this a image.
The code is under below the line. Copy and pest it in the body tag of your index.php file 


   

<div class="container pt-5" style="position: absolute; right: 0; width:70%;" >
  <h2 class="text-center">Add Photogallary</h2>
  <div class="row mt-4 mb-4">
    <div class="col-md-6 offset-md-3">
      <form action="" class="shadow-lg p-4" method="POST" enctype="multipart/form-data">

         <div class="form-group"  method="POST">
         <input type="file" name="uploadfile" value="UPLOAD IMAGE"></br></br>
        </div>

     
        <button type="submit" class="btn btn-danger mt-5 btn-block shadow-sm font-weight-bold" name="save_post">Post</button>
        <em style="font-size:10px;">Note - By clicking Sign Up, you agree to our Terms, Data
          Policy and Cookie Policy.</em>
        <?php if(isset($regmsg)) {echo $regmsg; } ?>
      </form>


after pesting this code you will be see the image selecting code.

3. Make database connection

Our third step is to make connection the file with our database. We have created the database name row and columns name.

We have to make connection this database to our index.php file.So copy and pest this code in your index.php file


<?php
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "uploadimage";

// Create Connection
$conn = new mysqli($db_host, $db_user, $db_password, $db_name);

// Check Connection
if($conn->connect_error) {
 die("connection failed");
}
?>

your can pest this code on the top of your index.php file.

4. Link image name to database column

The complete this task We will be use the languagae php jquery.  By php language We will make connection to the database.

After this code our image name will be stores in the database and the image will be uplaod in image folder.

<?php



if(isset($_REQUEST['save_post'])){

   // Assigning User Values to Variable

   $filename = $_FILES["uploadfile"]["name"];

   $tempname = $_FILES["uploadfile"]["tmp_name"];
   $folder = "Image/".$filename;
   move_uploaded_file($tempname,$folder);



   $sql = "INSERT INTO upload (imagename) VALUES ('$folder')";

   if($conn->query($sql) == TRUE){

    // below msg display on form submit success

    echo  '<h5 style="position:absolute; bottom:10px; right:10px;">successfull submit </h5>';

    // include('submitrequestsuccess.php');

   } else {

    // below msg display on form submit failed

    $msg = '<div class="alert alert-danger col-sm-6 ml-5 mt-2" role="alert"> Unable to Submit Your Request </div>';

   }

 }


?>

After using this code your image will be easily insert in the Image folder and the name if the image will be stored in the database.

No comments:

Post a Comment

thanks for your comments

Post Top Ad