SESSION variable on td button not change - php

I have a table where filled by php from database. For each row, there is a button to enter to detail of an user. As I need a reference to user, i order to open his detail table, I use a _SESSION variable to do that:
<td><?php echo $user_id; ?></td>
<td ><?php echo $user_name; ?></td>
<td><?php echo $user_email; ?></td>
<td><?php echo $user_level; ?></td>
<td>
<?php $_SESSION['detailtable'] = $user_name; ?>
<button type="button" class="btn btn-default btnVerDatos" aria-label="Left Align" >
</button>
</td>
If I log the variable $_SESSION['detailtable'], is different for each row.
But when I use it in another php file, as for example:
$query = "SELECT * FROM ".$_SESSION['detailtable']." ORDER BY DataOra DESC";
the variable is the the same.
Where is y fault?
Tx in advance

Related

how to know the max number in each row of mysql database and return the column name or return name in phpmysql

How to know the max number in each row of mysql database and return the column name or return name in phpmysql
I want the dominant row to display the max value of a row , the row has this column (o_result, c_result,e_result,_result )
My code is
<tbody>
<?php
$result= mysqli_query($connection,"select * from test_report order by test_id ASC" ) or die (mysqli_error($connection));
while ($row= mysqli_fetch_array ($result) ){
$id=$row['test_id'];
?>
<tr>
<td><?php echo $row['test_id']; ?></td>
<td><?php echo $row['date_of_submission']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['o_result']; ?></td>
<td><?php echo $row['c_result']; ?></td>
<td><?php echo $row['e_result']; ?></td>
<td><?php echo $row['a_result']; ?></td>
<td><?php echo $row['n_result']; ?></td>
<td><?php echo $row['dominant']; ?></td>
<td><button class="btn search"></button>
<button class="btn edit"></button>
<button class="btn btn-danger"></button>
</td>
</tr>
<?php } ?>
</tbody>
You want to identify the greatest value on each row, and the name of the column that holds that value.
A simple method uses greatest() and case:
select
t.*,
greatest(o_result, c_result, e_result, a_result, n_result) as max_result,
case greatest(o_result, c_result, e_result, a_result, n_result)
when o_result then 'o_result'
when c_result then 'c_result'
when a_result then 'a_result'
when n_result then 'n_result'
end as max_column_name
from mytable t

Get different data from a modal with database id

I have brought in data from the database using mysqli_fetch_array();
I put the data from the database and insert it into a table
so the tbody looks like this:
<tbody>
<?php
$query = mysqli_query($db, "SELECT * FROM project");
$index = 1;
while($data = mysqli_fetch_array($query)) {?>
<tr>
<td><?php echo $index; ?></td>
<td><?php echo $data['project_name']; ?></td>
<td><?php echo $data['client_name']; ?></td>
<td><a href="#"</a>View</td>
<td style="text-align:center">
<a data-id="$data['id']" href="dashboard.php?remove_project=<?php echo $data['id']; ?
>"><i style="color: red" class="fa fa-times"></i></a>
</td>
</tr>
<?php $index++; }; ?>
</tbody>
I want a situation where when I click view, a modal pops up showing me data exactly from that Id, the goal is to click on the view and see a modal or a registered user. So I want to click on View and then the modal shows me data from that click, and when I click another View on the table, it shows me different data.

Id from PHP not being passed to URL

I have some data displayed from SQL to PHP, when the user clicks the eye icon, he is redirected to another page with id on the URL, I did the following code
<tr>
<td><?php echo $record['id']; ?></td>
<td><?php echo $record['firstname'];?></td>
<td><?php echo $record['Email'];?></td>
<td><?php echo $record['mobilenumber']?></td>
<td><?php echo $record['company']?></td>
<td><?php echo $record['designation']?></td>
<td><?php echo $record['state']?></td>
<td><i class="fa fa-eye" aria-hidden="true"></i></td>
</tr>
<?php } ?>
Instead of the id, some special characters are coming in the URL, how can I fix it?
Below is how your anchor tag should be.
<i class="fa fa-eye" aria-hidden="true"></i>
You need to enclose the $record variable inside php tags and echo it.
You should echo it, like you did with another values:
<?php echo $record['id']; ?>
Full code:
<tr>
<td><?php echo $record['id']; ?></td>
<td><?php echo $record['firstname'];?></td>
<td><?php echo $record['Email'];?></td>
<td><?php echo $record['mobilenumber']?></td>
<td><?php echo $record['company']?></td>
<td><?php echo $record['designation']?></td>
<td><?php echo $record['state']?></td>
<td><i class="fa fa-eye" aria-hidden="true"></i></td>
</tr>
<?php } ?>
You should echo ID
as below
<a href="detail.php?id=<?= $record['id'] ?>\">

PHP MYSQL Table delete button not working

Here is the query statement that I have. For some reason it works for another table I have but doesn't work with this table. I checked phpmyadmin to see if I would create an error but there were zero errors.
$Store_ID = filter_input(INPUT_POST,'Store_ID',FILTER_VALIDATE_INT);
//DELETE the Store
$query="DELETE FROM store_location WHERE Store_ID = :Store_ID";
$statement2=$db->prepare($query);
$statement2->bindValue(':Store_ID',$Store_ID);
$statement2->execute();
$statement2->closeCursor();
Here is the table code for the Delete Button
<?php foreach ($stores as $s) : ?>
<tr>
<td><?php echo $s['Store_ID']; ?></td>
<td><?php echo $s['Loc_Street']; ?></td>
<td><?php echo $s['Loc_City']; ?></td>
<td><?php echo $s['Loc_State']; ?></td>
<td><?php echo $s['Rent']; ?></td>
<td><?php echo $s['License']; ?></td>
<td><?php echo $s['Inspect']; ?></td>
<td><form action="stores.php" method="post">
<input type ="hidden" name ="Store_ID" value ="<?php echo $s['Store_ID']; ?>"/>
<input type="button" name="delete" value="Delete"/>
</form></td>
</tr>
<?php endforeach; ?>
</table>

Select a table row from an action button in PHP

i presently have a table which is auto populated from my database, i have three action buttons connected to each row which are edit, delete, and view. Am presently using session to send my key value to the next page, so that i can use that key value to load that particular row's data. the problem am having is that my session is picking the unique id of the last row instead of the row that i selected. here is my code
<?php
$sql = "SELECT * FROM houses";
$q=$conn->query($sql);
while ($row = mysqli_fetch_array($q)) {
?>
<tr>
<td><?php echo $row['nickname']; ?></td>
<td><?php echo $row['state']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['house_type']; ?></td>
<td><?php echo $row['owner']; ?></td>
<td><?php echo $row['price']; ?></td>
<td>
<div class="btn-group">
<?php
$_SESSION['house'] = $row['nickname'];
echo $_SESSION['house'];
?>
<a class="btn btn-primary" href="edit_property.php"><i class="icon_plus_alt2"></i></a>
<a class="btn btn-success" href="view_property.php"><i class="icon_check_alt2"></i></a>
<a class="btn btn-danger" href="delete_property.php"><i class="icon_close_alt2"></i></a>
</div>
</td>
</tr>
<?php
}
?>
That's because you don't want to use sessions in this case, the browser must give you back the ID that you want to view or edit.
Change it like this:
<div class="btn-group">
<a class="btn btn-primary" href="edit_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_plus_alt2"></i></a>
<a class="btn btn-success" href="view_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_check_alt2"></i></a>
<a class="btn btn-danger" href="delete_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_close_alt2"></i></a>
</div>
</td>
Then you will find in $_GET['house'] the key to use, not in $_SESSION

Categories