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

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']."')";

Related

Can I run a separate query on each row instead of printing all the data with a single query?

$query = mysqli_query($conn,SELECT `date`,`name`,`surname`, COUNT(*) AS abc FROM `trial` WHERE `name`='asd' GROUP BY `date`,`name`,`surname` ORDER BY `date` DESC);
<?php while ($row = mysqli_fetch_array($query)) : ?>
<td><?php echo $row['abc']; ?></td>
<td><?php echo $row['Can I print a value returned from a different query, not abc? ']; ?></td>
<?php endwhile; ?>
Can I run a separate query for the second row instead?
Combine them into a single query that gets both counts for each surname.
<?php
$query = mysqli_query($conn,"
SELECT `date`,`surname`, SUM(name = 'asd') AS asd_count, SUM(name = 'opr') AS opr_count
FROM `trial`
WHERE `name`= IN ('asd', 'opr')
GROUP BY `date`,`surname`
ORDER BY `date` DESC");
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)): ?>
<tr>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['surname']; ?></td>
<td><?php echo $row['asd_count']; ?></td>
<td><?php echo $row['opr_count']; ?></td>
</tr>
<?php endwhile; ?>
See multiple query same table but in different columns mysql
You also need to start a new <tr> for each row returned by the query.

Trying to get property of non-object using OCI connection

I'm new in PHP with OCI connection. I want to retrieve some data from database and insert it into a table form. But it keep show an error
Trying to get property 'attribute' of non-object
I have try to use oci_fetch object / oci_fetch_array, but it still the same. I also have followed some tutorial but it doesn't help.
This is for my mini project for this semester.
Here my source code:
$sql="SELECT borrow.book_id, book_title, borrow.stud_id, stud_name, book_bdate, return_date, due_date
FROM book
JOIN borrow
ON book.book_id = borrow.book_id
JOIN student
ON borrow.stud_id = student.stud_id
where borrow.stud_id = '$stud_id'
ORDER BY 5 DESC ";
$query=oci_parse($link,$sql) or die ("error here!");
oci_execute($query);
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row->stud_id; ?></td>
<td><?php echo $row->book_id; ?></td>
<td><?php echo $row->book_title; ?></td>
<td><?php echo $row->book_bdate; ?></td>
<td><?php echo $row->due_date; ?></td>
<td><?php echo $row->return_date; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row->book_id; ?>'>Update</a></center>
</td>
<tr>
<?php
}
}
oci_close($link);
?>
Oracle returns field names as uppercase by default so you need to use uppercase indexes like so:
Here the solution where I got.
Btw thank you everyone for helping me.
Correct Your while loop You are fetching data as array:
oci_fetch_array($query, OCI_ASSOC) // return associated array
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row['stud_id']; ?></td>
<td><?php echo $row['book_id']; ?></td>
<td><?php echo $row['book_title']; ?></td>
<td><?php echo $row['book_bdate']; ?></td>
<td><?php echo $row['due_date']; ?></td>
<td><?php echo $row['return_date']; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row['book_id']; ?>'>Update</a></center>
</td>
<tr>
<?php
}

How to make images align side by side when fetch multiple images in a single column that belongs to the same category

I am trying to fetch multiple images from a single columns which belongs to the same category and I want this images to be lay side by side with their category. When I fetched the images with their category the category appeared number of times the images.
Take a look at the image below, ABUA appeared once in the user table but has three images in the upload table likewise Eksu which has two images. I do not want the display to be like this, I want the images to align side by side and the details appear once.
My sql query is below:
<?php
//Education
$e = mysqli_query($mysqli, "SELECT * FROM education where fid = '$id' group by schools ");
while($edu = mysqli_fetch_array($e)){
$eid = $edu['fid'];
$sc = $edu['schools'];
$now=mysqli_query($mysqli, "Select distinct * from uploads WHERE fid='$eid' AND category = 'education' and sch='$sc' order by '$sc' ");
while($resultsn=mysqli_fetch_array($now)){
$tempr = explode(',',$resultsn['img_name'] );
foreach($tempr as $imager){
$img = "<img src='../cert/".$imager."' class='img-thumbnail' width='50' height='50'/>";
?>
<tr>
<td><?php echo $img; ?></td>
<td><?php echo $edu['schools']; ?></td>
<td><?php echo $edu['course']; ?></td>
<td><?php echo $edu['qualification']; ?></td>
<td><?php echo $edu['years']; ?></td>
</tr>
<?php
}
}
//}
}
?>
Try this:
while($resultsn=mysqli_fetch_array($now))
{
$tempr = explode(',',$resultsn['img_name'] );
$imgs = '';
foreach($tempr as $imager)
{
$imgs .= "<img src='../cert/".$imager."' class='img-thumbnail' style='display: inline-block;' width='50' height='50'/> ";
}
?>
<tr>
<td><?php echo $imgs; ?></td>
<td><?php echo $edu['schools']; ?></td>
<td><?php echo $edu['course']; ?></td>
<td><?php echo $edu['qualification']; ?></td>
<td><?php echo $edu['years']; ?></td>
</tr>
<?php}?>
You're creating a table row for each one the entries in your second while loop.
If you want the images side by side, you should create the table row inside your first loop and have only image <td> elements added in the second while loop.
After that loop is closed, echo the remaining `' elements inside the initial while loop to display the other fields of your table. You'll have one row with all images for that category instead of multiple rows.
Try this one:
<?php
//Education
$e = mysqli_query($mysqli, "SELECT * FROM education where fid = '$id' group by schools ");
while($edu = mysqli_fetch_array($e))
{
$eid = $edu['fid'];
$sc = $edu['schools'];
$now=mysqli_query($mysqli, "Select group_concat(img_name) as img_name from uploads WHERE fid='$eid' AND category = 'education' and sch='$sc' order by '$sc' ");
while($resultsn=mysqli_fetch_array($now))
{
$tempr = explode(',',$resultsn['img_name'] );
foreach($tempr as $imager){
$imgs += "<img src='../cert/".$imager."' class='img-thumbnail' width='50' height='50' style="margin-right:10px;"/> ";
}
}
?>
<tr>
<td><?php echo $imgs; ?></td>
<td><?php echo $edu['schools']; ?></td>
<td><?php echo $edu['course']; ?></td>
<td><?php echo $edu['qualification']; ?></td>
<td><?php echo $edu['years']; ?></td>
</tr>
<?php
}
?>

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.

Issue with show all users in the table with pdo

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!

Categories