echo my images on my index page PHP - php

i built a CMS and my image shows succesfully in the admin page(back-end) once i upload an image with the form but once on the main index page(front-end) it shows that "broke image link"
here is my function to post the uploaded image on my admin page:
function gettestimony() {
$query = mysql_query("SELECT * FROM testimony") or die(mysql_error());
while($post = mysql_fetch_assoc($query)){
echo "<img src =\"" . $post['photo']."\">";
echo "<p>" . $post['imagename'] . "</p>";
echo "<br>";
echo "<p>" . $post['test'] . "</p>";
echo "<br>";
echo "<p>" . $post['author'] . "</p>";
echo "<br>";
echo "Delete";
echo "Edit";
echo "<br>";
}
}
and the directory is htdoc/blah/admin/include/functions.php
and the image is saved in htdoc/blah/admin/image/
for my main index, this is the function to post it:
function gettestimony() {
$query = mysql_query("SELECT * FROM testimony") or die(mysql_error());
while($post = mysql_fetch_assoc($query)){
echo "<div class='column second'>";
echo "<p>" . $post['test'] . "</p>";
echo "<img src =\"" . $post['photo']."\">";
echo "</div>";
}
}
this is the code i use in both functions:
echo "<img src =\"" . $post['photo']."\">";
so like i said it works fine in admin page but my main page it doesn't....someone please help

Your image's path relative to the path of your function is irrelevant. As far as that function is concerned the path to the image is just a string.
You need to make sure the path to the image is correct relative to the HTML file that's viewing it. The easy way to make it work is to just use the full URL to the image:
www.something.com/admin/images/myimage.php

Related

How to make a PHP echo into a link

I'm trying to get this so when I click the image, the image then shows in a new tab so it's able to be saved
I've tried adding the a href tags inside and around.
Code:
echo "<img src=" . random_image('css/avatars') . " />";
This is what you want:
echo '<img src="' . random_image('css/avatars') . '" />';
You should try
echo "<a target='_blank' href='".random_image('css/avatars')."'>"."<img src=" . random_image('css/avatars') . " /></a>";
I think this can help you
The problem is that you are not properly concatenating your string with what you get from random_image function.
You can try following code, here I am assuming that your random_image function returns complete path to your image
function random_image($path)
{
// returning dummmy image
return "https://images.unsplash.com/photo-1535498730771-e735b998cd64?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=623ed05afcc5a3fa1e444ffca95854dc&w=1000&q=80";
}
echo "<a href='".random_image('css/avatars')."' target='_blank'> <img src='".random_image('css/avatars')."' /></a>";

How to make link of the database in PHP

Hi I want to show URL as a link in PHP the URL is shown by query from database but it is not a link so I want to make it link like using but I don't know what I am doing wrong
My data show like this in browser
ID Name URL
2 This localhost/p_uploads/00.jpg
3 Nissan localhost/p_uploads/7a.jpg
I want these URL's to be link so anyone can click on the url to open the image
Here is my PHP Code:
<?php
if(!isset($_COOKIE['loggedin'])){
header("location:index.php");
}
session_start();
if(!isset($_SESSION['user'])){
header("location: index.php");
}
else {
?>
<?php
$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"Select * from private_uploads where username = '".$_SESSION['user']."'")
or die(mysql_error());
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>URL</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
echo "</tr>";
}
echo "</table>";
//Views Counter
mysqli_close($con);}
?>
<?php
$url = htmlspecialchars($_SERVER['HTTP_REFERER']);
echo "<a href='$url'>back</a>";
?>
replace
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
for this
echo '<td>[a name here would be nice]</td>';
you are missing the "" in the generated href
You're producing links that look like this:
That's both an invalid URL (unless you really do have a localhost/p_uplaods folder) and a link with no body, so you'll never see it.
If you want to access localhost the host, and not the folder, you should use use an absolute path on the current host, /p_uploads/00.jpg, or a fully qualified URL: http://localhost/p_uploads/00.jpg.
Your link looks like this:
It doesn't have a HTTP protocol. The link should look like this:
You might have a problem on this line:
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
As you are referring to Link whereas in your database the column is called URL. Also add a text to the tag:
echo '<td>View</td>';

Display image in php

