Issue with show all users in the table with pdo - php

I have the next issue, when I need the code show me all the users in the table always show me one data the first one or the last one if I change the ASC to DESC inside of SELECT..
I need to show me all users... can you please help me with this?
Here the code and the table with the row I need to show:
<?
include '../include/config.php';
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
$id_paciente = $row['id_paciente'];
$id_tipo = $row['id_tipo'];
$nombre = $row['nombre'];
$apellido = $row['apellido'];
$ciudad = $row['ciudad'];
$telefono = $row['telefono'];
$foto = $row['foto'];
}
?>
<tr>
<th><?php echo $id_paciente; ?></th>
<td><img src="../<?php echo $foto;?>" class="image_thumbnail" /></td>
<td><?php echo $nombre; ?></td>
<td><?php echo $apellido; ?></td>
<td><?php echo $id_tipo; ?></td>
<td><?php echo $ciudad; ?></td>
<td><?php echo $telefono; ?></td>

You are echoing your variables outside of the loop.
So, move it inside:
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
?>
<tr>
<th><?php echo $row['id_paciente'] ?></th>
<td><img src="../<?php echo $row['foto']?>" class="image_thumbnail" /></td>
<td><?php echo $row['nombre'] ?></td>
<td><?php echo $row['apellido'] ?></td>
<td><?php echo $row['id_tipo'] ?></td>
<td><?php echo $row['ciudad'] ?></td>
<td><?php echo $row['telefono'] ?></td>
<tr>
<? } ?>

well I get my answer with my problem...
now I see all the users, the code neccesary is:
<?
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
?>
for somebody want to
Best Regards!

Related

Is there any way to display two data in one row using Rowspan in HTML TABLE?

Im setting up a html table that connected from the database. But I want the table design like the excel which in html table using rowspan. but I cant work out the problem which Im working out.
Please help me to create this format. See the code below
<tbody>
<?php
include_once('connection.php');
$sql = "SELECT * FROM rqn ";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)){
$result = $row['rqn_no'];
?>
<tr>
<td rowspan="2"><?php echo $row['rqn_no'] ?></td>
<td><?php echo $row['date_filed'] ?></td>
<td><?php echo $row['desc_text'] ?></td>
<td><?php echo $row['remarks'] ?></td>
<td><?php echo $row['date_approved'] ?></td>
<td>
<?php
include_once('connection.php');
$sql1 = "SELECT * FROM po_data where po_data.rqn_base = $result ";
$query1 = mysqli_query($conn, $sql1);
while($row1 = mysqli_fetch_assoc($query1)){
?>
<?php echo $row1['po_no'] ?>
<?php
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
Result I Wanted:
https://prnt.sc/md2t2s
Actual Result:https://prnt.sc/md2tpk
You can use rowspan with a div to achieve the result you want. Try this,
<tbody>
<?php
include_once('connection.php');
$sql = "SELECT * FROM rqn ";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)){
$result = $row['rqn_no'];
?>
<tr>
<td rowspan="2"><?php echo $row['rqn_no'] ?></td>
<td><?php echo $row['date_filed'] ?></td>
<td><?php echo $row['desc_text'] ?></td>
<td><?php echo $row['remarks'] ?></td>
<td><?php echo $row['date_approved'] ?></td>
<?php
include_once('connection.php');
$sql1 = "SELECT * FROM po_data where po_data.rqn_base = $result ";
$query1 = mysqli_query($conn, $sql1);
$total_rows = mysqli_num_rows($query1);
// open <td>
echo "<td rowspan=$total_rows>";
while($row1 = mysqli_fetch_assoc($query1)){
?>
<?php echo "<div style='border-bottom:1px solid black'>" . $row1['po_no'] ."</div>" ?>
<?php
}
// close </td>
echo "</td>";
?>
</td>
</tr>
<?php
}
?>
</tbody>
Adjust the CSS according to your need.
Hi this is the final answer to my result.
Output
<tbody>
<?php
include ("connection.php");
$sql = "SELECT * FROM rqn ";
$result= mysqli_query($conn, $sql);
while($row=mysqli_fetch_array($result)):
$ename = $row['rqn_no'];
// count the esal in each ename
$sql2 = "SELECT * FROM po_data WHERE rqn_base=$ename";
$result2 = mysqli_query($conn, $sql2);
$count_result2 = mysqli_num_rows($result2);
?>
<tr >
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['rqn_no']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['date_filed']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['desc_text']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['remarks']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['date_approved']; ?></td>
<?php
// loop each esal
while($row2 = mysqli_fetch_array($result2)):
?>
<td><?php echo $row2['po_no']; ?></td>
</tr>
<?php
endwhile; // endwhile for each esal looping
endwhile; // endwhile for the main looping
?>
</tbody>
Tnx for helping me guys

