I cant get exist data from MySql using PHP - 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>";
}
?>

Related

Export HTML table to Excel using php

I am trying to export html table to excel sheet.But it is not working. I have a table where i've called DB table values. I want to export that values to a excel sheet on a button click. But after clicking on the button, Nothing is happening.
I have followed one tutorial for this. Can you please tell me, if i am missing something?
Code:
Php code:
<button name="create_excel" id="create_excel" class='btn c-theme-btn c-btn-uppercase btn-xs c-btn-square c-font-sm'>Export to Excel</button>
</div>
<div class="c-content-panel">
<div class="c-body">
<div class="row">
<div class="col-md-12" id="employee_table">
<table class="table table-bordered">
<?php
echo " <thead>
<tr>
<th>Order ID</th>
<th>Date</th>
<th>Name</th>
<th>Store Name</th>
<th>Zip</th>
<th>City</th>
<th>Address</th>
<th>Contact</th>
<th>Tv Varient</th>
<th>Total</th>
<th>Delivery</th>
<th>Action</th>
</tr>
</thead>
<tbody>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo"<tr id='row_$id'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['rdate'] . "</td>";
echo "<td>" . $row['rname'] . "</td>";
echo "<td>" . $row['rstore'] . "</td>";
echo "<td>" . $row['rzip'] . "</td>";
echo "<td>" . $row['rcity'] . "</td>";
echo "<td>" . $row['raddress'] . "</td>";
echo "<td>" . $row['rphone'] . "</td>";
echo "<td>" . $row['rtv_varient'] . "</td>";
echo"<td>" . $row['ramount'] . "</td>";
echo"<td>" . $row['rdelivery'] . "</td>";
echo "<input type='hidden' id='row_id' value='$id'/>";
echo "<td> <button id='test' class='btn c-theme-btn c-btn-uppercase btn-xs c-btn-square c-font-sm'>Delete</button> </td>";
echo"</tr>";
}
?>
</tbody>
excel.php:
<?php
//excel.php
header('Content-Type: application/vnd.ms-excel');
header('Content-disposition: attachment; filename='.rand().'.xls');
echo $_POST["data"];
?>
Script Code:
<script>
$(document).ready(function(){
$('#create_excel').click(function(){
var excel_data = $('#employee_table').html();
var page = "excel.php?data=" + excel_data;
window.location = page;
});
});
</script>
I have followed this tutorial: http://www.webslesson.info/2016/08/export-html-table-to-excel-file-using-jquery-with-php.html

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

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.

Update values in sql when the data is being printed using while

The form is used so that the method post could be used whenever the button is clicked, for more info go through the code.
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<?php
//selecting rowa where the approve status is false or simple "0"
$result = mysqli_query($db,"SELECT id, rid, time, amount, transid, wallet FROM donations WHERE approve = 0");
//this button will be used later update the rows and set approve to true or "1"
$button = "<button type='submit' name='approve' class='btn btn-success' role='button'>" . "APPROVE" . "</button>";
echo "<table class='table'>
<tr>
<th>ID</th>
<th>RID</th>
<th>TIME</th>
<th>AMOUNT</th>
<th>TRANS HASH</th>
<th>WALLET</th>
<th>APPROVE</th>
</tr>";
//printing all the values where approve = '0'
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr>";
echo "<td class='col-sm-1'>" . $row['id'] . "</td>";
echo "<td class='col-sm-1'>" . $row['rid'] . "</td>";
echo "<td class='col-sm-2'>" . $row['time'] . "</td>";
echo "<td class='col-sm-1'>" . $row['amount'] . "</td>";
echo "<td class='col-sm-3'>" . $row['transid'] . "</td>";
echo "<td class='col-sm-3'>" . $row['wallet'] . "</td>";
echo "<td class='col-sm-1'>" . $button . "</td>";
echo "</tr>";
}echo "</table>";
//when the button is clicked, the method 'post' is invoked and the value is updated
if($_SERVER["REQUEST_METHOD"] == "POST"){
$id = $row['id'];
$approve = mysqli_query($db,"UPDATE `donations` SET approve = 1 WHERE approve = 0 and id = '$id'");
}
?>
The problem here is that I want a button for each row and each should be updated individually when that row's button is clicked.
1st : Change your code order . update part should be in page top.Because you need to update if it's submit .then only you need to fetch data from database . In your order your fetching the data and showing to user then updating at last . it should not be like this.
2nd : Just pass the row id as submit button value like this and it should be inside the while loop .
$button = "<button type='submit' name='approve' value='".$row["id"]."' class='btn btn-success' role='button'>" . "APPROVE" . "</button>";
3rd : On submit you will get the row id in $_POST['approve']
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$id = $_POST['approve'];
$approve = mysqli_query($db,"UPDATE `donations` SET approve = 1 WHERE approve = 0 and id =$id");
}
?>
Note : The above code should be on page top .
Update 1 : The code order should be like this
<?php
//when the button is clicked, the method 'post' is invoked and the value is updated
if($_SERVER["REQUEST_METHOD"] == "POST"){
$id = $_POST['approve'];
$approve = mysqli_query($db,"UPDATE `donations` SET approve = 1 WHERE approve = 0 and id =$id");
}
?>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<?php
//selecting rowa where the approve status is false or simple "0"
$result = mysqli_query($db,"SELECT id, rid, time, amount, transid, wallet FROM donations WHERE approve = 0");
//this button will be used later update the rows and set approve to true or "1"
echo "<table class='table'>
<tr>
<th>ID</th>
<th>RID</th>
<th>TIME</th>
<th>AMOUNT</th>
<th>TRANS HASH</th>
<th>WALLET</th>
<th>APPROVE</th>
</tr>";
//printing all the values where approve = '0'
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$button = "<button type='submit' name='approve' value='".$row["id"]."' class='btn btn-success' role='button'>" . "APPROVE" . "</button>";
echo "<tr>";
echo "<td class='col-sm-1'>" . $row['id'] . "</td>";
echo "<td class='col-sm-1'>" . $row['rid'] . "</td>";
echo "<td class='col-sm-2'>" . $row['time'] . "</td>";
echo "<td class='col-sm-1'>" . $row['amount'] . "</td>";
echo "<td class='col-sm-3'>" . $row['transid'] . "</td>";
echo "<td class='col-sm-3'>" . $row['wallet'] . "</td>";
echo "<td class='col-sm-1'>" . $button . "</td>";
echo "</tr>";
}echo "</table>";
?>
Update 2 : Try to use prepared statement to avoid sql injection

