Print all table data from mysql into a html table - php

I've been trying to print all users into a html table. The objective is that the site administrator can edit the information.
I've done the following code so far, but I can't understand how I will print User1_Name, User1_Email and password into a table row. I want this for every user on the database.
<body>
<div class="edituser">
<h1> Edit Users </h1>
<table>
<tr>
<th>User Name</th>
<th>Email</th>
<th>Password</th>
</tr>
<?php include("include/headerhomeadmin.php");?>
<?php while($row = mysqli_fetch_assoc($result)) {
/*$line[]=$row;*/
echo
$userusername =$row['username'];
$useremail =$row['email'];
$userpassword=$row['password'];
?>
<td><?php echo implode(" ", $line)?></td>
<?php } ?>
</table>
</div>
Can someone help me?
UPDATE:
My code is now printing the first username, the email from the second user and the password from de third user.
The code is the following:
<body>
<div class="edituser">
<h1> Editar Utilizadores </h1>
<table>
<tr>
<th>Nome de Utilizador</th>
<th>Email</th>
<th>Password</th>
</tr>
<tr>
<?php while($row = mysqli_fetch_assoc($result)) {
$userusername =$row['username'];
?>
<td> <?php echo $userusername ?> </td>
<?php while($row = mysqli_fetch_assoc($result)) {
$useremail =$row['email'];
?>
<td> <?php echo $useremail ?> </td>
<?php while($row = mysqli_fetch_assoc($result)) {
$userpassword=$row['password'];
?>
<td> <?php echo $userpassword ?> </td>
<?php }
}
}?>
How can I fix this?

if ($result->num_rows > 0) {?>
<table border=1>
<tr>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
</tr>
<?php }?>
</table>
<?php
}
else{
echo "0 results";
}
?>

Related

I want the php results to be displayed on the same page instead of a completely new page

