Image not display using PHP and MySQL - php

Below is my code for display information of an image including its file, but this is not displaying, I don't know why.
<?php
include('config/dbconnect.php');
if(isset($_GET['id']))
{
$id = $_GET['id'];
$sql = mysqli_query($con, "SELECT * FROM collage WHERE id = '$id'");
while($row = mysqli_fetch_array($sql)) {
$name = $row['name'];
$content = $row['content'];
$size = $row['size'];
$type = $row['type'];
$date_upload = $row['date_upload'];
$file = $row['file'];
}
}
?>
<div class='body-content'>
<div class='img-name-cont'><h3><?php echo $name; ?></h3></div>
<div class='img-detail-cont'>upload: <?php echo $date_upload; ?><br>type: <?php echo $type; ?><br>size: <?php echo $size; ?>KB </div>
<div class='img-file-cont'>
<img src="img/collage/<?php echo $row['file'] ?>" style="width:130px; height:100%"></div>
<div class='img-content-cont'><?php echo $content; ?></div>
</div>

First of all use inspect to check whether the image is actually pointed to by the code. that is the actual loation might not be pointed at. what is your folder structure.
" style="width:130px; height:100%">
your code is ok. as long as php echo $row['file'] returns a file

The problem here is "Your all the variables are local to the while loop". so you can't access it outside of the while loop. Try this code.
<?php
include('config/dbconnect.php');
if(isset($_GET['id']))
{
$id = $_GET['id'];
$sql = mysqli_query($con, "SELECT * FROM collage WHERE id = '$id'");
while($row = mysqli_fetch_array($sql)) {
?>
<div class='body-content'>
<div class='img-name-cont'><h3><?php echo $row['name']; ?></h3></div>
<div class='img-detail-cont'>upload: <?php echo $row['date_upload']; ?><br>type:
<?php echo $row['type']; ?><br>size: <?php echo $row['size']; ?>KB </div>
<div class='img-file-cont'>
<img src="img/collage/<?php echo $row['file']; ?>" style="width:130px; height:100%"></div>
<div class='img-content-cont'><?php echo $row['content']; ?></div>
</div>
<?php
}
}
?>

Be sure to check echo $row['file'] if it returns the correct string of your image path with the correct file extension .

try this one :
first of all you have to check if your image path ok or not using this php function:
<?php
$img=getimagesize($imagewithpath);
if($img=="")
{ echo "there is no image ";}
else{ ?> <img src="<?php echo $imagewithpath; ?>" style="width:130px; height:100%"> <?php } ?>

As answered by Dulaj Sanjaya all your variables are local so you can't use that outside of while.
Second if you want to use your code then you have taken all data of $row in different variables then used outside of file then why don't you used same $file for image why you have used $row['file']?
Simply replace this line as
<div class='img-file-cont'>
<img src="img/collage/<?php echo $file; ?>" style="width:130px; height:100%"></div>

Related

Returning SQL data within an image tag using PHP

When I run the following file I get the database data i.e it prints it out on the website so I know my connections are good.
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo $row["name"], $row["image"];
}
?>
</div>
</html>
However when I try and format the results like below
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo <div id = "bookbar">
<img src= "$row['image']" alt = "image">
<p> $row['name'] </p>
</div>
}
?>
</div>
</html>
it doesn't work. Can anyone help me fix the code?
Maybe try this your code didn't close/open the php tags properly also don't echo like that
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<div id = "bookbar">
<img src= "<?php echo $row['image'] ?>" alt = "image">
<p><?php echo $row['name']; ?></p>
</div>
<?php
}
}
$conn->close();
?>
If you want to echo something out
Its better to close on open the php tags like so
PHP goes here
?>
HTML goes here
<?php
PHP goes here
And if you want to echo something inside the HTML just do this
<span> <?php echo "something" ?> </span>
much easier and makes the code easier to read.
change your echo statement to -
echo '<div id = "bookbar"><img src= "' . $row['image'] . '" alt = "image"><p>'. $row['name'] .'</p>'
Your issue is a syntax problem - you can't use echo like that, it has to echo a string variable. You should be seeing an error message about it.
You could keep the echo statement and put all the HTML inside a string, and concatenate (or interpolate) the PHP data into it. But IMO the easiest thing here in terms of readability and maintenance is to step out of the PHP tags, print the HTML, embed some PHP tags in it for the variables, and then step back in again to continue with the code. It makes the HTML far easier to understand:
?>
<div id="bookbar">
<img src="<?php echo $row['image'] ?>" alt="image">
<p><?php echo $row['name'] ?></p>
</div>
<?php
When you are in php mode you should echo strings as php variables wrapped with single quotes:
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<div id = "bookbar">';
echo '<img src="' . $row['image'] . '" alt = "image">';
echo '<p>' . $row['name'] . '</p>';
echo '</div>';
}

