How to fetch images from database in base64_encode in php - php

Thank you all in advance....
I am doing a project in which I have a field to store the image. In that form, the image uploaded is completed with croppie plugin. and I stored the data in base 64 encodings. But I cannot fetch it to a page called view.php but the images are fetched to the form after cropping.
Please help me to figure it out the mistake that I have done
<tbody>
<?php
$no = 1;
$data = mysqli_query($con, "SELECT * FROM `register` WHERE app_registration IS NULL ORDER BY `app_id` DESC ");
while ($row = mysqli_fetch_assoc($data)) {
?>
<tr>
<td><?php echo $row['app_id'] ?></td>
<?php if (!empty($row['image_reference_id'])) {
$data1 = mysqli_query($con, "SELECT * FROM `photo_table` WHERE image_unique_id = '" . $row['image_reference_id'] . "'");
$row1 = mysqli_fetch_assoc($data1)
?>
<td><img src="data:image/jpg;base64,'.base64_encode($row['images']).'"/></td>
<?php } else {
?>
<td><img src="../images/<?php echo $row['app_image'] ?>" style="width: 100px; height: 100px;"></td>
<?php } ?>
<td><?php echo $row['app_name'] ?></td>
<td><?php echo $row['app_mobile_no_1'] ?></td>
</tr>
<?php } ?>
</tbody>
Note: In db the images are in Long Blob

You will need to change the following:
<img src="data:image/jpg;base64,'.base64_encode($row['images']).'" />
to
<img src="data:image/jpg;base64, <?php base64_encode($row['images']);?>" />

Related

File view to the another tab (PHP)