I'm trying to search by name and display the results on the same page.
My php code and the html code are in 2 separate files. Given below is my search.php code.
<?php
include_once '../connection.php';
include_once '../session.php';
$output = '';
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
if($count == 0) {
$output = 'There was no search result!';
}else{
while($row = mysqli_fetch_array($result)) {
$ID = $row['id'];
$Name= $row['customername'];
$Type = $row['type'];
$Phone = $row['phoneno'];
$Email = $row['email'];
$output .= '<div>' .$Name.' '.$Type.' '.$Phone.' '.$Email.'<div>';
}
}
}
?>
<?php print("$output");?>
Given below is the html code for my form.
<form action="search.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
When I search, the results are displayed on a completely new page. But I want the results to be displayed on the same page with all the existing page layout, headers and footers.
How can I achieve this?
Thank in Advance!
Assuming your first file name is index.php.
Then need to call this file on form action & little bit refactor a file. Now it have $output array which is empty. If it contains search parameter, it will fill data to $output array and then on html part it checks if $output array has data to display it.
<?php
include_once '../connection.php';
include_once '../session.php';
$output = array();
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) {
$output[] = $row;
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<?php if(count($output) > 0): ?>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php foreach($output as $row): ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</form>
</body>
</html>

How to fix a table causing "502: Bad Gateway" error in PHP

I am creating a simple database that can do basic CRUD operations (create, read, update, delete) using php. I am able to complete the create, and able to see the results if I directly query the mySQL DB in the back end. But, I am having trouble getting the table to display on the webpage. It is instead displaying a "Bad Gateway" error if I attempt to display the database entries.
I tried removing the reference to the table, specifically
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>...
and the web page on front end works fine. Albeit can only see the data if I query the backend.
<?php include('php_code.php'); ?>
...
...
...
<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a href="test.php?edit=<?php echo $row['id']; ?>"
class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<!--in php_code.php-->
//to retrieve records
$select_query = "SELECT * FROM info";
$result = mysqli_query($db, $select_query);
I should be able to see the table with data containing name, address and city. But I am getting a 502 error instead.
Try this
<?php
include('php_code.php');
$results = mysqli_query($db, "SELECT * FROM `info` ");
$return = <<<HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
HTML;
while ($row = mysqli_fetch_array($results)) {
$return .= <<<HTML
<tr>
<td>{$row['name']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><a href="test.php?edit={$row['id']}" class="edit_btn" >Edit</a></td>
<td>Delete</td>
</tr>
HTML;
}
$return .= <<<HTML
</tbody>
</table>
HTML;
echo $return;
?>
your php_code.php should really only have the database config...
you are closing the php statements so you cannot retrieve the result of your query. Don't split php parts and just echo html like this
<?php
echo " <table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan='2'>Action</th>
</tr>
</thead> ";
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td>$row['name']</td>
<td>$row['address']</td>
<td> $row['city']</td>
<td>
<a href='test.php?edit=$row['id']'
class='edit_btn' >Edit</a>
</td>
<td>
<a href='php_code.php?del=$row['id']'
class='del_btn'>Delete</a>
</td>
</tr>";
}
echo "</table>";
?>

PHP View data from a single record by clicking on the view button

I have two pages students_list.php and students_profile.php
On students_list.php i have a table showing all records of all students and the VIEW button which when clicked it should open the students_profile.php page and get all the data from the single record selected based on the std_id
UPDATE: So now i get the id http://localhost/sms/student_profiles.php?std_id=3 when i click on the VIEW button but i am still not retrieving the data from this record selected
This is the code for students_list.php
<?php
require_once 'include/database/connect.php';
try {
$pdo = new PDO("mysql:host=$host_db;dbname=$name_db", $user_db, $pass_db);
$sql = 'SELECT * FROM students';
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database $name_db :" . $e->getMessage());
}
?>
<table id="data-table-default" class="table">
<thead>
<tr>
<th width="1%">#</th>
<th width="1%" data-orderable="false"></th>
<th>Name & Surname</th>
<th>Gender</th>
<th>Faculty</th>
<th>Semester</th>
<th>Mobile</th>
<th></th>
</tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<tr class="odd gradeX">
<td width="1%" class="f-s-600 text-inverse"><?php echo ($row['std_id']) ?></td>
<td width="1%" class="with-img"><?php echo ($row['std_status']) ?></td>
<td><?php echo ($row['std_name']) ?></td>
<td><?php echo ($row['std_gender']) ?></td>
<td><?php echo ($row['std_faculty']) ?></td>
<td><?php echo ($row['std_semester']) ?></td>
<td><?php echo ($row['std_mobile']) ?></td>
<td align="right">
<i class="fas fa-eye"></i> View
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and below this is the student_profile.php
<?php
require_once('include/database/sharepoint.php');
if(isset($_GET) & !empty($_GET)){
$std_id = $_GET['std_id'];
$sql = "SELECT * FROM `students` WHERE std_id=$std_id";
$res = mysqli_query($connection, $sql);
}
?>
<h4><?php echo ($row['std_name']) ?></h4>
<div class="d-flex">
<div class="pr-4"><strong>NATIONAL#</strong> <?php echo ($row['std_number']) ?></div>
<div class="pr-4"><strong>DOB</strong> <?php echo ($row['std_dob']) ?></div>
<div class="pr-4"><strong>GENDER</strong> <?php echo ($row['std_gender']) ?></div>
<div class="pr-4"><strong>MOBILE</strong> <?php echo ($row['std_mobile']) ?></div>
</div>
<div class="pt-1 font-weight-bold">Master's Degree (MBA)</div>
<div>STD# <strong><?php echo ($row['std_id']) ?></strong></div>
<div>email#example.com</div>
You don't fecth the data from the query. So $row will never contain the array you expect:
$row = mysqli_fetch_assoc($res); //this will populate $row
in your code:
<?php
require_once('include/database/sharepoint.php');
if(isset($_GET) & !empty($_GET)){
$std_id = $_GET['std_id'];
$sql = "SELECT * FROM `students` WHERE std_id=$std_id";
$res = mysqli_query($connection, $sql);
$row = mysqli_fetch_assoc($res);
}
?>

The First Data Do Not Show When Getting Data From MySQL Table to PHP

I am trying to fetch data from an sql database into a php table. Yet the problem is that the first data from my database doesn't appear into the table. Did i commit a mistake ? The following is my code....
<?php
include "koneksi.php";
$sql = mysqli_query ($link,
"SELECT * FROM absen");
$data = mysqli_fetch_array($sql);
?>
<html>
<head>
<title>Data Mahasiswa</title>
</head>
<body>
<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>
<table border="2" style="1000px;" align="center">
<tr bgcolor="blue">
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jenis Kelamin</th>
</tr>
<?php
while($data)
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['no']; ?></td>
<td><?php echo $data['nama']; ?></</td>
<td><?php echo $data['nim']; ?></</td>
<td><?php echo $data['jenis_kelamin']; ?></</td>
</tr>
<?php } ?>
</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px">
</a></h3></b></center>
</p>
</body>
</html>
You are doing mysqli_fetch_array() twice. Fixed code:
<?php
include "koneksi.php";
$sql = mysqli_query ($link,
"SELECT * FROM absen");
?>
<html>
<head>
<title>Data Mahasiswa</title>
</head>
<body>
<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>
<table border="2" style="1000px;" align="center">
<tr bgcolor="blue">
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jenis Kelamin</th>
</tr>
<?php
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['no']; ?></td>
<td><?php echo $data['nama']; ?></</td>
<td><?php echo $data['nim']; ?></</td>
<td><?php echo $data['jenis_kelamin']; ?></</td>
</tr>
<?php } ?>
</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px">
</a></h3></b></center>
</p>
</body>
</html>