showing each data from the database

I already show the list of names from the database, but the problem is I don't know how to show the information each user, once I click some user in my list her/his information will appear.
html
<div class="member_list">
<div class="list-unstyled">
<?php require_once "../function/admin_function.php"; ?>
</div>
</div>
<div class="information" id="table_information">
<table class="table_information">
<tr>
<th colspan="4">Information</th>
</tr>
<tr>
<th>lastname</th>
<th>address</th>
<th>contact</th>
</tr>
<tr>
<td>
<?php include "../function/information_function.php"; ?>
</td>
</tr>
</table>
</div>
information_function - php
<?php
include "../connection/connection.php";
$sql = "SELECT * FROM registration";
$result = mysqli_query($dbconn, $sql);
while ($row = mysqli_fetch_array ($result)){
echo "<tr>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "</tr>";
}
?>
user list - php
<?php
include "../connection/connection.php";
$sql = "SELECT * FROM registration";
$result = mysqli_query ($dbconn, $sql);
while($row = mysqli_fetch_array ($result)) {
echo "<ul class='table_php'>";
echo "<li>";
echo "<a href='#table_information' class='friends_link'>";
echo "<span class='chat-img pull-left'>";
echo "<img src='user.png' class='img-circle'>";
echo "</span>" . $row['lname'] . "</a>";
echo "</li>";
echo "</ul>";
}
?>
Put your code inside a function and just call that function after include statement.
//information_function//
<?php
include "../connection/connection.php";
function get_information(){
$sql = "SELECT * FROM registration";
$result = mysqli_query($dbconn, $sql);
while ($row = mysqli_fetch_array ($result)){
echo "<tr>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "</tr>";
}
}
?>
//html//
<div class="member_list">
<div class="list-unstyled">
<?php require_once "../function/admin_function.php"; ?>
</div>
</div>
<div class="information" id="table_information">
<table class="table_information">
<tr>
<th colspan="4">Information</th>
</tr>
<tr>
<th>lastname</th>
<th>address</th>
<th>contact</th>
</tr>
<?php include "../function/information_function.php"; ?>
<?php get_information(); ?>
</table>
</div>

Fetch Array display

So i got this for display in row my sql select:
$result = mysql_query("SELECT nome_sitio, horario, contato, morada, imagem FROM sitio where id_tipo=1");
echo "<table border='1'>
<tr>
<th>Nome</th>
<th>Horário</th>
<th>Contato</th>
<th>Morada</th>
<th>Imagem</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['nome_sitio'] . "</td>";
echo "<td>" . $row['horario'] . "</td>";
echo "<td>" . $row['contato'] . "</td>";
echo "<td>" . $row['morada'] . "</td>";
echo "<td>" . $row['imagem'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
But i made a design in html, that with html and css looks like this:
http://i.stack.imgur.com/gb1s9.png
And the html code is:
<div id="sitios">
<div class="imageRow">
<div class="single">
<img src="backoffice/images_sitios/thumbnails/$imagem"/>
</div>
</div>
<div id="texto">
<h2><b>$nome_sitio</b></h2>
<h3><b>Morada:</b></h3> $morada</br>
<h3><b>Horário:</b></h3> $horario</br>
<h3><b>Contato:</b></h3> $contato</br>
<h3><b>Freguesia:</b></h3> $idfreguesia
</div>
</div>
Can someone help me replacing the text with the vars?
You need to escape back to PHP to display the variables:
<div id="sitios">
<div class="imageRow">
<div class="single">
<img src="backoffice/images_sitios/thumbnails/<?php echo $imagem; ?>"/>
</div>
</div>
<div id="texto">
<h2><b><?php echo $nome_sitio; ?></b></h2>
<h3><b>Morada:</b></h3> <?php echo $morada; ?></br>
<h3><b>Horário:</b></h3> <?php echo $horario; ?></br>
<h3><b>Contato:</b></h3> <?php echo $contato; ?></br>
<h3><b>Freguesia:</b></h3> <?php echo $idfreguesia; ?>
</div>
</div>

Categories