PHP/MySQL: Displaying data from different tables based on a users ID

I have two tables "personal_trainer" and "training_plan". PersonalTrainerID is a foreign key in training_plan. I want to display that when a trainer logs in with their email and password, only the training plans that apply to that ID appears.
However, I am having trouble understanding the logic. I have coded it so that all the information from the training_plan table appears, I cannot created it such that only the rows that apply to the ID are visible to the user. I have made this by the simple sql statement "SELECT * from training_plan". There is a filter textbox to search the table that doesn't effect the code if you're wondering.
I have commented the code to try make it easier to understand. Any help would be greatly appreciated!
<?php
if (isset($_POST['search'])) /*This code allows the filter textbox to search the db */
{
$valueToSearch = $_POST['ValueToSearch'];
$query = "select * from training_plan WHERE concat('trainingPlanID', `personalTrainerID`, `clientID`, `trainingType`, `exercise1`, `exercise2`, `exercise3`, `exercise4`, `exercise5`, `exercise6`, 'reps', 'sets', 'description')like'%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * from training_plan WHERE PersonalTrainerID= (SELECT personalTrainerID FROM personal_trainer WHERE email=$_SESSION['user'])"; /*The error that is displayed is 'syntax error, unexpected string after ['*/
$search_result = filterTable($query);
}
function filterTable($query)
{
$connect = mysqli_connect("localhost:3308","root","","fypdatabase");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<?php /*This displays the data in a table but so far outputs all of the table data */
while($row = mysqli_fetch_array($search_result))
{
?>
<tr>
<td><?php echo $row["trainingPlanID"]; ?></td>
<td><?php echo $row["personalTrainerID"]; ?></td>
<td><?php echo $row["clientID"]; ?></td>
<td><?php echo $row["trainingType"]; ?></td>
<td><?php echo $row["exercise1"]; ?></td>
<td><?php echo $row["exercise2"]; ?></td>
<td><?php echo $row["exercise3"]; ?></td>
<td><?php echo $row["exercise4"]; ?></td>
<td><?php echo $row["exercise5"]; ?></td>
<td><?php echo $row["exercise6"]; ?></td>
<td><?php echo $row["reps"]; ?></td>
<td><?php echo $row["sets"]; ?></td>
<td><?php echo $row["description"]; ?></td>
<td>
Delete
</td>
<td>
Update
</td>
</tr>
<?php
Change your query to:
$query = "SELECT * from training_plan WHERE PersonalTrainerID = (SELECT personalTrainerID FROM personal_trainer WHERE email='".$_SESSION['user']."')";

duplicate on my PDO query

Here's my code sir:
<?php
session_start();
include 'include/db_config.php';
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
?>
<tr class="record">
<td><?php echo $row2['firstname'];?></td>
<td><?php echo $row['no_guest']; ?></td>
<td><?php echo $row['type_service']; ?></td>
<td><?php echo $row['datepicker']; ?></td>
<td><?php echo $row['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
}
?>
i didnt use a LEFT JOIN for displaying data's from 2 tables. i just want to do it in my own way . But my problem is it duplicates my data . before i inserted my second query its just 2 data's and now its 4 already. i just cant figure it out where is the duplication occurs.
Someone help me out please . Thanks in Advance.
You display data in second for, which is inside first for. So you get result count of customer_info table length*service_info length results. You should save info in arrays and then use only one for cycle. Eg.:
<?php
session_start();
include 'include/db_config.php';
$services = array();
$customers = array();
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$services[] = $row;
}
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
$customers[] = $row2;
}
foreach($services as $key => $service) {
?>
<tr class="record">
<td><?php echo $customers[$key]['firstname'];?></td>
<td><?php echo $service['no_guest']; ?></td>
<td><?php echo $service['type_service']; ?></td>
<td><?php echo $service['datepicker']; ?></td>
<td><?php echo $service['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
?>
Btw, i suggest you use different approach, like joins

display 2 tables on link by id

i have a table with id, name, address, sector, financiar, link
on the link i when i press it i want to show me 2 tables from the id of row selected, ex: id 1.
http://postimg.org/image/khelg1m0z/
and the result: http://s28.postimg.org/srvcwj065/Capture2.jpg
now it's a static page with search clause where by id 1, but i need an automatically link show by id on each row.
<?php
include "connect.php";
$sql = "select * from studenti where id='1'";
$query = mysql_query($sql) or die (mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Nume</td>
<td>Localitate</td>
<td>Judet</td>
<td>Sector Financiar</td>
<td>Link</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>
<?php } ?>
</table>
<?php
include "connect.php";
$sql1 = "select * from certificari where id='1' ";
$query = mysql_query($sql1) or die (mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Denumire certificare</td>
<td>Serie si numar certificare</td>
<td>Data certificarii</td>
<td>Valabilitate certificare</td>
<td>Sector Financiar</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['serie_numar']; ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['valabilitate']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<?php } ?>
</table>
You should not use the mysql_ functions since they are depricated and are very vulnerable to SQL injection attacks. Instead, I will use MySQLi in this answer, but you could also use PDO if you want to.
To display records for different ID's based on what the user selects you can pass an ID in the page URL. When you link to the page you add the desired ID to the address like this:
Link
The value of the variable id will now be available in page.php as $_GET['id'].
The actual PHP in your page would then look something like the code below. I have left out some of your HTML for brevity, but you can just add it int.
//Connect to the DB. Might want to put this in your connect.php and include it.
$db = new mysqli('localhost', 'user', 'pass', 'db');
//Prepare the statement. The ? will be your ID.
$statment = $db->prepare("SELECT * FROM studenti WHERE id = ?");
//Bind the parameters, so that the first (and only) question mark is turned into your ID.
//The 'i' means integer, if you store the id as a string your should use 's' instead.
$statement->bind_param('i', $_GET['id']);
//Execute the query.
$statement->execute();
//Get the results.
$result = $statement->get_result();
//Iterate over it to create the output.
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>
</tr>
<?
}
//Then do the same thing for the table certificari.
//Note that you only need to connect to the DB once.
This beginners guide to MySQLi is very helpful if you need some more guidance.

Nothing on my page with an exception of the header after SQL query and table

I've been troubleshooting for a while now, and I really can't find any answer. Basically this is what it looks like:
When I type the name of a real user: Nothing posted on the page with an exception of the header
When I type a fake user: User has not been found (direct translation from Norwegian in the code)
<?php
include_once("moduler/head.php");
$user = $_GET['user'];
$query1 = $con->query("SELECT * FROM players WHERE name='$user'");
echo $con->error;
if ($query1->num_rows == 0) {
echo "<h1>Ingen spiller funnet med: $user.</h1>";
return;
}
while ($row == mysqli_fetch_array($query1)) {
$styrkeLvl = $row['styrke'];
$beskyttelseLvl = $row['beskyttelse'];
$bueskytingLvl = $row['bueskyting'];
$trehuggingLvl = $row['trehugging'];
$gruvedriftLvl = $row['gruvedrift'];
$fiskingLvl = $row['fisking'];
$kills = $row['kills'];
$deaths = $row['deaths'];
$rank = $row['rank'];
$money = $row['money'];
$donstatus = $row['donationstatus'];
$lastlogin = $row['lastlogin'];
$regtime = $row['registrationtime'];
$rankName = getRankString($rank);
?>
<h1><?php echo $user; ?></h1>
<table class=\"userView\">
<tbody>
<tr><td>Brukerstatus</td>
<td><?php echo $rankName; ?></td>
</tr>
<tr><td>Donasjon status</td>
<td>D<?php echo $donstatus; ?></td>
</tr>
<tr><td>Styrke level</td>
<td><?php echo $styrkeLvl; ?></td>
</tr>
<tr><td>Beskyttelse level</td>
<td><?php echo $beskyttelseLvl; ?></td>
</tr>
<tr><td>Bueskyting level</td>
<td><?php echo $bueskytingLvl; ?></td>
</tr>
<tr><td>Trehugging level</td>
<td><?php echo $trehuggingLvl; ?></td>
</tr>
<tr><td>Fisking level</td>
<td><?php echo $fiskingLvl; ?></td>
</tr>
<tr><td>Drap</td>
<td><?php echo $kills; ?></td>
</tr>
<tr><td>Dødsfall</td>
<td><?php echo $deaths; ?></td>
</tr>
<tr><td>Sist pålogget</td>
<td><?php echo $lastlogin; ?></td>
</tr>
<tr><td>Registreringsdato</td>
<td><?php echo $regtime; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
Any ideas? I've also tried running the entire thing in an echo with no result
Thanks
All your variables inside the while loop are only available inside the while loop. so you cannot echo them in the tables. so <td><?php echo $rankName; ?></td> will give you an undefined variable error. The same applies for the other variables you are trying to display.
To avoid this you should try to declare your variables globally, or put your table inside the loop.
Also the other issue is that as it is the code inside the loop will never execute because of the double equal (==). You should use double equal (==) for comparison and single equal (=) for assignment.
So you have to change while ($row == mysqli_fetch_array($query1)) to while ($row = mysqli_fetch_array($query1))

Categories