Two tables displayed on one page using PHP, PDO and MySql - php

I am trying to create a CRUD dashboard, using a MySql backend and a bootstrap front-end with PHP and PDO to communicate with the database. I am a noob to web development, but not to coding.
The goal is to create a web app to log my patient consults. Thus, my table structure is a single "main" table and two children tables with relationships to the "main" table, named "consults" and "procedures".
I am trying to make a dashboard, where I display my "main" table, and then add two children tables below it. (Later on I will style it better, but I am trying to get this working).
The following is the best MWE I could think of (would love it if someone had a simpler solution). The first "logbook patients" table works well, and displays rows of patients well. Its the second table that is the problem, and in particular:
$sql = "SELECT * FROM proc";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
This is the area I keep getting an error. The error is:
Fatal error: Uncaught Error: Call to a member function query() on null in /home/paincl5/public_html/logbook/logbook.php:110 Stack trace: #0 {main} thrown in /home/paincl5/public_html/logbook/logbook.php on line 110
The code at line 110 is
unset($pdo);
My full code is:
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header clearfix">
<h2 class="pull-left">Logbook Patients</h2>
<a href="create.php" class="btn btn-success pull-right" >Add New Patient</a>
</div>
<?php
// Include config file
require_once 'config.php';
// Attempt select query execution
$sql = "SELECT * FROM main";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<div style='height:300px;overflow-y:scroll;;'>";
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Surname</th>";
echo "<th>first_name</th>";
echo "<th>DOB</th>";
echo "<th>Hospital</th>";
echo "<th>MRN</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['Surname'] . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['DOB'] . "</td>";
echo "<td>" . $row['Hospital'] . "</td>";
echo "<td>" . $row['MRN'] . "</td>";
echo "<td>";
echo "<a href='read.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='update.php?id=". $row['id'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='delete.php?id=". $row['id'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</div>";
// Free result set
unset($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo);
?>
</div>
</div>
</div>
</div>
// Procedure Table
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header clearfix">
<h2 class="pull-left">Procedures</h2>
<a href="create_proc.php" class="btn btn-success pull-right" >Add New Procedure</a>
</div>
<?php
// Include config file
require_once 'config.php';
// Attempt select query execution
$sql = "SELECT * FROM proc";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<div style='height:300px;overflow-y:scroll;;'>";
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Procedure Type</th>";
echo "<th>Procedure Name</th>";
echo "<th>Notes</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['procedure_type'] . "</td>";
echo "<td>" . $row['procedure_name'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>";
echo "<a href='update.php?id=". $row['id1'] ."' title='Update Record' data-toggle='modal' data-target='#myModal' ><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='delete.php?id=". $row['id1'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</div>";
// Free result set
unset($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo); //Here occurs the error (line 110)
?>
</div>
</div>
</div>
</div>

You are destroying your $pdo variable with unset($pdo) so any subsequent calls are trying to run methods against a null object.
Try removing references to that unset.
I'm assuming that the $pdo object is coming from config.php. Since you're using require_once, it will only include the config file the first time the require_once is called. That's why the $pdo is destroyed and not recreated.

Related

hyper link showing outside of table

I am trying to show a table as per the screen shot below.
Screen Shot
The goal is to have the 'folder' image as a clickable link getting the link data from the MySql table.
However, when i try to do this (step by step) the hyperlinks sit outside the table. When i add tags around the following
echo ''.$row['file'].'' ;
like this
echo "<td>" ''.$row['file'].'' "</td>";
The subsequent PHP page will not load
// Attempt select query execution
$sql = "SELECT * FROM versioncontrol";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Image Link</th>";
echo "<th>Manual Link File</th>";
echo "<th>Operating Procedure ID</th>";
echo "<th>Operating Procedure Name</th>";
echo "<th>Operating Procedure Version</th>";
echo "<th>Upload Date and Time</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>
<a href=\"sopversioncontrolKL001.php" . $record['images'] . "\" >
<img src=\"images/document.jpg" . $record['images'] . "\" height=\"30\"
/></a></td>";
echo ''.$row['file'].'' ;
echo "<td>" . $row['SOP_ID'] . "</td>";
echo "<td>" . $row['SOP_Name'] . "</td>";
echo "<td>" . $row['SOP_Version'] . "</td>";
echo "<td>" . $row['reg_date'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
echo "<td>" ''.$row['file'].'' "</td>";
The subsequent PHP page will not load
Well, it wouldn't. That's a syntax error.
After echo "<td>" you need to have either a ; or an operator.
You can't just run straight into another string literal.
Put the <td> and </td> inside the string literals you already have.
echo '<td>'.$row['file'].'</td>';
Better yet, get rid of the enormous pile of echo statements and just output HTML.
?>
...
<td><?php echo $row['file']; ?></td>
...
<?php
(You should probably make use of htmlspecialchars to protect yourself against stored XSS attacks too).

PHP while loop problem, is not creating a new row

i try to get some data from a database and put it in a table.
the problem is the loop, the second row is not going to the new row, is continuing in same row. I checked the code and I can not see the problem.
Infact in the database i have 3 row and in the website is geting just 2 of them and booth in same row.
Can anybody help me?
<h1>view database</h1>
<div class="container">
<div class="page-header">
<h1>Shop list</h1>
</div>
<?php
$query = "SELECT shop_name, shop_phone_number, shop_email, shop_address_no, shop_address_street, shop_town, shop_county, shop_postcode, shop_address_country FROM shop";
$stmt = $conn->prepare($query);
$stmt->execute();
$num = $stmt->rowCount();
//button to add new shop
echo "<a href='index.php?page=shop' class='btn btn-primary m-b-1em'>Add new shop</a>";
if($num>0){
echo "<table class='table table-hover table-responsive table-bordered'>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Phone</th>";
echo "<th>Email</th>";
echo "<th>Address no/name</th>";
echo "<th>Street</th>";
echo "<th>Town</th>";
echo "<th>County</th>";
echo "<th>Post Code</th>";
echo "<th>Country</th>";
echo "<th>Action</th>";
echo "</tr>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$shop_name}</td>";
echo "<td>{$shop_phone_number}</td>";
echo "<td>{$shop_email}</td>";
echo "<td>{$shop_address_no}</td>";
echo "<td>{$shop_address_street}</td>";
echo "<td>{$shop_town}</td>";
echo "<td>{$shop_county}</td>";
echo "<td>{$shop_postcode}</td>";
echo "<td>{$shop_address_country}</td>";
echo "<td>";
echo "<a href='read_one.php?name={$shop_name}' class='btn btn-info m-r-1em'>Read</a>";
echo "<a href='read_one.php?name={$shop_name}' class='btn btn-primary m-r-1em' >Edit</a>";
echo "<a href='#' onclick='delete_user({$shop_name})' class='btn btn-danger>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "<div class='alert alert-danger'>No Records found.</div>";
}
?>
</div>
?>

FPDF Generate blank documennt

I create pdf in php using FPDF it's always showing blank documents. its size only 1.60kb. i fetching mysql data and want to admit card.
I create pdf in php using FPDF it's always showing blank documents. its size only 1.60kb. i fetching mysql data and want to admit card.
(i) admitcard.php
<div class="container">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#viewapp">View Applications</a></li>
<li>Logout</li>
</ul>
<div class="tab-content">
<div id="viewapp" class="tab-pane fade in active">
<?php
$rs1 = mysqli_query($con,"select * from demodata");
echo '<table class="table table-striped" id="tblData">';
echo "<thead>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>NAME</th>";
echo "<th>DATE OF BIRTH</th>";
echo "<th>EMAIL ID</th>";
echo "<th>CONTACT NO.</th>";
echo "<th>SIGNUP DATE</th>";
echo "<tr>";
echo "</thead>";
echo "<tbody>";
while($ar = mysqli_fetch_array($rs1))
{
echo "<tr>";
echo "<td><a href='viewform.php?id=".$ar[0]."'>" . $ar[0] . "</a></td>";
echo "<td>" . $ar[3] . "</td>";
echo "<td>" . $ar[2] . "</td>";
echo "<td>" . $ar[4] . "</td>";
echo "<td>" . $ar[5] . "</td>";
echo "<td>" . $ar[6] . "</td>";
echo "<td> <a target='_blank' href='generate_pdf.php?id=".$ar[0]."'>
<input type='button' value='PDF' name='adcard' class='toggle btn btn-primary'> </a></td>";
echo "</tbody>";
echo "</table>";
?>
</div>
</div>
(ii) genrate_pdf.php
<?php
require("fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$a=$_POST["add"];
$pdf->Cell(40,10,$a);
$pdf->Output();
?>

I cant get exist data from MySql using PHP

I want to select data from 2 tables; First one is "Dictionary" and the second is "term_dic". The Problem is when the query is executed it's returning a repeated data or data not related with the query.
$stmt = $con->prepare("SELECT * from dictionary,term_dic where dictionary.task_ID = ? and term_dic.task_ID = ? ");
//execute The Statment
$stmt->execute(array($task,$task));
// Assign To Variable
$rows=$stmt->fetchAll();
?>
<h1 class="text-center"><?php echo $_SESSION['TaskName']; ?></h1>
<div class="container">
<div class="table-responsive">
<table class ="main-table text-center table table-bordered">
<tr>
<td>#ID</td>
<td>Word</td>
<td>From</td>
<td>To</td>
<td>Face EXP</td>
<td>Registerd Date</td>
<td>Control</td>
</tr>
<?php
foreach ($rows as $row) {
echo"<tr>";
echo "<td>".$row['dic_ID'] . "</td>";
echo "<td>".$row['word'] . "</td>";
echo "<td>".$row['start_from'] . "</td>" ;
echo "<td>".$row['end_to'] . "</td>" ;
echo "<td>".$row['face_exp'] . "</td>" ;
echo "<td>".$row['date_reg'] ."</td>";
echo "<td><a href='dictionary.php?do=Edit&userid=". $row['dic_ID'] ."' class='btn btn-success'><i class='fa fa-edit'></i> Edit</a>
<a href='dictionary.php?do=Delete&userid=". $row['dic_ID'] ."' class='btn btn-danger confirm'><i class='fa fa-close'></i> Delete </a>";
echo "</td>";
echo "</tr>";
}
?>

Selecting data from sql to create a thumbnail

I'm creating a thumbnail with a title, image and caption on it. I'm trying to select data from my table to show it into my homepage. Can someone help me to create a normal thumbnail in my php that contains the detail from my sql. I tried to search and can't find how to create a thumbnail using php and not html.
$sql = "SELECT * FROM news";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>first_name</th>";
echo "<th>last_name</th>";
echo "<th>email</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "<td>" . $row['caption'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
In short, is there a way to create a php file from this?
<div class="col-md-4">
<div class="thumbnail">
<img alt="Memory" img src="../../images/a2.jpg">
<div class="caption">
<h3><b>
Title
</b></h3>
<p>
Caption Caption Caption Caption Caption
</p>
<p align="right">
<a class="btn btn-primary" href="news2.html">Read More</a>
</p>
</div>
</div>
</div>
Do you want to replace your table structure with that template structure? You'll need to adjust some of the data to fill the hyperlink (I don't know how you want to build that).
while($row=mysqli_fetch_array($result)){
echo "<div class=\"col-md-4\">";
echo "<div class=\"thumbnail\">";
echo "<img alt=\"Memory\" src=\"../../images/{$row["image"]}\">";
echo "<div class=\"caption\">";
echo "<h3>{$row["title"]}</h3>";
echo "<p>{$row["caption"]}</p>";
echo "<p align=\"right\">";
echo "<a class=\"btn btn-primary\" href=\"news2.html\">Read More</a>";
echo "</p>";
echo "</div>";
echo "</div>";
echo "</div>";
}

Categories