How to upload and display image


How to upload image in form and the name of the uploaded image saved in database, then the image need to display in the output of form, which is displayed from the Database.

My code is :

include 'config.php';
echo "<h3>Category Adding Page</h3>";
if(isset($_POST['send'])) {
  $categoryname = $_POST['categoryname'];
 $categoryimage = $_POST['categoryimage'];

  //connect to database
   $con = mysqli_connect("localhost","root","","productlist");
   $q  = mysqli_query($con,"SELECT categoryname FROM categorylist WHERE categoryname  = '$categoryname'"); 
 if(mysqli_num_rows($q) > 0 )
		$errmsg = 'Category Name already exist';
	mysqli_query($con,"INSERT INTO categorylist (categoryname) VALUES ('$categoryname')");
	$savemsg = "Your value is saved in Database.";


    text-align: center;
    font-size: 27px;
    background: #000000;
    color: #ffffff;
    padding: 11px;
function validateform(){  
var categoryname=document.myform.categoryname.value;  

if (categoryname==null || categoryname==""){  
  alert("Category Name can't be blank");  
  return false;  


<?php if ($successmsg==0)
<?php if(isset($errmsg)) echo $errmsg; ?>

<form method="post" action="" enctype="multipart/form-data"
name="myform" onsubmit="return validateform();">

<table width="600px">
 <td valign="top" style="padding-bottom: 15px;">
  <label for="categoryname1">Category Name</label>
 <td valign="top">
  <input  type="text" name="categoryname" maxlength="50" size="30" value="<?php if(isset($_POST['categoryname'])) echo $_POST['categoryname']; ?>">
 <td valign="top" style="padding-top: 13px;">
  <label for="email1">Category Image</label>
 <td valign="top">
 <input type="file" name="fileToUpload" id="fileToUpload" style="padding-top:13px";>
    <input type="submit" value="Upload Image" name="submit" style=" margin-left: -11%;">

 <td colspan="3" style="text-align:center"><br>
 <input type="submit" name="send" value="Submit" style="margin-left: -14%;margin-top: 3%;padding: 6px 21px;">
<?php }
	echo $savemsg;
	header("location: categorylistpage.php");

<!--End code-->

Anyone know how to solve this?

Off Topic

@sankaripriyar: when you post code on the forums, you need to format it so it will display correctly. (I have formatted your post above.)

You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code.

You need to wait a bit and not expect answers straight away as most people here have other jobs and only visit now and again.

So a couple of things…

From a quick look it doesn’t seem as if you are inserting the $categoryimage = $_POST['categoryimage']; anywhere. It’s not doing anything.

Also you are open to sql injection as your variables are unsanitized.

The way i normally tackle this is in 2 parts. Firstly i store the uploaded image on my webserver (you have to do lots of checks to make sure it is actually an image) and then once that is in place and i have the image name/address i store that in the database with the additional data. Whilst you can store the image as a blob in the database it will eventually slow it down. I prefer to link to a file. With a bit of thought you can generate ‘fake’ names for the image when it is called so a user won’t know the actual address of the stored file.

I would suggest doing a search for an image upload script. Once you have that working create a database script to store the information you want and when that is working look at combining the 2 scripts.


Scroll down to this post from only a day ago, there’s some discussion about uploading and resizing images, that would give a good starting point.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.