PHP echo money format plus amount

I have a table for total report and I want to get a value from a database and format the numbers and have an output like this PHP 10,000.00 not like this 10000
Here is my trial PHP code. I have a table for print of Total reports and got problem in formatting the total amount.
function printpage()
{
window.print()
}
</script></head>
<body>
<br>
<br>
<div class="container">
<center>
<div class="alert alert-success" align="center"><h1>TOTAL REPORT</h1></div>
<br />
<table class="table table-striped table-bordered" border="1" width="900">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Start of Event</th>
<th>End of Event</th>
<th>Quantity</th>
<th>Amount</th>
<th>Status</th>
<th>Confirmation</th>
</tr>
</thead>
<tbody>
<?php
$con=mysql_connect("localhost","root","")or die(mysql_error());
$db=mysql_select_db('pm')or die(mysql_error());
$query=mysql_query("select * from reservation ")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['id'];
?>
<tr>
<?php $row['id'] ?>
<td><?php echo $row['firstname'] ?></td>
<td><?php echo $row['lastname'] ?></td>
<td><?php echo $row['address'] ?></td>
<td><?php echo $row['arrival']?></td>
<td><?php echo $row['departure'] ?></td>
<td><?php echo $row['result'] ?></td>
<td><?php
$number = 'payable';
$payable = 0;
setlocale(LC_MONETARY,"en_PHP");
echo $row("The price is %i", $payable);
?> </td>
<td><?php echo $row['status'] ?></td>
<td><?php echo $row['confirmation'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
$result = mysql_query("SELECT sum(payable) FROM reservation") or die(mysql_error());
while ($rows = mysql_fetch_array($result)) {
?>
<?php }
$result1 = mysql_query("SELECT result(qty) FROM rooinventory") or die(mysql_error());
while ($rows1 = mysql_fetch_array($result1)) {
?>
<div class="pull-right">
<div class="span">
<div class="alert alert-info"><i class="icon-credit-card icon-large"> </i> Total Amount: <?php echo $rows1['sum(p)']; ?></div>
</div>
</div>
</center>
<?php } ?>
Your locale is wrong and $number should actually be a $row value.
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $row['payable']);
For Windows machines
echo '$' . number_format(floatval($row['payable']));
Your code need to be fixed. Also en_US might not be enough depending on your system (ie: Debian needs en_US.utf-8):
setlocale(LC_MONETARY, "en_US.utf-8");
echo money_format("The price is %i", $row['payable']);

Categories