I have inserted a file with an UPDATE command because I want my files (pdf, jpg) to be in one table together with name, course, etc. and and I just want to know if it it is possible that instead of showing the name of the filename in the table, its filename would be an href on when the person have click it, it would go in the another tab to show is content (pdf, or jpeg).
Here is my code of my addrequirements.php
<?php
include_once 'connection.php';
$result = mysqli_query($conn,"SELECT * FROM collegestud");
$result1 = mysqli_query($conn,"SELECT * FROM shsstud");
session_start();
if(!$_SESSION['login']){
header("location:index.php");
die;
}
?>
<div id="ccc">
<?php
if (mysqli_num_rows($result) > 0) {
?>
<table id="example" class="display" style="width:100%;">
<thead>
<tr>
<th>Student Id</th>
<th>Course</th>
<th>Last Name</th>
<th>First Name</th>
<th>Psa</th>
<th>Form137</th>
<th>2x2 Picture</th>
<th>Actions</th>
</tr>
</thead>
<tbody id='cccc'>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["Stud_ID"]; ?></td>
<td><?php echo $row["Course"]; ?></td>
<td><?php echo $row["Lastname"]; ?></td>
<td><?php echo $row["Firstname"]; ?></td>
<td><?php echo $row["PSA"]; ?></td>
<td><?php echo $row["Form137"]; ?></td>
<td><?php echo $row["Picture"]; ?></td>
<td><a style="color:green;" href="updatecollege1.php?id=<?php echo $row["ID"]; ?>"><i class="fas fa-folder-plus fa-2x "></i></a> 
<span><a style="color:red" href="deletecollege.php?ID=<?php echo $row["ID"]; ?>"onclick="return confirm('Do you really want to delete this student?')"><i class="fas fa-user-minus fa-2x"></i></a></span>
</td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
<?php
}
else
{
echo "<p><center><strong>0 RESULTS IN THE TABLE</center></p></strong>";
}
?>
Code of my updatecollege1.php
<?php
include_once 'connection.php';
session_start();
if(!$_SESSION['login']){
header("location:index.php");
die;
}
$result = mysqli_query($conn,"SELECT * FROM collegestud WHERE id='" . $_GET['id'] . "'");
$row= mysqli_fetch_array($result);
?>
<div class = "container3">
<strong><left>PSA   </left></strong>
<span>   <input id="ra" type="text" value="" readonly>ADD or UPDATE</span><br>
Code of my psaadd.php
<?php
include_once 'connection.php';
session_start();
if(!$_SESSION['login']){
header("location:index.php");
die;
}
if(count($_POST)>0) {
mysqli_query($conn,"UPDATE collegestud
set PSA = '" . $_POST['PSA'] . "'
WHERE ID='" . $_POST['ID'] . "'");
$id = $_POST['ID'];
header('Location: updatecollege1.php?id='.$id);
}
$result = mysqli_query($conn,"SELECT * FROM collegestud WHERE id='" . $_GET['id'] . "'");
$row= mysqli_fetch_array($result);
?>
<form action="" method="POST" onsubmit="return confirm('Are you sure you want to save changes?');">
<div class= "container2">
<p style="font-weight: bold; font-size: 35px; text-align: center;">PSA</p>
<input type="hidden" name="ID" class="txtField" value="<?php echo $row['ID']; ?>">
<div class = "container3">
<center>
<input name="PSA" type = "file" accept = '.pdf'>
</center>
PROBLEM:
I just want to have a solution that instead it displays the filename of that file (text), it will show its filename in the table that is clickable to open its file in the another tab.
Yes, of course it possible. You should save also relative path to that PDF in database and then show link in UI part:
<!-- Start of table -->
<td>
Click to view
</td>
And your database table will have 1 additional field named path VARCHAR(256) NOT NULL with value e.g. students/some_name_of_file.pdf
Never store file content to database, it's not build to hold file contents

display type BLOB as a link

I want to display records in the website and the $row['data'] of the table report its type (BLOB) in a database and I want to display it as a link but it doesn't appear anymore.
What Can I do ? Whats the problem here?
<?php
include 'connect.php';
extract($_SESSION);
session_start();
?>
<html>
<body >
<table>
<?php
$sql="SELECT data,report.StudID,studFName,StudLName
FROM report,student
WHERE report.SuperID={$_SESSION['supervisor']} AND
report.StudID=student.StudID ";
$result= mysqli_query($con,$sql) or die ("could not found;
".mysqli_error($con));
while ($row=mysqli_fetch_array($result) )
{
?>
<tr>
<td><?php echo '<a href="data:application/pdf;base64,'.base64_encode($row ['data']).' " height="20" width="20" />'?></td>
<td><?php echo $row['StudLName'] ?></td>
<td><?php echo $row['studFName'] ?></td>
<td><?php echo $row['StudID'] ?></td>
<?php
}
?>
</tr>
</table>
Your link won't appear because you need to enclose it with </a> and specify text in it.
<?php
echo '<a href="data:application/pdf;base64,'.base64_encode($row ['data']).' " />Pdf Link</a>'
?>
I don't think you need to specify height and width for link.

Displaying an image in php

how to display blank if there is no image in a record.As i have inserted a record in database without an image but while fetching an record it is displaying an blank image in front end.It should not show any image if there is no image.Here is my code. If there is no image it should show only description.
Blogimage.php
<tbody>
<?php include "blogs.php" ;
while($row = mysql_fetch_array($result))
{?>
<tr>
<td><img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/></td>
</tr>
<tr>
<td><?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?></td>
</tr>
<?php }?>
</tbody>
Blogs.php
$id=$_GET['title'];
$res = "SELECT * FROM blogs
WHERE blog_title='$id'";
$result=mysql_query($res);
try changing this
<img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/>
to this
<?php if($row['image']) echo "<img src='admin/upload/".$row['image']."' height='100' style='width:60%;height:50%;' />"; ?>
A couple of general points before we go into this
1) The MySql library you are using is old and busted - you should be using something newer - read this - the options are Mysql PDO and MySqli - I promise you, you will be glad you did, i use IDIORM as an interface and find it very good (Also helps prevent stuff in point 2 below!)
2) Your code can be SQL injected - more about that here - this is very bad :)
Below is an example, I have fixed the SQL injection problem, and put in some logic that would test to see if there is an image, if not, it will dump out a 'noimage.jpg' src value.
I have removed the include, and just simply put the code for blogs.php inline.
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$iamgeSrc = 'admin/upload/' . $row['image'];
}
else
{
$imageSrc = 'admin/upload/noimage.jpg';
}
?>
<tr>
<td>
<img src="<?php echo $imageSrc; ?>" height="100" style="width:60%;height:50%;"/>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>
If you want no image to show at all, then this would work
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
$image = '';
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$image = '<img src="admin/upload/'.$row['image'].'" height="100" style="width:60%;height:50%;"/>';
}
?>
<tr>
<td>
<?php echo $image; ?>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>

How to create dynamic links for the dynamic posts in MySQL and PHP

