I need to upload and retrieve image from a database, I am able to store the image in the database but unable to display it later. please help
I wrote the following code to retrieve from the database.
$result1=mysql_query("INSERT INTO userdata(id, username, firstname, lastname, imageType, image)VALUES('', '" . $_SESSION['username'] . "', '" . $_SESSION['firstname'] . "', '$lastname','{$image_size['mime']}','{$imgData}')") or die("Invalid query: " . mysql_error());
if($result1)
{
echo "</br>";
echo "Registration successful";
echo "</br>";
echo $lastid=mysql_insert_id();//get the id of the last record
echo "uploaded image is :"; ?>
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/>
<?php
echo "</br>";
}#if result1into db successful
else
{
echo $result1;
echo "Problem in database operation";
imageView.php has the following code:
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("wordgraphic") or die(mysql_error());
if(isset($_GET['id'])) {
$sql = "SELECT imageType,image FROM userdata WHERE id=". $_GET['image_id'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: " . $row["imageType"]);
echo $row["image"];
}
mysql_close($conn);
?>
What could be possibly wrong with the code?
When i try to run imageView.php with static id image is getting displayed. So i guess the error is in passing the variable is this code:
echo "uploaded image is :"; ?>
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/>
<?php
what could be possibly wrong?
just a smal rectification
<img src="imageView.php?image_id=<?php echo $lastid; ?>" />
instead of this
src = src path where u r saving the image file lets say in images folder that would be here $imagedata store the file name in the DB so that you can retreive it from the image folder
<img src="images/$imgData" width="your wish" height="your wish"/>
Related
Just to preface im very new to this so this might be a dumb question but...
I am trying to design a website pulling from a database where i've stored the product info for the 'shop'. This is the code i have so far.
session_start();
include("connections.php");
$con = mysqli_connect($servername, $username, $password, $database);
$sql="SELECT product_title, product_desc, product_image, product_price, product_type FROM tbl_products";
$results=mysqli_query($con, $sql);
if(mysqli_num_rows($results) > 0) {
while($row=mysqli_fetch_array($results)) {
echo $row[0];
echo "<br>";
echo $row[1];
echo "<br>";
echo $row[2];
echo "<br>";
echo $row[3];
echo "<br>";
echo $row[4];
echo "<br>";
}
}
mysqli_close($con);
?>
It works fine at displaying the info however rather than displaying the image its just displaying the file name e.g. hoodie(1).jpg. I assume i need to save the images to the database or to the folder but i tried putting them in my code folder and they still dont show up, any ideas?
To display the image use:
echo '<img src="'.$row[2].'" alt="" />';
To upload a image, learn file uploading:
https://www.tutorialspoint.com/php/php_file_uploading.htm
I am trying to render uploaded image from Mysql to frontend in a file called result.php
This is the code that uploaded the image to Mysql
include ('connect.php');
//if button with the name uploadfilesub has been clicked
if(isset($_POST['uploadfilesub'])) {
//declaring variables
$filename = $_FILES['uploadfile']['name'];
$filetmpname = $_FILES['uploadfile']['tmp_name'];
//folder where images will be uploaded
$folder = 'imagesuploadedf/';
//function for saving the uploaded images in a specific folder
move_uploaded_file($filetmpname, $folder.$filename);
//inserting image details (ie image name) in the database
$sql = "INSERT INTO `uploadedimage` (`imagename`) VALUES ('$filename')";
$qry = mysqli_query($conn, $sql);
if( $qry) {
echo "</br>image uploaded";
}
}
?>
The uploaded images are saved on a folder on my server called imagesuploadedf
I used this code below to render the image on the result.php but it's showing the file name only, I need the image to be rendered.
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = 'SELECT * from uploadedimage';
if (mysqli_query($conn, $sql)) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$count=1;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) { ?>
<tbody>
<tr>
<td>
<?php echo $row['imagename']; ?>
</td>
</tr>
</tbody>
<?php
$count++;
}
} else {
echo '0 results';
}
?>
</table>
What do I need to write on result.php to show/render the uploaded image on the page?
You should write an HTML page containing <img> elements referring to the images you want to display.
It might look like this:
<td>
<?php
$imgTag = '<img src="/imagesuploadedf/' . $row['imagename'] . '" />';
echo $imgTag; ?>
</td>
This puts out stuff like this into your web page.
<td>
<img src="/imagesuploadedf/image_file_name"/>
</td>
If your web page is coming from https://images.example.com this will make the browser download the image at https://images.example.com/imagesuploadedf/image_file_name from your server and display it in the page.
This is the structure of my mysql table crij_personne :
id
photo longblob
image_name varchar(64)
My html code is the following:
<input type="file" name="photo" />
The code for uploading the image to my database:
$imagetmp=addslashes (file_get_contents($_FILES['photo'] ['tmp_name']));
$image_name = addslashes($_FILES['photo']['image_name']);
$imagetmp= base64_encode($imagetmp);
$sql = "INSERT INTO crij_personne (description, photo, image_name) VALUES ('".$_POST['description']."','$imagetmp',' $image_name');";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
The code for displaying the image on a web page is:
echo '<img height="300" width="300" src="data:image;base64, '.$row['photo'].'">';
But nothing is happening... I'm doing something wrong, obviously.
Thanks for the help.
Try this one.
For insertion values into the database.
$image_name = addslashes($_FILES['photo']['image_name']);
$sql = "INSERT INTO crij_personne (description, photo, image_name) VALUES ('".$_POST['description']."','$imagetmp',' $image_name');";
if (mysqli_query($conn, $sql)) echo "New record created successfully";
else echo "Error: " . mysqli_error($conn);
For retrieval of image from Database
$connectDb = mysqli_connect("localhost","root","password","DatabaesName"); //You need to replace with your own credentials
$query= "SELECT * FROM crij_personne WHERE id = $id";
$sth = $connectDb ->query($query);
$mysqli_fetch_array=mysqli_fetch_array($sth);
header("Content-type: image/jpeg");
echo '<img src="data:image/jpeg;base64,'.base64_encode( $mysqli_fetch_array['image_name'] ).'"/>';
And then retrieve, I hope you will get the correct output.
Hey guys am having trouble displaying an image from the DB on a web page.
Am using this to display
echo "<img
src=image.php?"' width=300 height=270/>";
here is my image.PHP
<?php
session_start();
$con1=mysql_connect("localhost","root","roots");
mysql_select_db("blog",$con1);
if(!$con1)
{
die('could not connect'.mysql_error());
}
$q="select image from data where Number=1";
$result= mysql_query($q,$con1);
if($result)
{
$row=mysql_fetch_array($result);
header("content-type:image/jpeg");
echo $row['image'];
}
else
{
echo mysql_error();
}
?>
echo $row['image']; simply display the data's fetched from DB.
Supposed your image column holds data like this http://domain/project/images/img.jpg
echo '<img src="'.$row['image'].'" width=300 height=270 />' ;
I want to display uploaded image to a different page showing first name last name and image corresponding to that upload this is the code I have it currently displaying all the images save on a folder I just want to display the recent uploaded image corresponding to the information submitted
<?php
$con = mysql_connect("localhost","username","pa…
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$sql="INSERT INTO nametable (fname, lname, mname, image, status, id)
VALUES
('$_POST[fname]','$_POST[mname]','$_PO… 'display','')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "";
mysql_close($con);
?> <?php
// Assigning value about your server to variables for database connection
$hostname_connect= "localhost";
$database_connect= "database";
$username_connect= "username";
$password_connect= "password";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR…
#mysql_select_db($database_connect) or die (mysql_error());
if($_POST)
{
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "
";
}
else
{
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{ //move_uploaded_file function will upload your image.
if(move_uploaded_file($_FILES["file"]["t… . $_FILES["file"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$query_image = "insert into table";
if(mysql_query($query_image))
{
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
else
{
echo'';
}
}
}
}
}
?>
<html>
<head>
</head>
<body>
<p>First name: <?php echo $_POST["fname"]; ?><br></p>
<p>Middle name: <?php echo $_POST["mname"]; ?><br></p>
<p>Last name: <?php echo $_POST["lname"]; ?><br></p>
</form>
</body>
</html>
<?php
// Assigning value about your server to variables for database connection
$hostname_connect= "localhost";
$database_connect= "database";
$username_connect= "username";
$password_connect= "password";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR…
#mysql_select_db($database_connect) or die (mysql_error());
$query_image = "SELECT * FROM table";
// This query will show you all images if you want to see only one image pass id='$id' e.g. "SELECT * FROM nametable id='$id'".
$result = mysql_query($query_image);
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
echo '<img width="165" height="104" border="0" src="images/'.$row["image"].'">';
}
}
else
{
echo 'File name not found in database';
}
?>
$query_image = "SELECT * FROM table order by id desc limit 1";