images in phpmyadmin using image path - php

Im trying to add images to my database by using an image path, under the Image column, i have used varchar as the the type and i have inserted images/canin.jpg as the value.
When im trying to display this image using php it doesn't work, this is what i have :
$sql = "SELECT dog_id, dogbreed, image, sellercontact, location FROM dog";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Dog Breed</th><th>Image</th><th>Seller and Contact number</th><th>Location</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["dog_id"]."</td><td>".$row["dogbreed"]." <td>".$row["image"]."</td><td>".$row["sellercontact"]." </td><td>".$row["location"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
All the other information such as dog it, dog breed etc is being retrieved, but its not bringing back the image.
any ideas?

You need to use the HTML img tag to display the image.
Replace <td>".$row["image"]."</td> with <td><img src=\"".$row["image"]."\"/></td>
This should produce <td><img="images/canin.jpg"/></td> when executed

Related

Broken Image Icon When Using Echo In PHP

I am trying to query the image file names from the database using the Id inputted by the user and want to display the images with the resulting file names form the select statement. However the echo function is showing only the broken image icons instead of the image itself. How can I fix this? Thanks!
//$searchValue is the Id inputted by the user in the html page
$result = mysqli_query($con,"SELECT FileName FROM images WHERE Id='$searchValue'");
//$folder is the path where the images are currently stored
$folder = "C:/xampp/htdocs/images/";
while($row = mysqli_fetch_array($result)){
$file = $row["FileName"];
echo '<img src="'.$folder.$file.'"><br />';
}

How do I display several images using URLs saved in phpmyAdmin in a website?

See Table of report data
The table is as displayed in the image above. I wanted to display all the data including several images for which URLs are saved in the imageURL column separated with a semicolon.
The number of image URLs differs in each columns.
The URLs refer to image path in upload folder as shown below:
InvestigatorsReportApi/uploads/KBC589L-1.jpg;
InvestigatorsReportApi/uploads/KBC589L-2.jpg;
InvestigatorsReportApi/uploads/KBC 589L-3.jpg;
How would I display this in a website?
You should simmply make a query about the URL:
$sql = "SELECT url FROM table";
$result = $conn->query($sql);
After that echo it into the <img> tag src attribute:
while($row = $result->fetch_assoc()) {
echo "<img src='".$row["url"]."'>";
}
And there you have it. The sample code above will print every image in the database.
Just try this
$sql = "SELECT url FROM table";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
foreach(explode(';',$row["url"]) as $image) {
echo "<img src='".$image."'>";
}
}
First fix your table. It is bad. You should let the attributes occupy the first row:
id: name: LOA: regNO: rout..., then
first you need to use explode function for separate your image url
$image=explode(";",$imageURL);
then use foreach loop for display image
foreach($image as $value)
{
<img src='".$value."'/>
}

Is it safe to use GET method for deleting information from a database?

I'm creating a website where users will need to be able to upload images, and I'd like them to be able to delete those images. Right now, I have a page that will display all of the images that that user has uploaded, and I have a php set up to delete an image from the database. It just needs to be given the id of the image. I have it functioning with the GET method, but I'm concerned a user could find the URL for my delete php and put in random ids, deleting everyone's images. Is there a way I can adjust my code to make it safer?
<?php
$sql = "SELECT id, userid, name, image FROM images";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
if ($imageUser == $row["userid"]){
echo "<tr>";
echo "<th>".$row["userid"]."</th>";
echo "<th>".$row["name"]."</th>";
echo "<th><img src='showimage.php?id=".$row["id"]."'></th>";
echo "<th><a href='imgdelete.php?id=".$row["id"]."'>delete</a></th>";
echo "</tr>";
}
}
} else {
echo "0 results";
}
?>
The delete.php simply deletes the entry WHERE id=$_GET['id'];
In a RESTful API, a GET request should never modify the data. If you want to delete items, you should use a POST or a DELETE request.

Only the last image displays from database

I asked this question before but no one could help me unfort. I have images and headings coming from database now the problem is that only one image is displaying(the last image) i need both to display.
here is my revised code
$query = "SELECT page_title, page_image FROM pages WHERE id='$page'";
$result = mysqli_query($connection, $query);
confirm_query($result);
while ($page_fetch = mysqli_fetch_assoc($result)) {
$page_title = $page_fetch['page_title'];
$images = $page_fetch['page_image'];
echo "<div class=\"content \">";
echo "<h3 class=\"words\"> $page_title </h3>";
echo "<img src='pics/" . $images . "' width=\"340\" height=\"252\" alt=\"\" />";
echo "</div>"; //end box
} // close while loop
here is my database for pages
page_id id page_image page_title
1 1 ocean.jpg have a look at the ocean
2 1 house.jpg The house
just some extra info the images must display dynamically , as they coming in from a form to db to this page
Your are returning your images from your query.
Check the view source page, You will find your error their.
Your returing image name will not match with the image which you got in the folder. (For the second one)

How to get the images and data stored in the database?

I have created a database namely newwork and table name property. In this database I have stored user details and one image. Now I want to display all the data from one field and stored image from the same field in a new page. Please help me for this program. The fields are property_id, property, location, image.
You really need two parts here (one to build the HTML and another to fetch/display the image):
Part 1: HTML builder
<?php
$res = mysqli_query($cnx, 'SELECT property_id, property, location from newwork';
if (res)
{
while ($row = mysqli_fetch_assoc($res))
{
echo '<span class="property">'.$row['property'].'</span>';
echo '<span class="location">'.$row['location'].'</span>';
echo '<span class="photo"><img src="image.php?id='.$row['property_id'].'" /></span>
}
}
Part 2: Image builder
<?php
$res = mysqli_query($cnx, 'SELECT image
FROM newwork
WHERE property_id='.intval($_REQUEST['id']));
if ($res)
{
$row = mysqli_fetch_assoc($res);
if (!empty($row))
{
header('Content-Type: image/jpg');
echo $row['image'];
exit;
}
}
header('Location: error_image.jpg',TRUE,302);
I suggest going though this step by step tutorial
http://www.tizag.com/mysqlTutorial/index.php

Categories