I am developing a document management system, so far I'm able to log in and upload documents based on the user that's logged in. I've also managed to construct the code so it captures the records assigned to that user and display them in a table. So I am able to log in as different users and see what I've uploaded as them users individually. However, the only problem is that the script is only pulling out one row of data for each users, when there are multiple instances in the database. See the code below:
<!-- start of php code to query and display documents -->
<?php
$sql = mysqli_query($conn, "SELECT upload.personalid, upload.file, upload.type, upload.size, person.personalid FROM upload inner join person on upload.personalid=person.personalid where person.username='$uname '") or die (mysqli_error($conn));
$filedata= ($sql) ? mysqli_fetch_array($sql) : false;
{
if($filedata){
?>
<tr>
<td><?php echo $filedata['file'] ?></td>
<td><?php echo $filedata['type'] ?></td>
<td><?php echo $filedata['size'] ?> Bytes</td>
</tr>
<?php
}
else {
echo 'No files found';
}
}
?>
<!-- end of php code to query and display documents -->
This all works fine, I just want it to display all the rows of data assigned to the logged in user, instead of one. How do I do this?
Thanks,
Sohail.
You're close. You need a while loop. Replace
$filedata= ($sql) ? mysqli_fetch_array($sql) : false;
if($filedata){
With
while($filedata = mysqli_fetch_array($sql)) {
You can change it so that you loop through each returned record like this:
<?php
$sql = mysqli_query($conn, "SELECT upload.personalid, upload.file, upload.type, upload.size, person.personalid FROM upload inner join person on upload.personalid=person.personalid where person.username='$uname '") or die (mysqli_error($conn));
if (mysqli_num_rows($sql) > 0) {
while ($filedata = mysqli_fetch_array($sql)) {
?>
<tr>
<td><?php echo $filedata['file']; ?></td>
<td><?php echo $filedata['type']; ?></td>
<td><?php echo $filedata['size']; ?> Bytes</td>
</tr>
<?php
}
} else {
echo 'No files found';
}
?>
Related
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"];
Here when i am searching key in search filter for shorting row from database based on search filter using pagination its showing correct but when i am clicking for second page its leaving the search filter and catching second query and which is only for pagination,
Here is my pagination code
<ul class="pagination">
<?php
for($i=1;$i<=$total_page;$i++)
echo"<li class='".($page_id == $i? 'active' : '')."'><a href='index.php?page=".$i."'>$i</a></li>";
?>
</ul>
and here is code when clicking for second page its leaving search session and going to again in normal pagination part
<?php
$total_num_page=1;
if(isset($_GET['page']))
{
$page_id=$_GET['page'];
}
else
{
$page_id=1;
}
if(isset($_POST['submitSearch']) || $_SESSION['search'])
{
if($_POST['submitSearch']){
$_SESSION['search']=$_POST['search'];
}
$all_post_query= "select * from files where recieved_by like '%". $_SESSION["search"]. "%' or processed_by like '%".$_SESSION["search"]."%' or purpose like '%".$_SESSION["search"]."%' or file_name like '%".$_SESSION["search"]."%' order by date desc";
$all_post_run=mysqli_query($con,$all_post_query);
$all_post=mysqli_num_rows( $all_post_run);
$total_page=ceil($all_post/$total_num_page);
$page_start_from=($page_id-1)*$total_num_page;
}
else
{
$all_post_query="select * from files order by date desc";
$all_post_run=mysqli_query($con,$all_post_query);
$all_post=mysqli_num_rows($all_post_run);
$total_page=ceil($all_post/$total_num_page);
$page_start_from=($page_id-1)*$total_num_page;
}?>
and this is code where fetching the data to table from database
<?php
$p_query="select * from files order by date desc limit $page_start_from, $total_num_page";
$p_run=mysqli_query($con,$p_query);
if(mysqli_num_rows($p_run)){
while($row=mysqli_fetch_array($p_run))
{
$c_id=$row['id'];
$file=$row['file_name'];
$purpose=$row['purpose'];
$recieve=$row['recieved_by'];
$processed=$row['processed_by'];
$address=$row['address'];
$contact=$row['contact_no'];
$date=$row['date'];
?>
<tr>
<td><?php echo $c_id;?></td>
<td><?php echo $file;?></td>
<td><?php echo $purpose;?></td>
<td><?php echo $recieve;?></td>
<td><?php echo $processed;?></td>
<td><?php echo $address;?></td>
<td><?php echo $contact;?></td>
<td><?php echo $date;?></td>
?>
So help me please and I know code is vulnerable to SQL injection that I will prevent this after completing .
There are several issues with your code:
First of all, when you select the data to display the table, you don't take the search param into consideration, so you will never display records filtered by search.
Also, you don't need to do "SELECT * FROM.." in order to get the count of all records, doing "SELECT COUNT(id) as count FROM.." is much more efficient
If you are storing the search param in the $_SESSION, make sure you have session_start() on top of your PHP scripts
I'm trying to delete a row from a MySQL table using PHP. I have visited many pages already but I can't find a reason why I can't make it work.
Contacts.php
require_once "functions.php";
$db = new DatabaseConnection();
$user = new AddressBook($db->pdo);
<table>
<tr>
<th>Nombre</th>
<th>Telefono</th>
<th>Direccion</th>
<th>Email</th>
</tr>
<?php
$i=0;
$contactos=$user->verContactos();
foreach($contactos as $conts){
$i++;
?>
<tr>
<td><?php echo $conts['nombre']; ?></td>
<td><?php echo $conts['telefono']; ?></td>
<td><?php echo $conts['direccion']; ?></td>
<td><?php echo $conts['email']; ?></td>
<td>Borrar</td></tr>
</tr>
<?php } ?>
</table>
delete.php
<?php
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
$result = mysql_query("DELETE FROM contacts WHERE id=$id")
or die(mysql_error());
header("Location: contacts.php");
}
else
{
header("Location: contacts.php");
}
?>
I know you're gonna say mysql is not useful anymore. In my web system I have this part using a function and PDO extension.
It doesn't matter if I use this example or my version using PDO, I cannot get the id. I just get something like this:
http://localhost/agenda/borrar.php?id <- Missing ID
The id field in database is called 'id'. I think the problem may be the use of $_GET but I can't find it.
This:
<td>Borrar</td></tr>
Needs to be this:
<td>Borrar</td></tr>
And this:
$result = mysql_query("DELETE FROM contacts WHERE id=$id")
Needs to be this:
$result = mysql_query("DELETE FROM contacts WHERE id=" . intval($id))
So I have the following SQLQuery:
$user = new User();
$nome = escape($user->data()->username);
$ligacao = mysqli_connect("localhost","root","","publicidade") or die("Erro MySQL");
$sql = "SELECT * FROM ads, users WHERE users.username = '$nome' AND users.id = ads.cod_user";
$resultado = mysqli_query($ligacao, $sql);
Following then the current code to list:
while(($registo = mysqli_fetch_assoc($resultado)) != null)
{
?>
<tr>
<td><?php echo $registo["id"]?></td>
<td><?php echo $registo["content"]?></td>
<td><?php echo $registo["image"]?></td>
<td><?php echo $registo["total_views"]?></td>
<td><?php echo $registo["contract_views"]?></td>
<td><?php echo $registo["active"]?></td>
<td><?php echo $registo["cod_type"]?></td>
<td><?php echo $registo["cod_organization"]?></td>
<td>ALTERAR</td>
<td>REMOVER</td>
</tr>
<?php
There's one thing I want, which is listing the whole ads made by that user (it works) but with the correct ID's, which in my case would be something like "1" "3" "6" "9". The problem is that it's listing them but with the user ID "1" instead.
<td><?php echo $registo["id"]?></td>
Should I change something there?
you could either split up the * into the exact list while of columns refering the table name and give the ads.id a special name: SELECT ... , ads.id as AdID FROM ...
Or you could change the name of the id-column in one of the tables. That would force you to change it everywhere it's used aswell. Therefor I'd go with option one.
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');