running sql on link click - php

please view the following code.
<?php foreach($rows as $row): ?>
<tr>
<td><?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8');?></td>
<td><?php echo htmlentities($row['id'], ENT_QUOTES, 'UTF-8');?></td>
<td><?php echo htmlentities($row['starthol'], ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlentities($row['endhol'], ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo htmlentities($row['days'], ENT_QUOTES, 'UTF-8'); ?></td>
<td>Approve</td>
<td>Reject</td>
</tr>
This populate a table from a previously ran sql query. Basically, when approve is clicked,I want to run an SQL query that uses the id of the the row and updates a column in the table called "active".
UPDATE holidays
SET active = "active"
WHERE holid = getid
Any advice on how to do this?
I am a php novice, so please go easy on me. Thank you.

You should consider learning how to GET and POST
<td><a href="active.php?id=?"<?php echo $row['id']; ?> >Approve</a></td>
Server side code active.php(sample dont copy paste you must use post to update db state not get also mysql_* depricated)
$id = mysql_real_escape_string($_GET['id']);
mysql_query("UPDATE holidays
SET active = 'active'
WHERE holid = $id")or die(mysql_error());
header('location:listingpage.php?msg=approved');

Are you able to get your query to run and return data? Assuming you have data, you will want to update your foreach() loop. If you want the loop to work over more than one line, you'll have to use curly braces ({ })
So your code would look more like:
<?php foreach($rows as $row) { ?>
...
<?php } ?>

Related

Using links in HTML tables associated with the right table row/person/id in the database

Im a begginer in the whole SO and programming enviroment, and here comes my first question(which i tried finding in here but with no luck so far..)
I have an html table that prints specific patients of the doctor that is active (in a simple web-application that i created). The doctor gets filtered using $_SESSIONS global var in php.
My problem is that i want to implement a few actions in the same HTML table that displays the patients, like view history (which is stored in a local DB table, from an HTML from using POST method) and create a new form for the same person.
I saw that providing the table with row.ids could be a solution, but my table isn't static, i woulda like for the user to have the option to add/delete patients..
Following is a sample of my code that displays the HTML table "Existing Patients" :
<table>
<tr>
<th>Patient Id</th><th>Patient Name</th><th>Phone Number</th><th>Email</th><th>History</th>
<th>Add a Follow Up Visit</th><th>Remove Patient</th>
</tr>
<?php $sql = "SELECT * FROM patients WHERE Doctor_ID = $usersid";
$result = $pdo->query($sql);
if($result->rowCount() > 0){
while($row = $result->fetch()){?>
<tr>
<td><?php echo $row['Patient_id']; ?></td>
<td><?php echo $row['Patient_name'] ; ?></td>
<td><?php echo $row['Phonenum'] ; ?></td>
<td><?php echo $row['Email']; ?></td>
<td><?php echo "<a href='/patienthistory.php'</a>" . 'Previous Visits'; ?></td> //problem here
<td><?php echo "<a href='/patientform.html'</a>" . 'Add Follow Up' ; }?></td> //and here
</tr>
</table>
I want the last 2 lines to connect immediately to the specific database stored Patient_id and retrieve the existing information stored there from the patients previous visits.
I hope i gave enough of a description, but if there is any more info neccesary, please let me know.
Thank you in advance!
You can specify the patient ID as a query parameter in the URL that you're linking to. Also the HTML you're generating for your links is invalid currently. And the second link is going to need to go to a .php script, not a .html file, if you want it to execute code and fetch data.
Try this example:
<td><?php echo "<a href='/patienthistory.php?id='".$row['Patient_id']."'>Previous Visits</a>"; ?></td>
<td><?php echo "<a href='/patientform.php?id='".$row['Patient_id']."'>Add Follow up</a>"; ?></td>
Then in each of the PHP scripts, use $_GET["id"] to retrieve that ID of the patient and use that in a query
e.g.
$patientID = $_GET["id"];

Handling loops and combining values in PHP

I have a table like this.
author_id author_state author_name
1 CA Hello
2 MI World
3 CA How
4 MI Are
Please let me know how can I achieve this. In other words, I should loop it and combine the state values first and display the details.
Here is my PHP Code
<table>
<tr><td>Author ID</td><td>Author State</td><td>Author Name</td></tr>
<?php
$row_data = mysql_query("select * from author" );
while($row = mysql_fetch_array($row_data) ) {
?>
<tr>
<td><?php echo $row['author_id']; ?></td>
<td><?php echo $row['author_state']; ?></td>
<td><?php echo $row['author_name']; ?></td>
</tr>
<?php } ?>
</table>
My Expected output is to combine the state and display something like below:
Author ID Author Name
=======================================
CA
==
1 Hello
3 How
MI
==
2 World
4 Are
You can do this fairly easily, by sorting your output by author_state, and using a variable to keep track of which state you're outputting. If the state changes, then you simply output the state as a new header.
You can take advantage of the SQL order by clause so that you don't have to sort the data yourself.
Here is an edit of your code, where I've simply added the order by to your mysql_query(), and added a variable for $current_state.
<table>
<tr><td>Author ID</td><td>Author State</td><td>Author Name</td></tr>
<?php
$current_state = "";
$row_data = mysql_query("select * from author order by author_state" );
while($row = mysql_fetch_array($row_data) ) {
// Output the "new" state
if ($row['author_state'] != $current_state)
{
echo "<tr><td colspan=\"3\">{$row['author_state'}}</td></tr>";
$current_state = $row['author_state'];
}
?>
<tr>
<td><?php echo $row['author_id']; ?></td>
<td><?php echo $row['author_state']; ?></td>
<td><?php echo $row['author_name']; ?></td>
</tr>
<?php } ?>
</table>
As a side note, I would strongly encourage you to look into using mysqli or PDO instead of the mysql_* functions. The mysql_* functions are only availble in very old versions of PHP (they were completely removed in PHP7).

How to safely use $_GET['id'] to get details from database by selecting html table row

So, I have this working code to click on a link for showing details from one row of a database on another page in input text boxes.:
<?php
foreach ($allCentrifuge as $list) {
$id = $list['id'];
?>
<tr>
<td><?php echo $list['experiment'] ?></td>
<td><?php echo $list['project_name'] ?></td>
<td><?php echo $list['project_date'] ?></td>
<td>Details</td>
</tr>
<?php
}
?>
And this function to get those details:
public function fetchCentrifuge()
{
$uid = $_GET['id'];
try {
$stmt = $this->dbconn->prepare("SELECT * FROM centrifuge WHERE id = ?");
$stmt->execute(array($uid));
return $stmt->fetchAll();
}
So, obvious from all the subjects I read, this is dangerous to SQL injection. But how do I use this code with prepared statements? I can't seem to get it work with the id placeholder.
Looks good as is. You could additionally cast your input var to INT.
(int)$_GET['id'] or intval($_GET['id'])

Trouble with displaying the result of a select in a table

So, I have an sql table that doesn't have a unique column. I have to display it in a table by it's ID. But, like I said, the ID is not unique
OBSĀ¹: Notice that it comes from a select statement
OBSĀ²: the user id can appear in the select from 0 up to 4 times (according to id_questionario)
So, the thing is, how am I going to make a table that will show only one time user_id but will display by it's side all the melhor_tentativa according to id_questionario (notice that id_questionario is not to be displayed, only so I can display the melhor_tentativa in the correct order)
What I understand is that I have lines that should be columns. I've been trying for 2 days to figure this out and turns out I can't. Thought that it might be miss in how I made my sql
This is how the table looks
This is how the sql table looks
And bellow is the code I made for it
<?php
$count = 0;
$nota1 = 0;
$nota2 = 0;
$nota3 = 0;
$nota4 = 0;
?>
<?php
foreach($notas as $nota):
$total = 0;
?>
<tr>
<td><?php echo $nota['user_id']?></td>
<td>NULL</td>
<?php
if($nota['id_questionario']==1){
$nota1 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota1 ?></td>
<?php
if($nota['id_questionario']==2){
$nota2 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota2 ?></td>
<?php
if($nota['id_questionario']==3){
$nota3 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota3 ?></td>
<?php
if($nota['id_questionario']==4){
$nota4 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota4 ?></td>
<td><?php $total = ($nota1 + $nota2 + $nota3 + $nota4)/4; echo $total; ?></td>
</tr>
<?php $count++; ?>
<?php
endforeach;
echo $count;
?>
</table>
P.S: I know that total is completly wrong ;)
After a bunch of work I figured this out. So what I did was the following. I had that table which would show 4 results for a one ID. I made a view out of this table
Then I did a subquery from that view, so I would be able to show all results in one line
Hope it helps anybody in the future :)

Codeigniter: Join 3 tables and display data in view

So I have 3 tables I wish to join.
I am building an app i Codeigniter and I have 3 tables
Client:
-id
-phone_number
-hospital_id
-smc_status
-testing_center_id
Hospital
-id
-name
Testing_center
-id
-name
In the model,I have this:
public function get_clients()
{
if($slug === FALSE)
{
$this->db->select('clients.*');
$this->db->from('clients');
$this->db->join('hospital', 'clients.id = hospital.id');
$this->db->join('testing_center', 'clients.id = testing_center.id');
$query = $this->db->get();
return $query->result_array();
}
$query = $this->db->get_where('clients');
return $query->row_array();
}
In the view I have:
<tbody>
<?php foreach ($clients as $client_item): ?>
<tr>
<td><?php echo $client_item['phone_number'] ?></td>
<td><?php echo $client_item['smc_status'] ?></td>
<td><?php echo $client_item['hospital_id'] ?></td> //i wish to have the hospital name here
<td><?php echo $client_item['testing_center_id'] ?></td> //i wish to have the testing center name here
<td><?php echo $client_item['language'] ?></td>
<td>View</td>
</tr>
<?php endforeach ?>
</tbody>
But that is because I have failed to show the hospital name and the testing center name on the third and fourth td. How can I go about that? I tried a few techniques that just did not seem to work for some reason. Please advise
You're only selecting the values from the clients table. You need to select the columns from the other tables as well
$this->db->select('clients.id,
clients.phone_number,
clients.smc_status,
clients.language,
hospital.name AS hospital_name,
testing_center.name AS testing_center_name');
Then you can access them by
<?php echo $client_item['hospital_name'] ?>
<?php echo $client_item['testing_center_name'] ?>
EDIT: Also you shouldn't use SELECT *, which clients.* is doing. Updated my code.
What happens if you try this:
$this->db->join('hospital', 'hospital.id = clients.id');
$this->db->join('testing_center', 'testing_center.id = clients.id');
instead of this:
$this->db->join('hospital', 'clients.id = hospital.id');
$this->db->join('testing_center', 'clients.id = testing_center.id');
also check
client*s* and client if they are the same everywhere
And also change as Nerd proposed:
$this->db->select('clients.*');
to:
$this->db->select('*');
It sholud be like this
$this->db->join('hospital', 'clients.hospital_id = hospital.id');
$this->db->join('testing_center', 'clients.testing_center_id = testing_center.id');

Categories