I'll try to explain the problem straight away. I have one HTML form which takes input just like a comment form and it saves the xyz data into a MySQL database using PHP. Now, what I want is to create and display links for those comments on a page.
I mean the comments which have been saved including the user's email and name, should be opened by clicking a link.
I don't want to display all the details on a single page from the database for all the users. There should be a page on which links are shown, when a user click a link, the full post should be displayed in next page.
There is not something which I know about this process. Please help me out.
// $rows = set of result from your database query
foreach($rows as $row){
echo '<a'
. ' href="my_link_to_display_comment?id='.$row['id'].'">'
. 'Comment from '.$row['user_name']
. '</a>';
}
First a page to display all the links like the below example -
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"".$row['event_name'].""
;}
and then in event.php(the next page after clicking link)
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];} ?>
<?php echo $title ?>
<?php echo $content ?>
If you want to show details of a single user just do this.
You can make a search box by using a form.
eg. like if I want to display a details of a student, I will search him by using his roll number and run these queries.
<?php //to search student
require_once './secure.inc.php';
$status = 0;
if(isset($_POST['submit'])){
$roll_number = $_POST['roll_number'];
$query = "select * from students where roll_number=$role_number";
require_once '../includes/db.inc.php';
$result = mysql_query($query);
if(mysql_num_rows($result)==1){
$status = 1;
$row = mysql_fetch_assoc($result); //mysql_fetch_array - both numeric and key index
}else{
$status=2;
}
}
?>
//to display
<?php } else if($status==1) { ?>
<table>
<tbody>
<tr>
<td>Roll Number : </td>
<td><?php echo $row['roll_number']; ?></td>
</tr>
<tr>
<td>Name : </td>
<td><?php echo $row['name']; ?></td>
</tr>
<tr>
<td>Gender : </td>
<td><?php echo $row['gender']; ?></td>
</tr>
<tr>
<td>Email : </td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Mobile Number : </td>
<td><?php echo $row['mobile_number']; ?></td>
</tr>
<tr>
<td>Course : </td>
<td><?php echo $row['course']; ?></td>
</tr>
</tbody>
</table>
<?php } ?>

How do I display a query in PHP table

My code
<?php
include('ConnectToDb.php');
$query = "SELECT * FROM News WHERE NewsFlag = 1 ORDER BY PostDate DESC";
$arrCount = -1;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$ID=$row['ID'];
$PostDate = $row['PostDate'];
$NewsHeader = stripslashes($row['NewsHeader'])
;
$NewsStart = stripslashes($row['NewsStart'])
;
echo "<hr>";
echo "<div>". date('j F Y',strtotime($PostDate)). "</div>";
echo "<p>";
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
if(count($array)>0) {
echo "<img src='". $array[0]. "' alt='' />";
}
echo "<h2 style='text-align:center'><u><a href='latestnews_full.php?ID=$ID'>". $NewsHeader. "</a></u></h2>";
echo "<div style='text-align:left'><h3>";
echo $NewsStart. " ......<a href='latestnews_full.php?ID=$ID'>(more)</a><br />";
echo "<div style='text-align:center'>";
echo "</div>";
echo "</h3></div>";
}
?>
displays my data nicely on four lines with date at the top, then a picture, title and then description.
However, I want to display the data as a table like this
<table style="width: 100%">
<tr>
<td colspan="2">postDate here</td>
</tr>
<tr>
<td rowspan="2">picture here</td>
<td>newsHeader here</td>
</tr>
<tr>
<td>newsStart here</td>
</tr>
</table>
I'm not sure how to echo the table cells correctly and all of my attempts so far have resulted in a white page. Could anyone please enlighten me?
I'd suggest you to make your logic separated from your presentable part. Close the PHP tag once you are ready fetching the results, assigning var's etc, then:
<table style="width: 100%">
<?php
//yourcode
//...
//...
$NewsStart = stripslashes($row['NewsStart']);
$news_id = strval(sprintf("%1$04d",$ID));
$array = scanImageFolder("newsImages/newsThumbs",$news_id);
?>
<tr>
<td colspan="2"><?= date('j F Y',strtotime($PostDate)) ?></td>
</tr>
<tr>
<?php
if(count($array)>0) {
?>
<td rowspan="2"><img src='<?= $array[0] ?>' alt='' /></td>
<?php } ?>
<td><?= $NewsHeader ?></td>
</tr>
<tr>
<td><?= $NewsStart ?> </td>
</tr>
<?php } ?>
</table>
However, it's again not so clear, and I would suggest using a template engine. If you want I can post a code with assigning vars to Smarty and output your presentation in Smarty template

Categories