No images displayed on the page after querying the database PHP mysql - php

This is my first post on stackoverflow after following posts for a very long time.
My problem is, I have a function that is supposed to retrieve data from a table (models) and display a name, location and an image to one of the pages. Everything is working but the images don't get displayed. Please look at my code and tell me what I should change to get these images to display. I'm using twitter bootstrap for styling.
My directory is: localhost/mainsite/admin/uploads
I've checked everything related to this post and tried it out but they didn't solve my problem. So please help.
function view_models() {
global $dbc;
$q = "SELECT * FROM models";
$r = mysqli_query($dbc, $q);
while ($row = # mysqli_fetch_array($r)) {
$id = $row['model_id'];
$mn = $row['model_name'];
$ln = $row['location'];
$img = $row['image1'];
echo "
<div class='col-xs-6 col-lg-3'>
<h3>$mn</h3>
<a href='details.php?id=$id'>
<img src='admin/uploads/$img' class='img-thumbnail' />
<p>$ln</p>
</div>";
}
}

Try this.,
I have changed the echo statement because you did a error, that was misplaced '&"
echo "<div class='col-xs-6 col-lg-3'>
<h3>".$mn."</h3>
<a href='details.php?id=".$id."'>
<img src='admin/uploads/".$img."' class='img-thumbnail' />
<p>".$ln."</p>
</a>
</div>";

Related

How to select an image from retrieved data

I'm writing an html code to retrieve the details of the item table in my database.In it, I have some columns and one such column name is Image.It stores images of the Items and it's type is blob.I have written the following code to retrieve and display the data in this table in my php file.
$sql = "SELECT * FROM item where item.Category = 'Tops' limit 3";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$id = $row["Item_ID"];
echo " <div class='ite'>
<div class='card'>
<img src=".$row["Image"]." alt=".$row["Item_Name"]." class='imge'>
<h3>".$row["Item_Name"]."</h3>
<p class='price'>Rs. ".$row["Price"]."</p>
<p><button type='submit' onclick='view_data($id)'>View Item</button></p>
</div>
</div>";
}
}
The code works perfectly for the other columns except the image column(<img src=".$row["Image"]." alt=".$row["Item_Name"]." class='imge'>)
The output is as follows.
It gives garbage values like this.How can I fix this error and display the image stored in the database?
<img src='data:image/jpeg;base64," . base64_encode($row['Image']) . "' class='imge'>
This works!Thanks for the support!

how to display a photo from database base on its id to another page

I have a huge problem and been having a hard time figuring it out.I want to pass down the image id i click on onto the 2nd page.The 2nd page then uses the id and echos out an image,image_text based on what i gave in the database.
1st page
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='img-single'>
<a href='images/fpage.php?image_id={$row['image_id']}'><img class='i'
src='images/".$row['image']."' ></a>
<figcaption class='figcaption'>".$row['image_text']."</figcaption>
</div>";
}
?>
2nd page
<?php
$image_id = $_GET['image_id'];
?>
So after passing the image id in a a href='somepage.php&image_id=$image' tag. On the 2nd page use: if (isset($_GET['image_id'])) { $pic_id = $_GET['image_id'] Then make a select from database using pic_idmysqli_query($dbc, "SELECT picture, name from table_name WHERE picture_id = '$pic_id' LIMIT 1;"); echo the picture in image tags and your done.
<img src="<?php echo $picture; ?>" alt="<?php echo $name; ?>">} // Close if(issset()) I hope this helps.

hiding content when variable is not present

