File view to the another tab (PHP) - 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

Related

How to fetch images from database in base64_encode in 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']);?>" />

Delete row data from one table using check box and insert the deleted row data in another table

/*I have two table , 1st table name is bazar and 2nd table name is bazarduepayment having same columne name : sl,date,item,paid,due,remark. 'sl' is auto increment . Delete function is working perfectly . Someone please help me how to insert deleted row data in 2nd table 'bazarduepayment' Here below is code detail i wrote */
<?php
session_start();
include_once("rwdbconnection.php");
error_reporting(0);
if(isset($_POST['save']))
{
$checkbox = $_POST['check'];
for($i=0;$i<count($checkbox);$i++)
{
$del_id = $checkbox[$i];
mysqli_query($conn,"DELETE FROM bazar WHERE sl='".$del_id."'");
$message = "Data deleted successfully !";
}
}
$result = mysqli_query($conn,"SELECT * FROM bazar");
?>
<!DOCTYPE html>
<html>
<head>
<linkrel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Delete data</title>
</head>
<body>
<div>
<?php if(isset($message)) { echo $message; } ?>
</div>
<form method="post" action="">
<table class="table table-bordered">
<thead>
<tr>
<th><input type="checkbox" id="checkAl"> Select All</th>
<th>Sl</th>
<th>Date</th>
<th>Item</th>
<th>Paid</th>
<th>Due</th>
<th>Remark</th>
</tr>
</thead>
<?php
$i=0;
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><input type="checkbox" id="checkItem" name="check[]" value="<?php echo $row["sl"]; ?>"></td>
<td><?php echo $row["sl"]; ?></td>
<td><?php echo $row["date"]; ?></td>
<td><?php echo $row["item"]; ?></td>
<td><?php echo $row["paid"]; ?></td>
<td><?php echo $row["due"]; ?></td>
<td><?php echo $row["remark"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<p align="center"><button type="submit" class="btn btn-success" name="save">DELETE</button></p>
</form>
<script>
$("#checkAl").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</body>
</html>
First you need to copy the data from one table to another using INSERT ... SELECT syntax and only then you can delete.
You should be using prepared statements for this.
if (isset($_POST['save'])) {
// Prepared INSERT query
$stmt_insert = $conn->prepare('INSERT INTO bazarduepayment(date,item,paid,due,remark)
SELECT date,item,paid,due,remark FROM bazar WHERE sl=?');
// Prepare DELETE query
$stmt_delete = $conn->prepare('DELETE FROM bazar WHERE sl=?');
// Loop on all checkboxes selected
foreach ($_POST['check'] as $del_id) {
$stmt_insert->bind_param('s', $del_id);
$stmt_insert->execute();
$stmt_delete->bind_param('s', $del_id);
$stmt_delete->execute();
}
}
You could even simplify this to get rid of the foreach loop entirely.

Why does edit button only function for row 1 in table php mysql

Why my edit button only function for row 1 only in my table?
When I click other rows it still show data from row 1 but the id is changed.
When I update row 1 the data updates in the database.
//fetch the record to be updated
if (isset($_GET['edit'])){
$entry_id = $_GET['edit'];
$edit_state = true;
$rec = mysqli_query($db, "select r.room_id, r.room_name, s.time_date, s.entry_id, s.time_exam, s.course_code, s.course_enroll from room r, schedule_entry s where s.room_id = r.room_id");
$record = mysqli_fetch_array($rec);
$time_date = $record['time_date'];
$time_exam = $record['time_exam'];
$course_code = $record['course_code'];
$course_enroll = $record['course_enroll'];
$room_name = $record['room_name'];
$room_id= $record['room_id'];
$entry_id= $record['entry_id'];
}
?>
<?php
if (mysqli_num_rows($results)>0){
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td width="180"><?php echo $row['time_date']; ?></td>
<td width="70"><?php echo $row['time_exam']; ?></td>
<td width="200"><?php echo $row['course_code']; ?></td>
<td width="70"><?php echo $row['course_enroll']; ?></td>
<td><?php echo $row['room_name']; ?></td>
<td width="70">
<a class="edit_btn" href="entry.php?edit=<?php echo $row['entry_id']; ?>">Edit</a>
</td>
<td width="70">
<a class="del_btn" href="entryserver.php?del=<?php echo $row['entry_id']; ?>">Delete</a>
</td>
</tr>
<?php } }?>
you need to run the loop to update the ids,
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<td> <a class="edit_btn" href="entry.php?edit=
<?php echo $row['entry_id']; ?>">Edit</a></td>
<?php
}
}
?>
you have to evaluate your syntax, i think $row['entry_id'] have same value, so you must var_dump($row) form know all, like that
.... SOME CODE ....
<?php
if (mysqli_num_rows($results)>0){
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><? var_dump($row); ?>
</tr>
<?php } }?>
and after that i think you can solve your problem ...