Using htmlspecialchars() in php/html the correct and professional way

I would like to know the correct way to use htmlspecialchars()
I have been reading about it and looking at what examples I can find but
I guess its just not registering because I have not been able to apply it my self in my own working example.
Could someone show me how to implement htmlspecialchars() and any other appropriate configuration to make this statement secure and what would be considered professional.
<h3>Recent Post</h3>
<?php
$stmt = $con->query('SELECT * FROM blogData ORDER BY id DESC');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$title = $row['title'];
$content = $row['content'];
$category = $row['category'];
?>
<div class="features">
<div class="box"><img src="Developer/common-files/icons/time#2x.png" width="100" height="100" alt="Wuno Inc.">
<h6><?php echo $category; ?> - <?php echo $title; ?></h6>
<p><?php echo $content; ?></p>
</div>
</div>
<?php
}
?>
</div>
Is this how it should be done? Or what more could I do to this.
<h3>Recent Post</h3>
<?php
$stmt = $con->query('SELECT * FROM blogData ORDER BY id DESC');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$title = $row['title'];
$content = $row['content'];
$category = $row['category'];
?>
<div class="features">
<div class="box"><img src="Developer/common-files/icons/time#2x.png" width="100" height="100" alt="Wuno Inc.">
<h6><?php echo htmlspecialchars($category); ?> - <?php echo htmlspecialchars($title); ?></h6>
<p><?php echo htmlspecialchars($content); ?></p>
</div>
</div>
<?php
}
?>
Yes, that is how it should be done. But you could also put it in the code above. Like
$title = htmlspecialchars($row['title']);
$content = htmlspecialchars($row['content']);
$category = htmlspecialchars($row['category']);
This way the variables used between your HTML stay short and readable.

Search results not showing

I have a problem with my search.php file to render me results...
I dont get any strings of error, but when I type an existing keyword I get no results...
The format of the results are the same as viewed in my main content on the website (grid view)...
The code:
<body>
<?php include_once("analyticstracking.php") ?>
<div class='container'> <!--Start of the container-->
<div><?php include("includes/header.php"); ?></div>
<div><?php include("includes/navbar.php"); ?></div>
<div><?php include("includes/left_col.php"); ?></div>
<div class='main_col'>
<div class='main_content'>
<?php
include("includes/connect.php");
if(isset($_GET['search'])){
$search_id = $_GET['q'];
$search_query = "SELECT * FROM games WHERE game_keywords LIKE '%$search_id%'";
$run_query = mysql_query($search_query);
echo '<table>';
$games = 0;
while($search_row = mysql_fetch_array($run_query)){
// make a new row after 9 games
if($games%9 == 0) {
if($games > 0) {
// and close the previous row only if it's not the first
echo '</tr>';
}
echo '<tr>';
}
// make a new column after 3 games
if($games%3 == 0) {
if($games > 0) {
// and only close it if it's not the first game
echo '</td>';
}
echo '<td>';
}
$game_id = $search_row['game_id'];
$game_name = $search_row['game_name'];
$game_category = $search_row['game_name'];
$game_keywords = $search_row['game_name'];
$game_image = $search_row['game_image'];
?>
<div class="game_grid">
<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" height="120" />
<span><?php echo $game_name; ?></span>
</div>
<?php
$games++;
}
}
?>
</table>
</div>
</div>
<div><?php include("includes/footer.php"); ?></div>
</div> <!--End of the container-->
</body>
Any idea?
EDIT:
I solved my problem, its a small mistake I made,
In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidently... now everything works perfectly :)
You have a typo in code
change code as below
if(isset($_GET['search'])){
$search_id = $_GET['search']; //$_GET['q'];
.
.
.
}
I solved my problem, its a small mistake I made, In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidentally... now everything works perfectly :)

