Retrieve User data from user id - php

I have just started learning PHP, so please bear with me. I have two tables in my database, users and appointments tables. The appointments table has idUsers as a foreign key for the id field in users table. I'm trying to display data from appointments table based on current user logged in. The problem is, it displays data from all users and not the one who logged in.
I'm using fetch_assoc() to get the data from the database.
<?php
while($row = $rows->fetch_assoc()): ?>
<td><?php echo $row['idApp'] ?></td>
<td class="col-md-10"><?php echo $row['fname'] ?> </td>
<td class="col-md-10"><?php echo $row['lname'] ?> </td>
<td class="col-md-10"><?php echo $row['email'] ?> </td>
<td class="col-md-10"><?php echo $row['phone'] ?> </td>
<td class="col-md-10"><?php echo $row['date'] ?> </td>
<td class="col-md-10"><?php echo $row['time'] ?> </td>
<td class="col-md-10"><?php echo $row['message'] ?> </td>
</tr>
<?php endwhile; ?>
The expected results would be for the user to log into the system and only see the data , that they have stored against their id.

you should to make "one to one relationship". for example you have two tables users and posts. the posts table must be some like: id,user_id,title,...
so when you want to get posts articles by specific user run this query:
$query = 'SELECT * FROM `posts` WHERE `user_id`= '.$_GET['id'].'';
however do not foget SQL injection!
learn PDO :)

Related

How to fetch a specific data from a table and display that specific data in another page?

Hi there:) I have created a table where there's a list of products.
if ($result) {
echo '<table align="center">
<tr><th><b>ID</b></th><th><b>Name</b></th><th><b>Made_In</b>
</th><th><b>Price</b></th></tr>';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr><td >' . $row['ID'] . '</td>
<td >' . $row['Name'] . '</td>
<td >' . $row['Made_In'] . '</td>
<td >' . $row['Price'] . '</td></tr>';
}
echo '</table>';
Expected Output:
When the user want to see more details about this particular product, for example Product A. The user just have to click the product name and it will take the user to a new page where the user can see more details about the Product A such as description, ingredient, company's details etc.
Just so you know all of this is using the same table from the database called Product.
I have tried make the product's name as a link but i don't know how to fetch a specific data from the table to a new page.
I know this seems easy but i can't find the answer anywhere. Thank you for your time
<?php
if($result){?>
<table align="center">
<tr><th><b>ID</b></th><th><b>Name</b></th><th><b>Made_In</b>
</th><th><b>Price</b></th></tr>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {?>
<tr>
<td ><?php echo $row['ID'];?> </td>
<td ><?php echo $row['Name']; ?></td>
<td ><?php echo $row['Made_In'];?></td>
<td ><?php echo $row['Price'];?></td>
</tr>
}
<?php } ?>
you need to fetch the particular record in new page.
eg: $id = $_GET['id']; // this will receive from url eg: example.com/products?id=2
and fetch the records using that $id.
Now you can use show table with fetched detail.
Suggestion: you can use php inside html. So, its better to write html table tags and open/close php tags where necessary. This will make your code easier to understand.

mysql data did not appear according to the id in html table

