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
Related
I tried the following code to show my SQL database data in a table in my dashboard
<tbody>
<?php
while ($row = $result->fetch_assoc()):?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><img src="<?php echo $row['image']; ?>" width=100px height=100px></td>;
<td><?php echo $row['class']; ?></td>
<td><?php echo $row['reg_no']; ?></td>
<td><?php echo $row['capacity']; ?></td>
<td><?php echo $row['oname']; ?></td>
<td><?php echo $row['province']; ?></td>
<td><?php echo $row['district']; ?></td>
<td><?php echo $row['contact']; ?></td>
<td><a href="#" class="btn btn-warning btn-circle editbtn">
<i class="fas fa-edit"></i></a></td>
<td><a href="#" class="btn btn-info btn-circle viewbtn">
<i class="fas fa-eye"></i></a></td>
<td><a href="#" class="btn btn-danger btn-circle deletebtn">
<i class="fas fa-trash"></i></a></td>
</tr>
<?php endwhile; ?>
</tbody>
But that image column image does not show has an image
Result show like this:
my result
How to solve that problem
To me it seems your are inserting to the src attribute the context of the file rather then the URL of the image.
you are saving the image content it self in the DB,
check this out :
https://www.codexworld.com/upload-store-image-file-in-database-using-php-mysql/
I'm creating a table using ajax and php but there's one problem, the table isn't showing in my div. I'm really new to ajax so I don't really fully understand it yet.
Here's my div:
<div class="body" id="live-data">
</div>
Here's the ajax code:
$(document).ready( function() {
function fetch_data() {
$.ajax({
url:"fetch.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
});
And here's fetch.php:
<?php
include('../global/db.php');
$output = '';
$sql ="SELECT * FROM students WHERE status = '0' AND stud_grade = '$level_id' ORDER BY date_enrolled DESC";
$result = mysqli_query($db, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th width="135" class="noExport">Action</th>
<th width="90">LRN</th>
<th width="20">Level</th>
<th>Name</th>
<th width="20">Gender</th>
<th width="60">Type</th>
<th width="105" style="font-size: 14px!important;">Date Enrolled</th>
</tr>
</thead>
<tbody>';
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
$output .= '
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
';
}
}
else {
$output .= '
<tr>
<td colspan="12">Data not Found</td>
</tr>';
}
$output .= '
</tbody>
</table>
</div>';
echo $output;
?>
It would great if anyone could help because I just don't know why it doesn't work
Edit:
I've changed the code so it returns the data in the console tab and here's what shows up:
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th width="135" class="noExport">Action</th>
<th width="90">LRN</th>
<th width="20">Level</th>
<th>Name</th>
<th width="20">Gender</th>
<th width="60">Type</th>
<th width="105" style="font-size: 14px!important;">Date Enrolled</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
<tr>
<td>
<button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?>">
<i class="material-icons">search</i>
<span>Profile</span>
</button>
<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#<?php echo $stud_id ?><?php echo $stud_id ?>">
<i class="material-icons">person</i>
<span>Parent</span>
</button>
</td>
<td><?php echo $stud_lrn ?></td>
<td><?php echo $stud_grade ?></td>
<td><?php echo $stud_lname ?>, <?php echo $stud_fname ?> <?php echo $stud_mname ?></td>
<td><?php echo $stud_gender ?></td>
<td><?php echo $stud_type ?></td>
<td style="font-size: 12px!important;"><?php echo $date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled));?></td>
</tr>
</tbody>
</table>
</div>
So it clearly returns the correct data but it just doesn't show up in the live-data div
Maybe take a look in your html where is your div. Your div id says "live-data" and in the ajax code you mentioned to fetch data for div id "#live_data" instead of "#live-data".
Maybe changing them for same name can solve your problem. I would use for thr div id and in the ajax same id names like "#liveData".
Example (same code, just edited to the right IDs, compare with your original):
Your HTML div
<div class="body" id="liveData">
</div>
Your ajax code
$(document).ready( function() {
function fetch_data() {
$.ajax({
url:"fetch.php",
method:"POST",
success:function(data){
$('#liveData').html(data);
}
});
}
fetch_data();
});
Just append the correct Id of the div tag in sucess function
So inside my table before tbody tag, I inserted a form inside tbody. However while submitting its form, the url is getting btnchange.php?qty=1 instead of btnchange.php?prod_id=cartno.
shopcart.php
<?php // start
$stmt = $DB_con->prepare("SELECT * FROM cart WHERE cartuser = '$userid' and cartpend='IN CART'");
$stmt->execute();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row); //end ?>
<form method="get" action="btnchange.php?prod_id=<?php echo $row['cartno'] ?>">
<tbody>
<tr>
<td><?php echo $row['cartno']; ?></td>
<td><?php echo $row['cartname']; ?></td>
<td><?php echo $row['cartprice']; ?></td>
<td><input type="text" name="qty" value="<?php echo $row['cartqty']; ?>"></td>
<td><?php echo $row['cartsub']; ?></td>
<td><span class="label label-primary"><?php echo $row['cartpend']; ?></span></td>
<td><button type="submit" name="submit">Submit Quantity</button></b>
<td><a class="btn btn-danger btn-xs" href="?delete_id=<?php echo $row['cartno']; ?>" title="click for delete" onclick="return confirm('Sure to Delete?')"><i class="fa fa-trash-o"></i> Remove</a></td>
</tr>
</tbody>
</form>
This is my btnchange.php code, this is what i used to get the prod_id code and the qty input from shopcart.php.
btnchange.php
//get prod id
if(isset($_GET['prod_id']) && !empty($_GET['prod_id']))
{
$prod_code = $_GET['prod_id'];
$newqty = $_GET['qty'];
You can't put query parameters in the action attribute. You need to use a hidden input instead:
<?php // start
$stmt = $DB_con->prepare("SELECT * FROM cart WHERE cartuser = '$userid' and cartpend='IN CART'");
$stmt->execute();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row); //end ?>
<form method="get" action="btnchange.php">
<input type="hidden" name="prod_id" value="<?php echo $row['cartno']; ?>">
<tbody>
<tr>
<td><?php echo $row['cartno']; ?></td>
<td><?php echo $row['cartname']; ?></td>
<td><?php echo $row['cartprice']; ?></td>
<td><input type="text" name="qty" value="<?php echo $row['cartqty']; ?>"></td>
<td><?php echo $row['cartsub']; ?></td>
<td><span class="label label-primary"><?php echo $row['cartpend']; ?></span></td>
<td><button type="submit" name="submit">Submit Quantity</button></b>
<td><a class="btn btn-danger btn-xs" href="?delete_id=<?php echo $row['cartno']; ?>" title="click for delete" onclick="return confirm('Sure to Delete?')"><i class="fa fa-trash-o"></i> Remove</a></td>
</tr>
</tbody>
</form>
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
I am trying to update the contents of the table by pressing the update button in PHP using the MySQL connection. I want to combine Ajax and PHP, because by using that I don't have to refresh the page every time.
I don't have any idea to do this. And I haven't found anything on the internet. So I came here to ask.
Here you see a picture of how my current page looks like.
My table
The code I use to generate/populate the table:
<div class="col-md-12">
<h3 class="title">Active Tasks</h3>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Task</th>
<th>Predecessor</th>
<th>Dev</th>
<th>MOSCOW</th>
<th>Plan</th>
<th>Do</th>
<th>Check</th>
<th>Act</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($conn, $SQL);
while ($row = mysqli_fetch_array($query)) {?>
<tr class="active">
<td style="display:none;><?php echo $row['ID'];?></td>
<td style="display:none;"><?php echo $row['ProjectID'];?></td>
<td><?php echo $row['Task'];?></td>
<td><?php echo $row['Predecessor'];?></td>
<td><?php echo $row['Dev'];?></td>
<td><?php echo $row['MOSCOW'];?></td>
<td class="js-Task-Plan"><?php echo $row['Plan'];?></td>
<td class="js-Task-Do"><?php echo $row['Do'];?></td>
<td><?php echo $row['Check'];?></td>
<td><?php echo $row['Act'];?></td>
<td>
<button type="button" class="js-TimerStart btn btn btn-default">Start</button>
<button type="button" class="js-TimerPauzeer btn btn-default">Pauzeer</button>
<button type="button" class="js-TimerResume btn btn-default">Resume</button>
<a class="btn btn-default btn-info" href="editTask.php?TaskID=<?php echo $row['ID'];?>&projectid=<?php echo $ID;?>">Aanpassen</a>
<a class="btn btn-default btn-danger" href="delete_task.php?TaskID=<?php echo $row['ID'];?>&projectid=<?php echo $ID;?>">Verwijderen</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<button class="btn btn-block btn-default btn-success" name="UpdateTable"><span style="font-weight: bold;">Update</span></button>
</div>
Thank you for every bit of response and information.
Its not too big issue.
You have to just give some class and ids to your table row and tds.
first of all give unique ids to all rows, in your case you have
<?php echo $row['ID'];? and give same class to all rows these will use later steps.
and give data-id to each row with unique id...so
every tr may be named with <tr id='tr_<?php echo $row['ID'];?>' class='projects' data-id='<?php echo $row['ID'];?>'> and Predecessor td witl be <td id='p_<?php echo $row['ID'];?>'></td>
now i am asssuming that you want to update each project Predecessor every 3 seconds..then just you need to write ajax call for updation.
setInterval(function(){
$('.project').each(function(){
var id=$(this).data('id');
$.ajax({
url:'php_file _name_that_takes_project_id_and_get_details_from_database',
data:{id:id},
type:'POST',
sucess:function(data){
// here data is returned value by ajax php file call which takes project id and fetch Predecessor related to this id.
$('#p_'+id).html(data);
}
});
});
, 3000);
Put it in separate script:
<?php
$query = mysqli_query($conn, $SQL);
while ($row = mysqli_fetch_array($query)) {?>
<tr class="active">
<td style="display:none;><?php echo $row['ID'];?></td>
<td style="display:none;"><?php echo $row['ProjectID'];?></td>
<td><?php echo $row['Task'];?></td>
<td><?php echo $row['Predecessor'];?></td>
<td><?php echo $row['Dev'];?></td>
<td><?php echo $row['MOSCOW'];?></td>
<td class="js-Task-Plan"><?php echo $row['Plan'];?></td>
<td class="js-Task-Do"><?php echo $row['Do'];?></td>
<td><?php echo $row['Check'];?></td>
<td><?php echo $row['Act'];?></td>
<td>
<button type="button" class="js-TimerStart btn btn btn-default">Start</button>
<button type="button" class="js-TimerPauzeer btn btn-default">Pauzeer</button>
<button type="button" class="js-TimerResume btn btn-default">Resume</button>
<a class="btn btn-default btn-info" href="editTask.php?TaskID=<?php echo $row['ID'];?>&projectid=<?php echo $ID;?>">Aanpassen</a>
<a class="btn btn-default btn-danger" href="delete_task.php?TaskID=<?php echo $row['ID'];?>&projectid=<?php echo $ID;?>">Verwijderen</a>
</td>
</tr>
<?php } ?>
and call it 'table.php'.
and add js code, which will be make AJAX call to table.php and update our table
$.ajax('table.php', {
success: function(data) {
$('.table-bordered.table-condensed tbidy').html($(data));
$('#notification-bar').text('The page has been successfully loaded');
},
error: function() {
$('#notification-bar').text('An error occurred');
}
});