How to upload and display image from database using php mysql oops concept.

index.php


<?php 


include './classes/image_class.php';

$obj_image = new Image();

if(@$_POST['Submit'])
{
$obj_image->image_name=str_replace("'", "''", $_POST['txt_image_name']);
$obj_image->image=str_replace("'", "''", $_POST['txt_image']);

$obj_image->Insert_into_image();

$data_image=$obj_image->get_all_image_list();
$row=mysql_num_rows($data_image);
}

?>

<!DOCTYPE html>
<html>
<head>
<title>NOvaeye </title>
</head>
<body>
<CENTER><H1>Novaeyewear</H1></CENTER>
<CENTER><H2>Sunglass</H2></CENTER>

<CENTER>
<form method="post" enctype="multipart/form-data">
<table border="1" width="80%">
<tr>
<th width="50%">IMage NAme</th>
<td width="50%"><input type="text" name="txt_image_name"></input></td>
</tr>
<tr>
<th width="50%">Upload IMage</th>
<td width="50%"><input type="file" name="txt_image"></input></td>
</tr>
<tr>
<td></td>
<td width="50%"><input type="submit" name="Submit" value="Submit"></input></td>
</tr>
</table>
</form>
</CENTER>

<?php 
if($row!=0)
{
?>
<center>
<table width="80%" border="1"> 
<?php
$icount = 1;
while($data= mysql_fetch_assoc($data_image))
{
?>
<tr>
<td><?php echo $icount; ?></td>
<td><?php echo $data['image_name']?></td>
<td><img src="images/<?php echo $data['image']; ?>" width="400px" height="200px"></td>
</tr>
<?php
$count++;
}
?>
</table>
</center>
<?php
}

?>
</body>
</html>



classes/image_class.php



<?php
include 'db/db.php' ;

class Image{

var 
$image_id,
$image_name,
$image;

function Insert_into_image(){
if(isset($_FILES['txt_image']))
{
     $tempname = $_FILES['txt_image']['tmp_name'];
     $originalname =$_FILES['txt_image']['name'];
     $size =($_FILES['txt_image']['size']/5242888). "MB<br>";
     $type=$_FILES['txt_image']['type'];
     $image=$_FILES['txt_image']['name'];
     move_uploaded_file($_FILES['txt_image']['tmp_name'],"images/".$_FILES['txt_image']['name']);
   }


$query = "Insert into t_image_upload
(
image_name,
image
)
values
(
'$this->image_name',
'$image'
)";
if(mysql_query($query)){
echo "Insert success";
}
else
{
echo "Insert not success";
}
}

function get_all_image_list(){
$query = "select *from t_image_upload";
$result = mysql_query($query);
return $result;
}

}
?>


db/db.php



<?php   
$host = "localhost";
$username = "root";
$password = "";
$db_name  = "phpschool";

mysql_connect($host,$username,$password) or die("Database not connect");
mysql_select_db($db_name) or die ("could not connect databse");
error_reporting("E_ERROR_WORKING");
ECHO "DAtabase connecet success";


?>
Web hosting

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. mysql_ method is deprecated use mysqli_ please update your script

    ReplyDelete
  3. the image doesnt appear, but everything is working. Help please thanks!

    ReplyDelete
  4. How to display rate is null until update after chang date

    ReplyDelete
  5. include './classe/image_class.php';

    Warning: include(./classe/image_class.php): failed to open stream: No such file or directory in

    ReplyDelete

  6. Wonderful post!!Thank you for sharing this info with us.
    Keep updating I would like to know more updates on this topic
    Very useful content, I would like to suggest this blog to my friends.

    PHP Training Center in Chennai

    PHP Training Center in Chennai

    ReplyDelete

  7. Fatal error: Uncaught Error: Call to undefined function mysql_connect() in F:\creat\xampp\htdocs\image\db\db.php:7 Stack trace: #0 F:\creat\xampp\htdocs\image\classes\image_class.php(2): include() #1 F:\creat\xampp\htdocs\image\index.php(4): include('F:\\creat\\xampp\\...') #2 {main} thrown in F:\creat\xampp\htdocs\image\db\db.php on line 7

    ReplyDelete
  8. Gambling in California | FAQs - Dr.DMC
    We offer some good casino gambling. Our 속초 출장샵 state lottery has some great 문경 출장안마 casino gambling options. For 태백 출장마사지 example, 경주 출장샵 if you bet $1 문경 출장마사지 on the SuperLotto Plus,

    ReplyDelete

Post a Comment