Session not working as it has to be, auto session lost or content loss upon refresh page

I am new is PHP, doing my final year project in Student Result Online System.
In my system, I have an error in session, Once the user/student logs in using their user name and password, the dashboard displays his details properly.
But, as soon as the page reloads or you click to view other contents or just simply click on the student's tool bar within dashboard, the page loses all of its relevant contents and becomes idle or blank.
I want to prevent auto session destroy or loss while the user remains on the same page until he/she logs-out. any help would be grateful. Interesting thing is that, this codes works really well in Localhost (Wamp Server), but it loses its session in Online (cpanel).
My codes pages are:
login.php
<?php
include('dbcon.php');
if (isset($_POST['login'])){
session_start();
$student_no = $_POST['student_no'];
$password = $_POST['password'];
$query = "SELECT * FROM students WHERE student_no='$student_no' AND password='$password' and status = 'active' ";
$result = mysql_query($query)or die(mysql_error());
$num_row = mysql_num_rows($result);
$row=mysql_fetch_array($result);
if( $num_row > 0 ) {
header('location:dasboard.php');
$_SESSION['id']=$row['student_id'];
}
elae{
header('location:access_denied.php');
}
}
?>
session.php
<?php
session_start();
if (!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
header("location: index.php");
exit();
}
$session_id=$_SESSION['id'];
?>
dasboard.php
<?php include('session.php'); ?>
<?php include('header.php'); ?>
<?php include('navbar.php'); ?>
<?php
$query=mysql_query("select * from students where student_id='$session_id'")or die(mysql_error());
$row=mysql_fetch_array($query);
$year_level = $row['year_level'];
$term = $row['term'];
$status = $row['student_status'];
$school_year = $row['year_level'];?>
<div class="container">
<div class="margin-top">
<div class="row">
<?php include('head.php'); ?>
<div class="span12">
<div class="grade">
<?php include('grade_option.php'); ?>
</div>
</div>
<div class="span2">
<?php include('user_sidebar.php'); ?>
</div>
<div class="span10">
<table cellpadding="0" cellspacing="0" border="0" class="table table-bordered" id="example">
<thead>
<tr>
<th width="100">Code</th>
<th width="300">Subject</th>
<th width="50">Units</th>
<th>Gen Ave.</th>
<th>Term</th>
<th>Year Level</th>
<th>Remarks</th>
</tr>
</thead>
<tbody>
<?php $user_query=mysql_query("select * from grade where student_id = '$session_id' and school_year = '$year_level'
and semester = '$term'
")or die(mysql_error());
while($row=mysql_fetch_array($user_query)){
$id=$row['grade_id'];
$remarks = $row['remarks'];
$subject_id = $row['subject_id'];
$subject_query = mysql_query("select * from subject where subject_id = '$subject_id'")or die(mysql_error());
while($subject_row=mysql_fetch_array($subject_query)){
?>
<tr>
<td>
<?php echo $subject_row['code']; ?></td>
<td><?php echo $subject_row['title']; ?></td>
<td><?php echo $subject_row['unit']; ?></td>
<td><?php echo $row['gen_ave']; ?></td>
<td><?php echo $row['semester']; ?></td>
<td><?php echo $row['school_year']; ?></td>
<?php if ($remarks == 'Very Good'){ ?>
<td><span class="very_good"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Excellent'){ ?>
<td><span class="Excellent"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Satisfactory'){ ?>
<td><span class="sat"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Fair'){ ?>
<td><span class="fair"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Failed'){ ?>
<td><span class="failed"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Incomplete'){ ?>
<td><span class="failed"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Officially Dropped'){ ?>
<td><span class="drop"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'PASS'){ ?>
<td><span class="Excellent"><?php echo $row['remarks']; ?></span></td>
<?php }else if ($remarks == ''){ ?>
<td><?php echo $row['remarks']; ?></td>
<?php } ?>
</tr>
<?php }} ?>
</tbody>
</table>
</div>
<?php include('units_table.php'); ?>
<?php include('gwa_table.php'); ?>
<?php include('cwa_table.php'); ?>
</div>
<?php include('grading_system.php') ?>
</div>
</div>
</div>
<?php include('footer.php') ?>
In your following code:
if( $num_row > 0 ) {
header('location:dasboard.php');
$_SESSION['id']=$row['student_id'];
}
You do
header('location:dasboard.php');
Before you set the session:
$_SESSION['id']=$row['student_id'];
Meaning you redirect first, and session is NEVER set.
By the way header.php, includes header.php?
And
elae{
header('location:access_denied.php');
}
Should be else of course.

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