how to display image in html by retrieving it from mysql database.
i have the following code but only text is being displayed in place of image.
How to display the image instead of the text of image.
Code:
while ($row = mysql_fetch_assoc($result))
{
//echo "<div>";
//echo "<div class=\"slide-image\">";
print $row['Image'];
//echo "</div>";
echo "<div class=\"slide-text\">";
echo $row['Head'];
echo $row['Description'];
//echo "</div>";
//echo "</div>";
}
It displays the resulting JPEG as text, which as you might imagine is very difficult to interpret.
You have to set the Content-Type header with PHP. If it is JPEG it will be image/jpeg. You can set it with:
header('Content-Type: image/jpeg');
Be sure to set that header before outputting any data.
update
But in your file as you are outputting image data and text data from same document you cant set its header type as multiple types.
one option would be
while ($row = mysql_fetch_assoc($result))
{
echo "<div>";
echo "<div class=\"slide-image\">";
echo "<img src='image_render.php?id=".$row['some_key_for_record']."'";
//print $row['Image']; we are moving it to an external page
echo "</div>";
echo "<div class=\"slide-text\">";
echo $row['Head'];
echo $row['Description'];
echo "</div>";
echo "</div>";
}
and in image_render.php
//code to pull data from db according to $_GET['id'];
header('Content-Type: image/jpeg');
print $row['Image'];
//no text data to be output from here
If you are stroing the image source(full path) in your MYSQL table then you can able to print the html img tag inside your php like a html div. You can do it by this way,
while ($row = mysql_fetch_assoc($result))
{
//echo "<div>";
//echo "<div class=\"slide-image\">";
echo '<img src='".$row['Image']."' width="100" height="100" />';
//echo "</div>";
echo "<div class=\"slide-text\">";
echo $row['Head'];
echo $row['Description'];
//echo "</div>";
//echo "</div>";
}
Note :
If you want only the image to be displayed in your web page then you can do it with header('Content-Type: image/jpeg');

How to Displaying an image with path stored in Database?

I have stored images path in database and images in project folder. Now i am trying to view all IMAGES with their PRODUCTS in my HTML template. I have written the following code and its given me an empty images box in the output and when i open that image box in the new tab, it opens the following URL.
http://localhost/cms/1
Kindly tell me the way of referring images in 'img src ' tag.
include 'connect.php';
$image_query = "select * from product_images";
$image_query_run = mysql_query($image_query);
$image_query_fetch = mysql_fetch_array($image_query_run);
if (!$query=mysql_query ("select product_id,name from products")) {
echo mysql_error();
} else {
while ($query_array = mysql_fetch_array($query)) {
echo '</br><pre>';
$product_id = $query_array['product_id'];
echo "<a href='single_product.php?product_id=$product_id' >";
print_r ($query_array['name']);
echo "</a>";
echo "<img src=". $image_query_fetch
['images'] ." alt=\"\" />";
echo '</pre>';
}
}
} else {
echo "You need to Log in to visit this Page";
}
Add your local image path before source
example
echo "<img src='http://localhost/cms/1/". $image_query_fetch['images'] .'" alt=\"\" />";
*Use PHP *
<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
You can use the HTML base tag to set a base URL that relative URLs will be resolved from.
See -> http://www.w3schools.com/tags/tag_base.asp

Display an Image from PHP mysql table

I have a PHP script to echo the contents of a Mysql table.
I'm using it as a small CMS for a static page.
What I want to know is how can I go about displaying an Image in my PHP script.
I have a form that submits the Date, Title, Message, and Image.
I'm using the Image field to insert the URL of an image.
Whats the correct way of displaying the image on a page.
Below is my code:
<?php
$con = mysql_connect("localhost","name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databaseName", $con);
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $row['Date'];
echo "<br />";
echo $row['Title'];
echo "<br />";
echo $row['Message'];
echo "<br />";
echo $row['Image'];
echo "<br />";
echo "<br />";
echo "<br />";
}
mysql_close($con);
?>
This is the outcome:
17th Feb
Title
Here is my Message
http‍://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
18th Feb
Title
Here is my Message
http‍://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
but I want the page to display the image not the URL.
I'm guessing just using the <img></img> tags, but I'm not sure where in the PHP.
You can write code in image tag instead of echo directly.
Try this code.
echo "<img src='".$row['Image']."'/>";
Try this code, but give image folder path perfectly
<?php
$imagepath = "../uploads/";
echo "<img src='".$imagepath.$row['Image']. "' alt='' height='200' width='200' /> ";
?>
According to need , you can change height and width of the image. but clarity differs.
Thanks
echo $row['Image'];
replace like this
echo '<img src="'.$row['Image'].'" alt="" />';

Categories