Menu
Upload-Image-in-MySql-Database-in-PHP-small

Upload Image in MySql Database Using PHP Use Them in Table/Slider

PHP By Nov 17, 2013 5 Comments

Upload Image in MySql Database Using PHP Use Them in Table/Slider

Upload Image in MySql Database in PHP

Upload Image in MySql Database in PHP this is the topic on which we will discuss today and learn it how to do it. In this tutorial we will see that how we will upload the image and save it in our image folder and save the image in database too. In this tutorial we will go through the 3 technologies that is – PHP, CSS, MYSQL.

PHP for getting the image and saving the image.

CSS for showing that image in our page.

MySQL for saving the image in the datbase.

So lets start our Upload Image in MySql Database Using PHP tutorial.  😎

First Step – Form for Saving Image

In this step we will write the code for generating the form for uploading the image and saving the name with the image in the database.

<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" enctype="multipart/form-data">
<table>
<tr>
<td>Artist Name</td><td>:</td><td><input type="text" name="aname" id="aname" value=""></td>
</tr>
<tr>
<td>Artist Image</td><td>:</td><td><input type="file" name="upload" id="upload"></td>
</tr>
<tr>
<td></td><td></td><td><input type="submit" id="addPage" name="addPage" value="Add Artist"></td>
</tr>
</table>
</form>

As you can see we will are using 2 more entities in our FORM that is “Action” and “Enctype”. We are using the action as $_SERVER[“PHP_SELF”] which will call our php code which is written in our page when we will click the submit button. Now you are think action is ok but why we are using the “Enctype”. We are using it because we are using the multipart data not only the text. So while we are uploading any data except text than we have to use the Enctype=”multipart/data-type”.

Step 2: – Create database table

Now we will create the database to store the image url so that we will use it in our project or page.

CREATE TABLE IF NOT EXISTS myimages(
Id int(11) NOT NULL AUTO_INCREMENT,
Image varchar(100),
Image_name varchar(100),
PRIMARY KEY (`Id`)
)

We will create the table in our database with 3 filed. ID, Image, Image_name. Our Id will be auto increment and image will save the url of the image and also the image name.

Step 3: Image Upload PHP Code

We will write the PHP code for Upload Image in MySql Database Using PHP in our image folder on submit button.

if(isset ($_POST["addPage"]))
{
$getName=$_POST["aname"];
$getSlider=basename($_FILES["upload"]["name"]);
$target="myimages/";
$ran = time();
$target=$target.$ran.$getSlider;
if($_FILES["upload"]["type"]=="image/gif"||$_FILES["upload"]["type"]=="image/jpeg"||
$_FILES["upload"]["type"]=="image/png"||$_FILES["upload"]["type"]=="image/pjpeg")
{
move_uploaded_file($_FILES["upload"]["tmp_name"],$target);
if(move_uploaded_file)
{
$alert=addImage($target,$getName);
}
}
else
{
$alert= "<span>File type not supported</span>";
}
}

In above code we will get the image and name. Than we will check that its an image format or not if its an image it will call the “addImage” function. Which will add the image url and the name in the database.

Upload Image in MySql Database

function addTeam($target,$name)
{
$dbhost="localhost";
$dbusername="root";
$dbpassword="";
$dbname="mydatabase";
$con=mysqli_connect($dbhost,$dbusername,$dbpassword,$dbname);
$insert="Insert into myimages(Image,Team_name) values('$target','$name')";
$result=mysqli_query($con,$insertFloor);
if($result)
{
return "<span>Image is uploaded</span>";
}
else
{
return "<span>Error in image upload</span>";
}
}

If the image will upload it will return the image uploaded message else it will show the error message.

If we want to show this mysql table into our page than we need the code which will iterate the code and show the data.




Display values from MySql in PHP

You can checkout the tutorial for this How to Fetch Data From Database and Display in Table in PHP

$getlist="Select * from myimages Order By Id DESC";
$result=mysqli_query($con,$getlist);
$count=mysqli_num_rows($result);
if($count=="")
{
}
else
{
$alert.="<table><tr><th>Image</th><th>Name</th></tr>";
while($row= mysqli_fetch_array($result))
{
$alert.="<tr>
<td><img src='$row[Image]' width=120px height=120px></td>
<td>$row[Image_name]</td></tr>";
}
$alert.="</table>";
return $alert;

So thats the end of the tutorial and we are ready to Upload Image in MySql Database Using PHP. You can use it in your project or application. If you found any kind of error or if you want to put any feedback than let me know or comment here. Thank You

~~~~ HAPPY CODING ~~~~

Author

Hello i am Vivek Moyal PHP developer and writes tutorials for students and beginners. Love to explore new cities

5 Comments

  1. anon says:

    First of all you can’t use like this: $row[Image]
    because PHP will interpret the “Image” like a CONSTANT.
    Other problem is:

    move_uploaded_file($_FILES[“upload”][“tmp_name”],$target);
    if(move_uploaded_file)

    move_uploaded_file is a function and you use it as a CONSTANT in second line.
    just set the result on a variable like:
    $result = move_uploaded_file(..)
    also if you don’t want to get any warnings, write like this: $result = @move_uploaded_file(..);

    1. Vivek Moyal says:

      Thank you for the comment but i am not getting any kind of warning while performing this. For the first one why php will interpret it CONSTANT. If you have 10 or 20 images than also it will show everyone in different rows. There will no any error. Everytime the $row[Image] value will be changed.

      I dont think that we need to store the move_upload_file in any variable as we can do not want to use it further. Ya your point is true if we need the result for more than 1 we should save the result in any variable.

      For making something constant we have to use CONSTANT keyword

      1. anon says:

        error_reporting(E_ALL | E_STRICT);
        use this 🙂

        in PHP constants are written without any symbol in the front.

        in PHP there are no keyword CONSTANT, except in classes “const”.
        http://us1.php.net/constants

        if(move_uploaded_file)

        this line can trigger an error because you evaluating non existing constant:

        Notice: Use of undefined constant move_uploaded_file – assumed ‘move_uploaded_file’ in […][…] on line 1

        maybe you are using <php5 or error reporting is disabled.

        1. Vivek Moyal says:

          For constants i think we have to use “Define” or “Constant” keyword where as if you see that in myfunction i was using $row variable which is in while loop and fetching the values.

          And in another one that is move_uploaded i am still not getting the warning…. but let it be….. if we dont use it like this than what should we do……. It will be nice for everyone if you will describe it for us.

Leave a comment

Your email address will not be published. Required fields are marked *