How to upload and display image

Hi,

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 :

<?php
include 'config.php';
session_start();
echo "<h3>Category Adding Page</h3>";
$successmsg=0;
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';
	}
	
	
else
{
	mysqli_query($con,"INSERT INTO categorylist (categoryname) VALUES ('$categoryname')");
	$savemsg = "Your value is saved in Database.";
	$successmsg=1;
}

}
?>

<html>
<head>
<style>
h3{
    text-align: center;
    font-size: 27px;
    background: #000000;
    color: #ffffff;
    padding: 11px;
}
	</style>
	</head>
<body>
<script>  
function validateform(){  
var categoryname=document.myform.categoryname.value;  

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

}  
</script> 
 
<body>  

<?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">
<tr>
 <td valign="top" style="padding-bottom: 15px;">
  <label for="categoryname1">Category Name</label>
 </td>
 <td valign="top">
  <input  type="text" name="categoryname" maxlength="50" size="30" value="<?php if(isset($_POST['categoryname'])) echo $_POST['categoryname']; ?>">
 </td>
</tr>
<tr>
 <td valign="top" style="padding-top: 13px;">
  <label for="email1">Category Image</label>
 </td>
 <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>
</tr>


<tr>
 <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;">
 </td>
</tr>
</table>
</form>
<?php }
else{
	echo $savemsg;
	header("location: categorylistpage.php");
}

?>
<!--End code-->
</body>
</html>

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.

hth

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.