retrieve image from mysql with pageing
Posted: Thu May 15, 2008 2:18 am
Hi
I am new to PHP
i have created a table to store images in to the database, when i retrieve data and image from the mysql table using pageing,
text data changes but image does not change...
please help me with this code....
this is the table that i use to store images in
==========================
table------------------------------------------------
CREATE TABLE image_table ( ImageId int(10) NOT NULL auto_increment,
name varchar2(35) default null,
imgdata longblob, filetype varchar(32) default NULL,
PRIMARY KEY (ImageId) );
-----------------------------------------------------------------------------------------------------------------------------
code to insert the image in to the table
insert_image.html
<html>
<head>
<title>Add Image</title>
</head>
<body>
<form action="insert_image.php" method="post" enctype="multipart/form-data" name="form1">
Name : <input type="text" name="name" size="20" />
<br>
Please browse to upload your Photo:
<input type="file" name="file" size="20">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
-----------------------------------------------------------------------
the code for insert_image.php which is called by insert_image.html
<?php
if ($_POST['Submit'])
{
if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size'])
{
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test");
$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"),$_FILES['file']['size']));
$query = sprintf("INSERT INTO
image_table(
name,
imgdata,
filetype
)
VALUES(
'$_POST[name]',
'%s', '%s')", $photo, $_FILES['file']['type']);
if (mysql_query($query))
{
echo "Your files is successfully store in database";
}
else
{
echo " ".mysql_error();
}
}
else
{
echo "The file is bigger than the allowed size please resize";
}
}
mysql_close($con);
?>
------------------------------------------------------------------------------------------------------------------------------
this is the code to retrive image from database ....
name is changing when i click the page 1 2 3 link... but my problem is that image is not changing...
this code is saved as image_show.php
-------------------------------------------------------------------------------------------------------------
this is the code for image_search.html
<HTML>
<HEAD><TITLE></TITLE></HEAD>
<BODY>
<CENTER>
<BR><BR>
<FORM NAME="Search" ACTION="image_search.php" METHOD="POST">
<INPUT TYPE="TEXT" NAME="search_name">
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Search">
</FORM>
</CENTER>
</BODY>
</HTML>
--------------------------------------------------------------------------------------------------------------------------------
this is the code for image_search.php which is called by image_search.html
<?php
// Connect to database
$search_name=$_REQUEST[search_name];
$errmsg = "";
$con = mysql_connect("localhost","root","password");
if (!$con) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("test");
// Find out about latest image
//$uid = 0;
$query ="select name, imgdata from image_table where name LIKE '%$search_name%' ORDER BY name";
$SearchResult=mysql_query($query) or die(mysql_error());
$NumberOfResults=mysql_num_rows($SearchResult);
$Limit = 5; //Number of results per page
$NumberOfPages=ceil($NumberOfResults/$Limit);
$page=$_GET["page"]; //Get the page number to show
If($page == "") $page=1;
$SearchResult=mysql_query("SELECT name,imgdata FROM image_table WHERE name LIKE '%$search_name%' ORDER BY name LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error());
While($row = mysql_fetch_array($SearchResult))
{
$name = $row[name];
$bytes = $row[imgdata];
if ($_REQUEST[abc] == 1) {
header("Content-type: image/jpeg");
print $bytes;
exit ();
}
echo "</table>";
}
$Nav=" ";
For($i = 1 ; $i <= $NumberOfPages ; $i++) {
If($i == $page) {
$Nav .= "<B>$i</B>";
}Else{
$Nav .= " "."<A HREF=\"image_show.php?page=" . $i . "&search_name=" .urlencode($search_name) . "\">$i</A>"." ";
}
}
//mysql_close($con);
?>
</body>
</html>
<html><head>
<title></title>
<body bgcolor=white><h2>Picture</h2>
<p><font color=red>
<?php $errmsg ?>
</font></p>
<center>
<p> </p>
<table width="675" height="295" border="1">
<tr>
<td width="393" height="289">
<?php echo "Name: "." ".$name."<br>"; ?>
</td>
<td width="266"><img src=?abc=1 height = 285 width=266></td>
</tr>
</table>
<?php Echo "<br>Pages". $Nav ."</center>"; ?>
</center>
</body>
</html>
-------------------------------------------------------------------
Regards
Monty sen
I am new to PHP
i have created a table to store images in to the database, when i retrieve data and image from the mysql table using pageing,
text data changes but image does not change...
please help me with this code....
this is the table that i use to store images in
==========================
table------------------------------------------------
CREATE TABLE image_table ( ImageId int(10) NOT NULL auto_increment,
name varchar2(35) default null,
imgdata longblob, filetype varchar(32) default NULL,
PRIMARY KEY (ImageId) );
-----------------------------------------------------------------------------------------------------------------------------
code to insert the image in to the table
insert_image.html
<html>
<head>
<title>Add Image</title>
</head>
<body>
<form action="insert_image.php" method="post" enctype="multipart/form-data" name="form1">
Name : <input type="text" name="name" size="20" />
<br>
Please browse to upload your Photo:
<input type="file" name="file" size="20">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
-----------------------------------------------------------------------
the code for insert_image.php which is called by insert_image.html
<?php
if ($_POST['Submit'])
{
if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size'])
{
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test");
$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"),$_FILES['file']['size']));
$query = sprintf("INSERT INTO
image_table(
name,
imgdata,
filetype
)
VALUES(
'$_POST[name]',
'%s', '%s')", $photo, $_FILES['file']['type']);
if (mysql_query($query))
{
echo "Your files is successfully store in database";
}
else
{
echo " ".mysql_error();
}
}
else
{
echo "The file is bigger than the allowed size please resize";
}
}
mysql_close($con);
?>
------------------------------------------------------------------------------------------------------------------------------
this is the code to retrive image from database ....
name is changing when i click the page 1 2 3 link... but my problem is that image is not changing...
this code is saved as image_show.php
-------------------------------------------------------------------------------------------------------------
this is the code for image_search.html
<HTML>
<HEAD><TITLE></TITLE></HEAD>
<BODY>
<CENTER>
<BR><BR>
<FORM NAME="Search" ACTION="image_search.php" METHOD="POST">
<INPUT TYPE="TEXT" NAME="search_name">
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Search">
</FORM>
</CENTER>
</BODY>
</HTML>
--------------------------------------------------------------------------------------------------------------------------------
this is the code for image_search.php which is called by image_search.html
<?php
// Connect to database
$search_name=$_REQUEST[search_name];
$errmsg = "";
$con = mysql_connect("localhost","root","password");
if (!$con) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("test");
// Find out about latest image
//$uid = 0;
$query ="select name, imgdata from image_table where name LIKE '%$search_name%' ORDER BY name";
$SearchResult=mysql_query($query) or die(mysql_error());
$NumberOfResults=mysql_num_rows($SearchResult);
$Limit = 5; //Number of results per page
$NumberOfPages=ceil($NumberOfResults/$Limit);
$page=$_GET["page"]; //Get the page number to show
If($page == "") $page=1;
$SearchResult=mysql_query("SELECT name,imgdata FROM image_table WHERE name LIKE '%$search_name%' ORDER BY name LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error());
While($row = mysql_fetch_array($SearchResult))
{
$name = $row[name];
$bytes = $row[imgdata];
if ($_REQUEST[abc] == 1) {
header("Content-type: image/jpeg");
print $bytes;
exit ();
}
echo "</table>";
}
$Nav=" ";
For($i = 1 ; $i <= $NumberOfPages ; $i++) {
If($i == $page) {
$Nav .= "<B>$i</B>";
}Else{
$Nav .= " "."<A HREF=\"image_show.php?page=" . $i . "&search_name=" .urlencode($search_name) . "\">$i</A>"." ";
}
}
//mysql_close($con);
?>
</body>
</html>
<html><head>
<title></title>
<body bgcolor=white><h2>Picture</h2>
<p><font color=red>
<?php $errmsg ?>
</font></p>
<center>
<p> </p>
<table width="675" height="295" border="1">
<tr>
<td width="393" height="289">
<?php echo "Name: "." ".$name."<br>"; ?>
</td>
<td width="266"><img src=?abc=1 height = 285 width=266></td>
</tr>
</table>
<?php Echo "<br>Pages". $Nav ."</center>"; ?>
</center>
</body>
</html>
-------------------------------------------------------------------
Regards
Monty sen