I have two mysql table in the same database. First table is 'coi_system' and second, 'monthly_saving' which the id is refer from the first table. I want to display these two table data into this table.
So, the 'monthly_saving' should be displayed in that Cost Saving Monthly html column refering to the id of the 'coi_system' but it turns out it display all the data of 'Jan 17' column into each of the result that makes each result display double like this. Heres the code,
<?php
$result = mysqli_query($conn,"SELECT * FROM coi_system, monthly_saving where monthly_saving.id=coi_system.id");
while ($row = mysqli_fetch_assoc($result)) {
$accountcode=$row['accountcode'];
$department=$row['department'];
$person_in_charge=$row['person_in_charge'];
$project_title=$row['project_title'];
$objective=$row['objective'];
$how_to_do=$row['how_to_do'];
$activities=$row['activities'];
$project_started=$row['project_started'];
$project_completed=$row['project_completed'];
$target_cost_saving=$row['target_cost_saving'];
$costsaving_afterjustification=$row['costsaving_afterjustification'];
$costsaving_monthly=$row['costsaving_monthly'];
$sum_of_month=$row['sum_of_month'];
$targetsaving_currentmonth=$row['targetsaving_currentmonth'];
$avg_monthly_saving=$row['avg_monthly_saving'];
$todays_date=$row['todays_date'];
$current_month=$row['current_month'];
$Jan=$row['Jan'];
$Feb=$row['Feb'];
$Mac=$row['Mac'];
$Apr=$row['Apr'];
$May=$row['May'];
$Jun=$row['Jun'];
$Jul=$row['Jul'];
$Aug=$row['Aug'];
$Sep=$row['Sep'];
$Oct=$row['Oct'];
$Nov=$row['Nov'];
$Dis=$row['Dis'];
$id=$row['id'];
?>
<div class="scroll">
<tr id="row1">
<td> </td>
<td id="accountcode_row1"><div> <?php echo $accountcode;?></div></td>
<td id="department_row1"><div> <?php echo $department;?></div></td>
<td id="pic_row1"><div> <?php echo $person_in_charge;?></div></td>
<td id="protitle_row1"><div> <?php echo $project_title;?></div></td>
<td id="objective_row1"><div> <?php echo $objective;?></div></td>
<td id="howtodo_row1"><div><?php echo $how_to_do;?></div></td>
<td id="activities_row1"><div> <?php echo $activities;?></div></td>
<td id="prostart_row1"><div> <?php echo $project_started;?></div></td>
<td id="procompl_row1"><div> <?php echo $project_completed;?></div></td>
<td id="targetcost_row1"><div> <?php echo $target_cost_saving;?></div></td>
<td id="costafter_row1"><div> <?php echo $costsaving_afterjustification;?></div></td>
<td><div><?php echo $Jan;?></div></td>
<td><div><?php echo $Feb;?></div></td>
<td><div><?php echo $Mac;?></div></td>
<td><div><?php echo $Apr;?></div></td>
<td><div><?php echo $May;?></div></td>
<td><div><?php echo $Jun;?></div></td>
<td><div><?php echo $Jul;?></div></td>
<td><div><?php echo $Aug;?></div></td>
<td><div><?php echo $Sep;?></div></td>
<td><div><?php echo $Oct;?></div></td>
<td><div><?php echo $Nov;?></div></td>
<td><div><?php echo $Dis;?></div></td>
<td><div><?php echo $sum_of_month; ?></div></td>
<td><div><?php echo $avg_monthly_saving; ?></div></td>
<td><div><?php echo $todays_date; ?></div></td>
<td><div><?php echo $current_month; ?></div></td>
<td><div></div></td>
<td><div> <?php echo $targetsaving_currentmonth;?></div></td>
I'm not sure where is the mistake but I have tried write it like this, $result = mysqli_query($conn,"SELECT * FROM coi_system, monthly_saving where id"); but thats not the solution. I truly need someones help in this as I'm not good with mysqli. Thankyou in advance!
UPDATE
This is table monthly_saving and this from 'coi_system' where the id is referred. Solved, but new problem, currently theres only 2 rows of results that appear which that has the 'monthly_saving' data. The rest didnt appear because they dont have any 'monthly_saving' data. So how to display them all?
Try this
SELECT * from coi_system, monthly_saving where monthly_saving.id=coi_system.id;
SELECT coi_system.*, monthly_saving.* FROM coi_system RIGHT JOIN monthly_saving ON monthly_saving.id=coi_system.id;
The right join would solve your problem if monthly saving is null.
SELECT * FROM coi_system INNER JOIN monthly_saving ON 'monthly_saving.id' = 'coi_system.id'

multiple id in sql how to expload and get the id values in codeigniter

Multiple id in mysql table how to expload and get the particular id values in another mysql table for codeigniter,
Pages view images added,
View page
`<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td><?=$loop->departments_list_id ?></td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>`
Controller Page
`public function company_settings() {
$data['company_branch'] = $this->settings_model->company_branch();
$this->load->view('settings/company_settings',$data);`
Model Page
`function company_branch(){
$this->db->select('company_branch.*,
company_departments.department_name as departments_name ')-
>from('company_branch');
$this->db- >join('company_departments','
company_branch.departments_list_id = company_departments.id');
$query = $this->db->get();
return $query->result();
}`
How to display the `departments_list_id ` to department names in view
page,
The image is company_branch and company_department mysql table view,
browser View page,
View Page
`<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td>
<?php
foreach(explode(",",$loop->departments_list_id) as $department)
{ $depart=$this->db->query("select department_name from company_departments where id=".$department)->row()->department_name;
echo $depart.',<br />';
}
?>
</td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>`
You can't enter some id into 1 field. You can add some fields to another id. Example field: departement_id_1, departement_id_2, departement_id_4, departement_id_5 etc depending on need.
you can change your sql select to this :
select branc.*,
(select dept.departement_name as departement_name_1 from company_departement as dept where branc.departement_id_1=dept.id),
(select dept.departement_name as departement_name_2 from company_departement as dept where branc.departement_id_2=dept.id),
(select dept.departement_name as departement_name_3 from company_departement as dept where branc.departement_id_2=dept.id)
from company_branc as b
then you can use script from your form
<?php foreach($company_branch as $loop){ ?>
<tr>
<td></td>
<td><?=$loop->branch_name ?></td>
<td><?=$loop->branch_head ?></td>
<td><?=$loop->departments_name_1 ?></br>
<?=$loop->departments_name_2 ?></br>
<?=$loop->departments_name_3 ?>
</td>
<td><?=$loop->write_date ?></td>
</tr>
<?php } ?>
i hope this helps

create dynamic reports using core php and mysql

Currently we are doing a project to manage projects and our client need a dynamic reports which he can get Time Sheet Project wise.
Let me explain the situation.
3 database tables.
Project table, user table and task table.
This is the sample reports format with a sample data, but structure is the same.
Multiple users can work on a single project and user can work on multiple projects at the same time.
This is what i have tried to up to now.
<?php
/////get projects which are not finished/////
$sql_project = "SELECT * FROM project where P_Status!='finished'";
$result_project = mysql_query($sql_project) or die("Error in query: $sql_project.".mysql_error());
////////get users from the users table
$sql_getUsers="select * from tbl_user where status='Active' order by U_Name";
$result_getUsers=mysql_query($sql_getUsers);
?>
<tr>
<td>Project</td>
<td>Client</td>
<td>Estimated Cost</td>
<td >Total spent to date </td>
<?php
while($row_getUsers=mysql_fetch_array($result_getUsers))
{
?>
<td ><?php echo $row_getUsers['U_Name']; ?></td>
<?php
}
?>
</tr>
<?php
while($row_project=mysql_fetch_array($result_project))
{
$individualProject=trim($row_project['P_Title']);
$sql_tasks = "Select SUM(Time)as totalTime from tbl_txn where project like '%$individualProject%' and Date<='$endDate'";
$result_sql = mysql_query($sql_tasks) or die("Error in query: $sql_file_no.".mysql_error());
?>
<?php
while($row_sql_tasks=mysql_fetch_array($result_sql))
{
?>
<tr>
<td><?php echo $row_project['P_Title']; ?></td>
<td ><?php echo $row_project['P_Client']; ?></td>
<td ><?php echo $row_project['estimatedCost']; ?></td>
<td ><?php echo round($row_sql_tasks['totalTime']); ?></td>
<td><?php echo round($row_getIndiUserTime['Time']); ?></td>
<?php
}
}
?>
I have managed to display the users, but i have no idea how to display users have work on each project?.
on project A, how much time User 1 has spent and user 2 like wise.
hope i have clearly mentioned about the problem.
can anyone help me on this please?
Thanks in advance
The idea to do this is to join a table in mysql.
Here is a resource you can learn how to use mysql join table and show the data accordingly. Hope this will help you.

Getting contenteditable div's current value and update database

I'm new to web development and I'm trying to create editable database interface. I have got checkboxes in every row and an edit button. I need to update table(in database) when button clicked(rows that are checked). However I can't get current values in cells. I tried contentedittable div to display attiributes.
<?php while ( $rows = mysql_fetch_array($result)): ?>
<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<?php echo $rows['UniqueID']; ?>]" type="checkbox" id="checkbox[<?php echo $rows['UniqueID']; ?>]" value="<?php echo $rows['UniqueID']; ?>"></td>
<td bgcolor="#FFFFFF"><div contenteditable><?php echo $rows['Timestamp']; ?></div></td>
<td bgcolor="#FFFFFF"><div contenteditable><?php echo $rows['Name']; ?></div></td>
<td bgcolor="#FFFFFF"><div contenteditable><?php echo $rows['Email']; ?></div></td>
<?php endwhile; ?>
I also tried to give unique names to divs, but it didn't work. I want to get current values from there if possible.
<?php
if( ! empty($_POST['edit']) ){
$query = 'UPDATE `asd` SET `Timestamp` = '????' WHERE `UniqueID`='.(int)$id;
mysql_query($query);
}
?>
Checkboxes work fine when I delete rows from table. Any suggestions ?
Thanks.
There are several javascript libraries that help with this problem:
x-editable http://vitalets.github.io/x-editable/ (bootstrap)
datatables https://datatables.net/ (jquery)
I had a similar issue that I solved here:
http://www.abrandao.com/2019/12/saving-contenteditable-html-using-php/
basically, it involves reading the HTML and using PHP parser to exctract the tag and the update the data

Categories