I am using php to publish news articles from my mysql database. The articles have images placed within their text. The images are stored as variables, each its own path. The problem is I don't always use the full complement of images for each article, and the way I've written it, a broken image displays.
In short, how can I keep broken images from showing up if say '$image_ii' or '$image_iii' does not exist for a specific ID.
The way I have it written out is as follows:
<?php
include ('connect.php');
$friendly_url=$_GET['friendly_url'];
$query = "SELECT * FROM entries WHERE friendly_url='$friendly_url'";
$entries = mysql_query($query);
while($row = mysql_fetch_array($entries, MYSQL_ASSOC))
{
$title = $row['title'];
$author = $row['author'];
$pub_date = $row['pub_date'];
$content_i = $row['content_i'];
$content_ii = $row['content_ii'];
$content_iii = $row['content_iii'];
$content_iv = $row['content_iv'];
$image_i = $row['image_i'];
$image_ii = $row['image_ii'];
$image_iii = $row['image_iii'];
$image_caption_i = $row['image_caption_i'];
$image_caption_ii = $row['image_caption_ii'];
$image_caption_iii = $row['image_caption_iii'];
$pre_link = $row['pre_link'];
$post_link = $row ['post-link'];
$id = $row['id'];
$friendly_url = $row['friendly_url'];
$american_date = date("F d, Y", strtotime($pub_date));
$friendly_content_i = nl2br($content_i);
$friendly_content_ii = nl2br($content_ii);
$friendly_content_iii = nl2br($content_iii);
$friendly_content_iv = nl2br($content_iv);
echo "
<div id='post'>
<div id='post-title'>$title</div>
<div id='post-content'
$friendly_content_i
<div id='media'> <img src='../$image_i' class='scale-image'> </div>
<div id='media-caption'> $image_caption_i </div>
$friendly_content_ii
<div id='media'> <img src='../$image_ii' class='scale-image'> </div>
<div id='media-caption'> $image_caption_ii </div>
$friendly_content_iii
<div id='media'> <img src='../$image_iii' class='scale-image'> </div>
<div id='media-caption'> $image_caption_iii </div>
$friendly_content_iv
</div>
<div id='post-footer'> by $author <br> $american_date </div>
</div>
</div> ";
} mysql_close($connection); ?>
Apologies if this is terrible, it's my first dynamic website.
Try using the following snippet
if (file_exists("../".$image_iii)) {
echo "<img src='../$image_iii' class='scale-image'>";
} else {
echo "<img src='../default-image' class='scale-image'>";
}
I ended up finding a solution that worked using:
if (!empty($image_iii)) { echo "<img src='../$image_iii' class='scale-image'>"; }
else { echo "";}
That said, clearly you kind folks are right about me needing to swap over to mysqli...found this website as a good resource for migrating code to mysqli. Thanks to everyone who responded, everyones comments were helpful.
You already told us the solution to your problem:
if say '$image_ii' or '$image_iii' does not exist for a specific ID.
Use if file_exists() to check that your image exists, and don't output it, if it doesn't.
Furthermore I see you're using mysql_query - This is deprecated and very unsafe. You should use mysqli or PDO instead

image display, php mysql

I'm developing a project Personl and I have encountered a problem.
Details my of problem
Perform a query via a form, which calls me to another page where the query is displayed, everything is correct, it displays the information that I need, but when viewing the user's image, calling me by request the ID corresponding to this user, but not my image code is displayed below.
Code to display the image in html:
<img src="../../registro/imagen.php?matricula=<?php echo $_POST['matricula']; ?>" width="150px" height="150px" />
Code search the image by id to display:
<?php
$numero=$_REQUEST['matricula'];
$tabla="alumno";
include("../../Connections/colegio.php");
$conexion=#mysqli_connect($hostname_colegio,$username_colegio,$password_colegio,$database_colegio);
$sacar = "SELECT * FROM ".$tabla." WHERE (matricula=$numero)" ;
$resultado = mysqli_query($conexion,$sacar);
while ($registro = mysqli_fetch_assoc($resultado))echo mysqli_error( $conexion );{
$tipo_foto=$registro['matricula'];
header("Content-type: image/jpg");
echo $registro['matricula'];
}
mysqli_close($conexion);
?>
may terminate this issue, if it helps someone, I leave the correct code.
<?php
$numero=$_REQUEST['matricula'];
$consulta="select * from alumno WHERE (matricula = $numero)";
$busca_fotos=mysql_query($consulta) or die("Error en: $busca_fotos:" .mysql_error()) ;
while($ro=mysql_fetch_assoc($busca_fotos)){
$url=$ro['matricula'];
echo "
<img src=\"../../registro/fotos/".$url.".jpg\" width=\"150\" height=\"150\" alt=\"\" />
</a>
";
}
?>

error outputting html with javascript