PHP: HTML Tags Not Rendering

Hope y'all can help!
I'm using a PHP driven link directory program and, fo some reason, the summary descriptions recorded to my MySQL database and being generated, BUT the HTML tags won't render on the page. I've included the code below and here's an http://abiautism.com/autism-resources/?page=pages&id=2 Any help would be greatly appreciated!
<?php if ($rowpages[IMAGENAME] != "") { ?>
<?php if ($rowpages[WIDTH] == '') $rowpages[WIDTH] = '128px'; ?>
<?PHP
$filename = "$svr_rootscript/product_images/$rowpages[IMAGENAME].jpg";
if (file_exists($filename)) $ext = jpg;
$filename = "$svr_rootscript/product_images/$rowpages[IMAGENAME].png";
if (file_exists($filename)) $ext = png;
?>
<?PHP if ($ext != '') { ?>
<img class="photo" src="product_images/<?php echo $rowpages[IMAGENAME]; ?>.<?php echo $ext; ?>" width="<?php echo $rowpages[WIDTH]; ?>">
<?PHP } ?>
<?PHP } ?>
<h1><?php echo $rowpages[HEADER]; ?></h1>
<h3><?php $description = preg_replace("/\n/","\n<BR>",$rowpages[CAPTION]); ECHO $description; ?></h3>
<?PHP if ($rowpages[SHOWADS] != '') { ?>
<div align ="center"><?php echo $rowxxx[ADS]; ?></div>
<?PHP } ?>
<b><p><? echo $pages_vst; ?></p></b>
<?php $description = preg_replace("/\n/","\n<BR>",$rowpages[PAGECONTENT]); ECHO $rowpages[PAGECONTENT]; ?>
You can use this
echo htmlspecialchars_decode($content_html);
This change the < to <

:nth child not working in jquery

I am trying to insert a <br clear="all" /> after each fifth div. Below is how I create the divs in php and then I try to use jquery to seperate them. But it is not working! Any help would be greatly appreciated! Thanks in advance!
PHP:
$get = mysql_query("SELECT * FROM Products $cate ") or die(mysql_error());
while($row= mysql_fetch_array($get)){
$pname = $row['Pname'];
$image = $row['Pimage'];
$id = $row['ID'];
?>
<div class="productCat"><img src="../products/<?php echo $image ?>" width="100" height="100" /><br /><?php echo $pname ?></div>
<?php
}
?>
JQUERY:
$(".productCat :nth-child(5)").append("<br clear='all'/>");
There isn't supposed to be a space after .productCat.
Also, why not just do it in the PHP? Just add a counter:
$count=0;
while($row= mysql_fetch_array($get)){
$count++;
$pname = $row['Pname'];
$image = $row['Pimage'];
$id = $row['ID'];
?>
<div class="productCat"><img src="../products/<?php echo $image ?>" width="100" height="100" /><br /><?php echo $pname ?></div>
<?php
if($count%5==0)
echo "<br clear='all'/>";
}
?>
put the space out between class selector and nth-child :
$(".productCat:nth-child(5)").append("");
The problem is that it's looking for the multiple-of-five-th children of .productCat, because of the extra space. EDIT: Actually, it's only looking for the 5th child of .productCat, because you left the n out.
Besides, what's wrong with:
/* CSS: */
.productCat:nth-child(5n+1) {clear: left;}
This CSS-only solution is far more efficient.
Why not doing it in PHP ? If you use jQuery, people without javascript won't have your "br" ...
<?php
$get = mysql_query('SELECT * FROM Products '.$cate) or die(mysql_error());
$i=0;
while($row = mysql_fetch_array($get)){
$i++:
?>
<div class="productCat"><img src="../products/<?php echo $row['Pimage']; ?>" width="100" height="100" /><br /><?php echo $row['Pname']; ?></div>
<?php
if($i == 5){
echo '<br clear="all" />';
$i=0;
}
}
?>

Categories