I have this php code, with which I am trying to generate a popup window that will contain the contents of a html file, however after adding in the script tags, no html is displayed. I tried echoing out $row2, but the word array is printed to the screen and nothing else.
<?php
session_start();
if (isset($_GET["cmd"]))
$cmd = $_GET["cmd"];
else
die("You should have a 'cmd' parameter in your URL");
$pk = $_GET["pk"];
$con = mysql_connect("localhost","root","geheim");
if(!$con)
{
die('Connection failed because of' .mysql_error());
}
mysql_select_db("ebay",$con);
if($cmd=="GetAuctionData")
{
$sql="SELECT * FROM Auctions WHERE ARTICLE_NO ='$pk'";
$sql2="SELECT ARTICLE_DESC FROM Auctions WHERE ARTICLE_NO ='$pk'";
$htmlset = mysql_query($sql2);
$row2 = mysql_fetch_array($htmlset);
echo $row2;
echo '<script>
function makewindows(){
child1 = window.open ("about:blank");
child1.document.write('.$row2["ARTICLE_DESC"].');
child1.document.close();
}
</script>';
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
echo "<div id='leftlayer'>
<strong>Article Number</strong> ".$row['ARTICLE_NO']."
<p><strong>Article Name</strong></p> ".$row['ARTICLE_NAME']."
<p><strong>Subtitle</strong></p> ".$row['SUBTITLE']."
<p><strong>Username</strong></p> ".$row['USERNAME']."
<p><strong>Total Selling</strong></p> ".$row['QUANT_TOTAL']."
<p><strong>Total Sold</strong></p> ".$row['QUANT_SOLD']."
<p><strong>Category</strong></p> ".$row['CATEGORY']."
<p><strong>Highest Bidder</strong></p> ".$row['BEST_BIDDER_ID']."
</div>
<div class='leftlayer2'>
<strong>Current Bid</strong> ".$row['CURRENT_BID']."
<p><strong>Start Price</strong></p> ".$row['START_PRICE']."
<p><strong>Buyitnow Price</strong></p> ".$row['BUYITNOW_PRICE']."
<p><strong>Bid Count</strong></p> ".$row['BID_COUNT']."
<p><strong>Start Date</strong></p> ".$row['ACCESSSTARTS']."
<p><strong>End Date</strong></p> ".$row['ACCESSENDS']."
<p><strong>Original End</strong></p> ".$row['ACCESSORIGIN_END']."
<p><strong>Auction Type</strong></p> ".$row['AUCTION_TYPE']."
</div>
<div class='leftlayer2'>
<strong>Private Auction</strong></p> ".$row['PRIVATE_AUCTION']."
<p><strong>Paypal Accepted</strong></p> ".$row['PAYPAL_ACCEPT']."
<p><strong>Auction Watched</strong></p> ".$row['WATCH']."
<p><strong>Finished</strong></p> ".$row['FINISHED']."
<p><strong>Country</strong></p> ".$row['COUNTRYCODE']."
<p><strong>Location</strong></p> ".$row['LOCATION']."
<p><strong>Conditions</strong></p> ".$row['CONDITIONS']."
</div>
<div class='leftlayer2'>
<strong>Auction Revised</strong></p> ".$row['REVISED']."
<p><strong>Cancelled</strong></p> ".$row['PRE_TERMINATED']."
<p><strong>Shipping to</strong></p> ".$row['SHIPPING_TO']."
<p><strong>Fee Insertion</strong></p> ".$row['FEE_INSERTION']."
<p><strong>Fee Final</strong></p> ".$row['FEE_FINAL']."
<p><strong>Fee Listing</strong></p> ".$row['FEE_LISTING']."
<p><a href='#' onclick='makewindows(); return false;'>Click for full description </a></p>
</div>";
$lastImg = $row['PIC_URL'];
echo "<div id='rightlayer'>Picture Picture
<img src=".$lastImg.">
</div>";
}
}
mysql_close($con);
?>
edit: I have fixed the errors that Roborg pointed out, however the script will still not load and does not give a precise error.
i have updated the code above
As well as the missing </script>,
child1.document.write('.$row2["ARTICLE_DESC"].')
should be
child1.document.write(' . json_encode($row2["ARTICLE_DESC"]) . ');
The json_encode() function will take care of any quoting for you.
Edit:
<a href='#' onclick=makewindows()> should be <a href='#' onclick='makewindows(); return false;'> - You should have quotes there, and the return false will stop you getting taken to "#" when you click the link.
Also, from memory I'm not sure you can open about:blank and then write to it - I think it sees that as cross-domain scripting. You might be better off creating a minimal "blank.html" file on your server and using that.
You have to print_r an array, like print_r($row2);
In this line:
$row2 = mysql_fetch_array($htmlset);
You are setting $row2 to be an array representing an entire row of the result from your query. Even if the "row" is just one field, it's still an array. If you want to get just the value of the first field, you can use mysql_result.
The parameters are: resource $result , int $row [, mixed $field ] so an example of the usage would be:
// get the first field of the first row
$fieldVal = mysql_result($htmlset, 0);
// get the third field
$fieldVal = mysql_result($htmlset, 0, 2);
// get the first field of the 2nd row
$fieldVal = mysql_result($htmlset, 1);
Your <script> tag is never closed and your JavaScript instructions are not ended with semicolons. It might be the source of the problem.

Categories