Didn't get the ID from database based on row table - php

Why I can't get the ID from a row in table when I click Pending
Here is the code:
<!-- ############################################################################################################################################################################################################ -->
<!-- TABLE BUSINESS -->
<div class="row" id="TblBiz" style="display: none;">
<div class="col" style="height: 71px;">
<h1 style="font-family: Roboto, sans-serif; font-weight: 300;">Business</h1>
</div>
<div class="col-md-8 offset-md-2">
<div class="panel" id="main">
<div class="container-fluid container-bg">
<div class="table-responsive">
<table id="myTable4" class="table table-hover table-dark" style="font-size:11px; margin-top:6px;">
<thead style="font-size: 18px;">
<th>#</th>
<th>Business ID</th>
<th>Name</th>
<th>Owner</th>
<th>View</th>
<th>Approval</th>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM mechanic_business";
$result = mysqli_query($connect, $sql);
if(mysqli_num_rows($result) > 0){
$i = 1;
while ($row = mysqli_fetch_assoc($result)) {
$biz_ID = $row['businessID'];
$mech_ID = $row['mechID'];
$mech_name = $row['mech_name'];
$bizName = $row['business_name'];
$bizApprove = $row['business_approval_status'];
$bizStatus = $row['business_status'];
// $dateJoin = $row['DATE_REGISTERED'];
// $dateFormated = date("d M Y", strtotime($dateJoin));
// Color class in PHP color(Pending, Approved, Banned)
$color = ($bizApprove === 'Pending') ? '#f0ad4e' : (($bizApprove === 'Approved') ? '#28a745' : '#dc3545');
echo "<tr style='font-size:18px;'>";
echo "<td class='align-middle'>" . $i; $i++ . "</td>";
echo "<td class='align-middle'>" . $biz_ID . "</td>";
echo "<td class='align-middle'>". $bizName ."</td>";
echo "<td class='align-middle'>". $mech_name ."</td>";
echo "<td class='align-middle'><input type='button' name='view' value='view' id='". $biz_ID ."' class='btn btn-info btn-xs view_biz'></td>";
echo "<td class='align-middle'><a role='button' class='approval-bton' id='". $biz_ID ."' style='color:". $color ."';>". $bizApprove ."</a></td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td colspan='7' class='table-active align-middle'>No Record in Database</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- ############################################################################################################################################################################################################ -->
<!-- ############################################################################################################################################ -->
<!-- UPDATE APPROVE STATUS MODAL -->
<div class="modal fade" id="approveModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"> Update Business Approval Status </h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="" method="post" enctype="multipart/form-data">
<div class="modal-body">
<input type="text" name="businessID" id="" value="<?php echo $biz_ID; ?>">
<div class="form-group">
<div>
<small id="photoHelp" class="form-text text-muted"><span style="color: #FF0000;">*</span><em>320px x 320px is ideal dimension, Format: jpg, png, jpeg, and PDF only</em></small>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="updateApprove" class="btn btn-primary">Save Change</button>
</div>
</form>
</div>
</div>
</div>
<!-- ############################################################################################################################################ -->
When I clicked a Pending from each row it suppose to get their ID based on the database but when I clicked from each row they display the same ID as shown picture in the link below.
https://drive.google.com/file/d/1IcfDUs3Q8Wl5vVO3KF793M5noTukLrCL/view?usp=sharing
They both show ORVA01BIZ-00000002 but in database first row is ORVA01BIZ-00000001 and ORVA01BIZ-00000002 is the second row and goes on.

There is very simple logic.
first, your model is not in loop so you can't pass $biz_ID in the model if you want to get each id from the database.
When the loop ends then it will only print the last row biz_id in the model.
The solution is :-
Changes you have to do in your code
You have to do one thing if you want to get each id
This is your second td :-
echo "<td class='align-middle bizzid'>" . $biz_ID . "</td>";
In your model you have to paste this : -
<input type="text" name="businessID" id="businessID" value="">-
This jquery code can help you to get desired result.
var bizid = $(this).closest('tr').find('.bizzid').text();
$('#businessID').val(bizid);
$('#approveModal').modal('show');
});'

Okay, got it
I did what you asked #Gurpreet Kait it really workss.
I put the jquery code inside the initialize modal bootstrap
// Show Modal 1
$(document).ready(function () {
$('.approval-bton').on('click', function() {
var bizid = $(this).closest('tr').find('.bizzid').text();
$('#businessID').val(bizid);
$('#approveModal').modal('show');
});
Thank you very much #Gurpreet Kait for your help.
});

Related

Bootstrap-table - Modify fields with bootstrap modal

I continue with my new website and I'm trying the bootstrap framework and I would like modify a row, its fields, from a bootstrap modal window. At moment I show a modal windows with fields but I don't know to include the data from the row of the table :(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AddCloud - Producció</title>
<!-- INCLUDES -->
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/bootstrap-table/dist/bootstrap-table.css">
<link rel="stylesheet" href="assets/bootstrap-editable/css/bootstrap-editable.css">
<script src="assets/jquery/jquery.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/bootstrap-table/dist/bootstrap-table.js"></script>
<script src="assets/bootstrap-editable/js/bootstrap-editable.js"></script>
</head>
<body>
<!-- SESSION PHP OK -->
<?php
session_start();
if(isset($_SESSION['username']) and $_SESSION['username'] <> ''){
?>
<!-- NAVIGATION -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<a class="navbar-brand" href="http://www.addvantage.es"><font color=#8abe44>AddCloud</font></a></div>
<div class="collapse navbar-collapse" id="defaultNavbar1">
<ul class="nav navbar-nav">
<li class="active">Producció<span class="sr-only">(current)</span></li>
<li>Menu2</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown"><a href="#" class="dropdown-toggle"
data-toggle="dropdown" role="button" aria-expanded="false"><span class="glyphicon glyphicon-user"></span> <?php echo $_SESSION['username'] ?></a>
<ul class="dropdown-menu" role="menu">
<li><span class="glyphicon glyphicon-wrench"></span> preferències</li>
<li><span class="glyphicon glyphicon-lock"></span> canviar password</li>
<li class="divider"></li>
<li><span class="glyphicon glyphicon-log-out"></span> log out</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<!-- TABLE -->
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="text-center">Miquel Alimentació</h1>
</div>
</div>
<hr>
</div>
<div class="container">
<div class="row text-center">
<div class="col-md-6 col-md-offset-3"><h3>Edició 12</h3></div>
</div>
<hr>
<div class="row">
<?php
// Conexió a la base de dades
include("connect.php");
$conn = Conectarse("localhost", "5432", "dbname", "dbuser", "dbpass");
//query
$query = "SELECT * FROM produccion.ma_origen ORDER BY id_articulo ASC";
$result = pg_query($conn, $query);
//se despliega el resultado
echo "<table id='tableprod'
data-toggle='table'
data-toolbar='#toolbar'
data-show-refresh='true'
data-show-toggle='true'
data-sort-name='name'
data-sort-order='desc'
data-show-columns='true'
data-pagination='true'
data-search='true'
data-click-to-select='true'>";
echo "<thead class='thead-inverse'>";
echo "<tr>";
echo "<th data id='seleccion' data-switchable='false' data-checkbox='true'></th>";
echo "<th data id='pagina' data-sortable='true'>pagina</th>";
echo "<th data id='codigo' data-sortable='true' data-switchable='false'>codigo</th>";
echo "<th data id='descripcion' data-sortable='true' data-switchable='false'>descripcion</th>";
echo "<th data id='pvp-cat' data-sortable='true'>pvp-cat</th>";
echo "<th data id='pvp-lev' data-sortable='true'>pvp-lev</th>";
echo "<th data id='pvp-and' data-sortable='true'>pvp-and</th>";
echo "<th data id='pvp-cen' data-sortable='true'>pvp-cen</th>";
echo "<th data id='pvp-nor' data-sortable='true'>pvp-nor</th>";
echo "<th data id='pvp-vas' data-sortable='true'>pvp-vas</th>";
echo "<th data id='fecha-mod' data-sortable='true'>fecha-mod</th>";
echo "<th data id='user' data-sortable='true' data-visible='false'>user</th>";
echo "<th data id='edit' data-sortable='false' data-switchable='false'>edit</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while ($row = pg_fetch_row($result)){
echo "<tr>";
echo "<td></td>";
echo "<td>$row[2]></td>";
echo "<td>$row[3]</td>";
echo "<td>$row[4]</td>";
echo "<td>$row[5]</td>";
echo "<td>$row[6]</td>";
echo "<td>$row[7]></td>";
echo "<td>$row[8]</td>";
echo "<td>$row[9]</td>";
echo "<td>$row[10]</td>";
echo "<td>$row[11]</td>";
echo "<td>$row[12]</td>";
echo "<td><p data-placement='top' data-toggle='tooltip' title='Edit'><button class='btn btn-primary btn-xs' data-title='Edit' data-toggle='modal' data-target='#edit' ><span class='glyphicon glyphicon-pencil'></span></button></p></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
</div>
</div>
<hr>
<div class="row">
<div class="text-center col-md-6 col-md-offset-3">
<p>Copyright © 2016 · All Rights Reserved · <a href="http://www.addvantage.es/" >http://www.addvantage.es</a></p>
</div>
</div>
<hr>
</div>
<!-- MODAL MENU -->
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">Editar registre</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input class="form-control " type="text" placeholder="Pàgina">
</div>
<div class="form-group">
<input class="form-control " type="text" placeholder="Codi">
</div>
<div class="form-group">
<input class="form-control " type="text" placeholder="Descripció">
</div>
<div class="form-group">
<input class="form-control " type="text" placeholder="pvp-cat">
</div>
<div class="form-group">
<input class="form-control " type="text" placeholder="pvp-lev">
</div>
<div class="form-group">
<input class="form-control " type="text" placeholder="pvp-and">
</div>
<div class="form-group">
<input class="form-control " type="text" placeholder="pvp-cen">
</div>
<div class="form-group">
<input class="form-control " type="text" placeholder="pvp-nor">
</div>
<div class="form-group">
<input class="form-control " type="text" placeholder="pvp-vas">
</div>
</div>
<div class="modal-footer ">
<button type="button" class="btn btn-warning btn-lg" style="width: 100%;"><span class="glyphicon glyphicon-ok-sign"></span> Update</button>
</div>
</div>
</div>
</div>
<!-- SESSION PHP ERROR -->
<?php
} else{
?><p>La sesión no está activa, por favor ingrese aquí</p>
<?php
}?>
</body>
</html>
I use php to connect to database and add data into the table. My idea is from a edit button can modify this fields from a modal window. After that I would like to update database.
I'm not sure if this is the best way, I haven't a experience programming from web environment.
Please Could you help me to modify this data-fields from windows modal?
Thanks!
The way to try to do is correct. What you have to do is on clicking Edit link find the closest tr and find all the td that tr contain. Read td one by one the assign it to the text box.
Below is the code:
$('.edit').click(function(){
var $row = $(this).closest("tr"), $tds = $row.find("td"); //This will all the tds we need.
$.each($tds, function() {
$('#txtbox').val($(this).text()); //textbox in the modal window
});
});
The above code will fill all the values from the row to modal window after that you can modify and submit the form.
First step - identify row somehow. I'm working under assumption that $row[0] is the id of that row in the database. If it isn't change the code accordingly.
index.php
while ($row = pg_fetch_row($result)){
echo "<tr id="{$row[0]}">"; // assign id that corresponds to database id to each row
echo "<td></td>";
echo "<td class="pagina">$row[2]></td>";
echo "<td class="codigo">$row[3]</td>";
echo "<td class="descripcion">$row[4]</td>";
echo "<td class="pvp-cat">$row[5]</td>";
echo "<td class="pvp-lev">$row[6]</td>";
echo "<td class="pvp-and">$row[7]></td>";
echo "<td class="pvp-cen">$row[8]</td>";
echo "<td class="pvp-nor">$row[9]</td>";
echo "<td class="pvp-vas">$row[10]</td>";
echo "<td class="fecha-mod">$row[11]</td>";
echo "<td class="user">$row[12]</td>";
echo "<td>
<p data-placement='top' data-toggle='tooltip' title='Edit'>
<button class='edit' class='btn btn-primary btn-xs' data-title='Edit' data-toggle='modal' data-target='#edit'>
<span class='glyphicon glyphicon-pencil'></span>
</button>
</p>
</td>";
echo "</tr>";
}
Second, set jquery click event to copy values from rows and fill the modal input fields with correct values:
JQUERY
$('.edit').click(function(){
var row = $(this).closest("tr"), // edit button is in the same row as data you want to change
tds = row.find("td"); // get all table cells in that row
$.each(tds, function(index) {
if( $.inArray(index, [0, 10, 11, 12]) ) { // indexes of cells not to be used when getting data from table cells to edit
return; // skip to next loop iteration if one of these indexes
}
var input_name = $(this).attr("class"); // get name of cell we are evaluating (pagina, codigo, descripcion etc)
$([name='"' + input_name + '"']).val($(this).text()); //input name in the modal window
});
});
I've changed the modal to reflect changes in code above.
MODAL
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">Editar registre</h4>
</div>
<form action="/store.php">
<div class="modal-body">
<input type="hidden" name="id" type="text"> <!-- hidden input for id -->
<div class="form-group">
<input class="form-control" name="pagina" type="text" placeholder="Pàgina">
</div>
<div class="form-group">
<input class="form-control" name="codigo" type="text" placeholder="Codi">
</div>
<div class="form-group">
<input class="form-control" name="descripcion" type="text" placeholder="Descripció">
</div>
<div class="form-group">
<input class="form-control" name="pvp-cat" type="text" placeholder="pvp-cat">
</div>
<div class="form-group">
<input class="form-control" name="pvp-lev" type="text" placeholder="pvp-lev">
</div>
<div class="form-group">
<input class="form-control" name="pvp-and" type="text" placeholder="pvp-and">
</div>
<div class="form-group">
<input class="form-control" name="pvp-cen" type="text" placeholder="pvp-cen">
</div>
<div class="form-group">
<input class="form-control" name="pvp-nor" type="text" placeholder="pvp-nor">
</div>
<div class="form-group">
<input class="form-control" name="pvp-vas" type="text" placeholder="pvp-vas">
</div>
</div>
<div class="modal-footer ">
<input type="submit" class="btn btn-warning btn-lg" style="width: 100%;" value="Update">
</div>
</form>
</div>
</div>
</div>
File for storing edited values to the database:
store.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = $_POST['id'];
$pagina = $_POST['pagina'];
$codigo = $_POST['codigo'];
$descripcion = $_POST['descripcion'];
$pvp-cat = $_POST['pvp-cat'];
$pvp-lev = $_POST['pvp-lev'];
$pvp-and = $_POST['pvp-and'];
$pvp-cen = $_POST['pvp-cen'];
$pvp-nor = $_POST['pvp-nor'];
$pvp-vas = $_POST['pvp-vas'];
$sql = "UPDATE produccion.ma_origen SET pagina='{$pagina}',codigo='{$codigo}',descripcion='{$descripcion}',
pvp-cat='{$pvp-cat}', pvp-lev='{$pvp-lev}',pvp-and='{$pvp-and}',
pvp-cen='{$pvp-cen}',pvp-nor='{$pvp-nor}',pvp-vas='{$pvp-vas}'
WHERE id={$id}";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Since this was a lot of code to change and I don't have the time and all the data to test this properly you'll have to do it, but this is one of the ways you could solve your problem. Hope it helps.

Populating Codeigniter Bootstrap Modal from DB via Ajax?

I am trying to populate a list of players into a table within a bootstrap modal. Currently I have button that sends the event_id to the controller; the controller then sends a query to the model and I pass the data to a view. However I would like the modal to load within the original view. I am guessing I need some ajax to get this to work however I am having some trouble getting it to work. Hoping someone can point me in the right direction or have another route for me to go.
Here is the code I currently am using.
Controller:
function event_users($event_id)
{
$this->load->model('email_model');
$data['darkteam'] = $this->event_model->dark_team_list($event_id);
$data['lightteam'] = $this->event_model->light_team_list($event_id);
$data['darkgoalie'] = $this->event_model->dark_goalie_list($event_id);
$data['lightgoalie'] = $this->event_model->light_goalie_list($event_id);
$data['event_data'] = $this->email_model->get_location_date($event_id);
$this->load->view('templates/header');
$this->load->view('event/player_list', $data);
}
View:
<td><a href="<?php echo base_url(); ?>event/event_users/<?php echo $e->id; ?>Player List</td>
Modal View (player_list.php):
<script type="text/javascript">
$(window).load(function(){
$('#user_event_modal').modal('show');
});
</script>
<?php foreach ($event_data as $ed): ?>
<?php $location = $ed->location; ?>
<?php $date = $ed->date; ?>
<?php endforeach; ?>
<!--User Event List-->
<div class="modal fade" id="user_event_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" onclick="location.href='<?php echo base_url();?>event'"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading"><strong><?php echo $location . " - " . date("l, F jS # g:ia",strtotime($date)); ?></h4></strong>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">
<table class="table table-striped table-condensed table-bordered table-list events-list-table">
<thead>
<tr>
<strong>Dark Team</strong>
</tr>
</thead>
<tbody>
<?php if (!empty($darkteam)): ?>
<?php foreach ($darkteam as $row): ?>
<tr>
<td><div style="float: left; text-align: left;"><?php echo ($row->first_name) . " " . ($row->last_name); ?></div><div style="float: right; text-align: right;">
<?php if ($this->ion_auth->is_admin()) : ?>
<button onclick="location.href='<?php echo base_url();?>event/switch_team/<?php echo ($row->user_id . "/" . $row->event_id); ?>' " class="btn btn-primary right-block btn-xs" data-dismiss="modal"><i class="fa fa-arrow-right"></i></button>
<?php elseif ($row->user_id == $this->session->userdata('user_id')) :?>
<button onclick="location.href='<?php echo base_url();?>event/switch_team/<?php echo ($row->user_id . "/" . $row->event_id); ?>' " class="btn btn-primary right-block btn-xs" data-dismiss="modal"><i class="fa fa-arrow-right"></i></button>
<?php else: ?>
<?php endif; ?>
</div></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td class="bg-info" colspan="6">No users currently registered.</td>
</tr>
<?php endif; ?>
Yes, use ajax to empty and then append content to the modal:
<!--replace <a></a> tag with element so jquery can target-->
<td id="update_event">Player List</td>
<!--add an id to the table so ajax can update on success-->
<table id="updateable"class="table table-striped table-condensed table-bordered table-list events-list-table">
Now for the ajax:
//we can use a click event listener
$('#update_event").on("click", "td", function(){
var id = '<?php echo $e->id;?>';
$.ajax({
url: '/event/event_users/'+ id +'',
success: function(results){
$(#updateable).empty(); //first empty table
}) $(#updateable).html(//append data to table)
})

Update & delete MySQL from modal bootstrap

I´m trying to delete and/or update data from form inside a modal window .For create it I haven´t problem but with the others actions (delete and update) I always have a problem,I think the key is the "id" it seems take the last value and not the value that I want to delete or update.
I know my code could be more nice.
<div class="container">
<div class="row espacio">
<p class="col-md-1"><a data-toggle="modal" href="#myModal" class="btn btn-default glyphicon glyphicon-plus"><span></span></a></p>
<p class="col-md-1 col-md-offset-10"><span class="btn btn-default glyphicon glyphicon-envelope"></span></p>
<table class="table table-bordered">
<caption>Bitácora</caption>
<thead>
<tr>
<th>Tecnico</th>
<th>Turno</th>
<th>Información</th>
<th>Estado</th>
<th>Creada</th>
<th>Modificada</th>
</tr>
</thead>
<tbody>
<?php
$txt ="";
$nombres =array();
$turnos = array('Mañana','Tarde','Noche');
$estados =array('Pendiente','Cerrado');
$db = new Db;
$sql = "SELECT * FROM tecnicos";
foreach ($db->consulta($sql) as $row) {
array_push($nombres,$row['Nombre']);
}
$sql = 'SELECT * FROM bitacora ORDER BY Estado DESC';
$consulta = $db->consulta($sql);
while ($row = mysqli_fetch_array($consulta)) {
$tecnico = $turno = $estado = "";
if ($row['Estado'] == "Pendiente"){
$txt .= "<tr class= 'bg-warning'>";
}else{
$txt .= "<tr class= 'bg-success'>";
}
$txt .= "
<td>". $row['Name'] . "</td>
<td>". $row['Turno'] . "</td>
<td>". $row['Info'] . "</td>
<td>". $row['Estado'] . "</td>
<td>". $row['RecordDate'] . "</td>
<td>". $row['ModiDate'] . "</td>
<td class='text-center'><a data-toggle='modal' href='#myModalUpdate".$row['Id']."''><span class='glyphicon glyphicon-pencil'></span></a>
<a data-toggle='modal' href='#myModalDelete".$row['Id']."''><span class='glyphicon glyphicon-remove'></span></a> </td>
</tr>";
//MODAL UPDATE
for ($i=0; $i <sizeof($nombres) ; $i++) {
if ($nombres[$i] == $row['Name'] ) {
$tecnico .= '<option value="'.$nombres[$i].'" selected="selected">'.$nombres[$i].'</option>';
}
else{
$tecnico .= '<option value="'.$nombres[$i].'" >'.$nombres[$i].'</option>';
}
}
for ($i=0; $i <sizeof($turnos) ; $i++) {
if ($turnos[$i] == $row['Turno']) {
$turno .= '<option value="'.$turnos[$i].'" selected="selected">'.$turnos[$i].'</option>';
}
else{
$turno .= '<option value="'.$turnos[$i].'" >'.$turnos[$i].'</option>';
}
}
for ($i=0; $i <sizeof($estados) ; $i++) {
if ($estados[$i] == $row['Estado']) {
$estado .= '<option value="'.$estados[$i].'" selected="selected">'.$estados[$i].'</option>';
}
else{
$estado .= '<option value="'.$estados[$i].'" >'.$estados[$i].'</option>';
}
}
$txt .= " <div class='modal fade' id='myModalUpdate".$row['Id']."' role='dialog'>
<div class='modal-dialog'>
<!-- Modal content-->
<div class='modal-content'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h4 class='modal-title'>Modifica registro en Bitácora</h4>
</div>
<div class='modal-body'>
<form role='form' action='' method='post'>
<div class='form-group'>
<input type='text' name='id' value='".$row['Id']."'>
</div>
<div class='form-group'>
<label>Técnico</label>
<select class='form-control' name='modnombre'>
".$tecnico."
</select>
</div>
<div class='form-group'>
<label>Turno</label>
<select class='form-control' name='modturno'>
".$turno."
</select>
</div>
<div class='form-group'>
<label>Información</label>
<textarea class='form-control' name='modinformacion'>".$row['Info']."</textarea>
</div>
<div class='form-group'>
<label>Estado</label>
<select class='form-control' name='modestado'>
".$estado."
</select>
</div>
<div class='form-group text-center'>
<button type='submit' class='btn btn-warning' name='actualizar'>Actualiza</button>
</div>
</div><!--/modal body-->
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div><!--/modal content-->
</div><!--/modal dialog-->
</div><!--/modal-->";
//MODAL DELETE
$txt .= " <div class='modal fade' id='myModalDelete".$row['Id']."' role='dialog' >
<div class='modal-dialog'>
<!-- Modal content-->
<div class='modal-content'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h4 class='modal-title'>Elemina registro en Bitácora</h4>
</div>
<div class='modal-body'>
<form role='form' action='' method='post'>
<div class='form-group'>
<input type='text' name='id' value='".$row['Id']."'>
</div>
<div class='form-group text-center'>
<button type='submit' class='btn btn-danger' name='eliminar'>Elimina</button>
</div>
</div><!--/modal body-->
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div><!--/modal content-->
</div><!--/modal dialog-->
</div><!--/modal-->";
}//end for principal
echo $txt;
?>
</tbody>
</table>
</div>
<!--MODAL CREATE-->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Añade registro en Bitácora</h4>
</div>
<div class="modal-body">
<form role="form" action="" method="post">
<div class="form-group">
<label>Técnico</label>
<select class="form-control" name="nombre">
<?php
$txt ="";
$db = new Db;
$sql = "SELECT * FROM tecnicos";
foreach ($db->consulta($sql) as $row) {
$txt.= "<option value='".$row['Nombre']."'>".$row['Nombre']."</option>";
}
echo $txt;
?>
</select>
</div>
<div class="form-group">
<label>Turno</label>
<select class="form-control" name="turno">
<option value="Mañana">Mañana</option>
<option value="Tarde">Tarde</option>
<option value="Noche">Noche</option>
</select>
</div>
<div class="form-group">
<label>Información</label>
<textarea class="form-control" name="informacion"></textarea>
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-success" name="crear">Crear</button>
</div>
</form>
</div><!--/modal body-->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!--/modal content-->
</div><!--/modal dialog-->
</div><!--/modal-->
</div> <!-- /container -->
And these are the action´s form
enter code //formulario CREATE
if(isset($_POST["crear"])) {
$nombre = $_POST["nombre"];
$turno = $_POST["turno"];
$informacion = $_POST["informacion"];
$sql = "INSERT INTO bitacora (Name,Turno,Info,Estado,RecordDate,ModiDate) VALUES ('" . $nombre . "', '" . $turno . "', '" . $informacion . "','Pendiente',now(),now())";
$db->consulta($sql);
}
//formulario UPDATE
if(isset($_POST["actualizar"])) {
$sql = "UPDATE bitacora SET Name = '" . $_POST["modnombre"] . "', Turno = '" . $_POST["modturno"] . "', Info = '" .$_POST["modinformacion"]. "', Estado = '" . $_POST["modestado"] . "' , ModiDate =now() WHERE Id = '".$_POST["id"]."' ";
$db->consulta($sql);
}
//form DELETE
if(isset($_POST["eliminar"])) {
$id = $_POST["id"];
$sql = "DELETE FROM bitacora WHERE Id = $id";
$db->consulta($sql);
}here
It seems that your input named "id" is not taking the correct value of $row['Id']. Try to inspect it and see if his "value" is not empty.
If the value was empty, your problem is the value of $row['Id'].
Try to replace $row['Id'] by $row['id'] and see the results.

mysql query retuning only 1 result

I have a MySQL query running on page that for some reason I sonly returning 1 row when run on my PHP application.
But when running the exact same query within PHP my admin it returns the correct number of rows.
This is the code I am using:
<?php
include ('includes/headFunctions.php');
include('includes/head.php');
?>
<div class="row container">
<?php
if(isset($_POST['weekly_search'])) {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$reporting = DB::getInstance()->query("SELECT * FROM transactions WHERE transaction_date BETWEEN '2015-08-01' AND '2015-08-17'");
if(!$reporting->count()) {
echo 'No results';
} else {
$_SESSION['weekly_records'] = 'true';
//if I run the foreach here it works correctly
}
}
$week_start_date = date("Y-m-d", strtotime('last Sunday'));
$week_end_date = date("Y-m-d");
//echo $start_date, '<br>';
//echo $end_date;
//die();
$results = DB::getInstance()->query("SELECT * FROM transactions WHERE transaction_date BETWEEN '$week_start_date' AND '$week_end_date'");
if ($results) {
$add = 0;
$minus = 0;
$total_transAdded = 0;
$total_transRedeemed = 0;
foreach ($results->results() as $result) {
$add = $total_transAdded += $result->transaction_add;
$minus = $total_transRedeemed += $result->transaction_redeem;
};
//print Elements
echo '<div class="row"><h1>Week Ending:</br> ', $week_end_date, '</h1></div>';
echo '<div class="row">';
echo '<div class="col-xs-6"><h2>Rewarded</h2>', '<h3>£', round ($add, 2), '</h3></div>';
echo '<div class="col-xs-6"><h2>Redeemed</h2></div>', '<h3>£', round ($minus, 2), '</h3></div>';
echo '</div>';
} else {
echo '<div class="error">No records found</div>';
} //end if search->data
?>
<div class="row">
<div class="col-lg-12">
<a class="btn btn-primary btn-lg" href="#" data-toggle="modal" data-target="#date_picker_modal">Search By Date</a>
</div>
</div>
</div> <!-- End row container -->
<!-- Report Generator Modal -->
<div class="modal fade" id="date_picker_modal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title" id="myModalLabel">Add Transaction</h4>
</div>
<div class="modal-body">
<form action="" method="post">
<div class="input-group">
<span class="input-group-addon">Start Date</span>
<input type="date" min='0001-01-01' max='9999-12-31' name="start_date" class="form-control" step="any" min="0" required>
</div>
<hr>
<div class="input-group">
<span class="input-group-addon">End Date</span>
<input type="date" min='0001-01-01' max='9999-12-31' name="end_date" class="form-control" step="any" min="0" required>
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<button type="submit" name="weekly_search" class="btn btn-primary">Search</button>
</form>
</div>
</div>
</div>
</div>
<?php if(isset($_SESSION['weekly_records'])) {?>
<!-- Report Generator Modal -->
<div class="modal fade" id="weekly_records" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title" id="myModalLabel">Reports</h4>
</div>
<div class="modal-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Customer</th><th>Added</th><th>Redeemed</th><th>Date</th>
</tr>
</thead>
<tbody>
<?php
//running the foreacheach only retunrs the last result
foreach($reporting->results() as $report) {
echo '<tr>';
echo '<td>View</td>';
echo '<td>£', round($report->transaction_add, 2), '</td>';
echo '<td>£', round($report->transaction_redeem, 2), '</td>';
echo '<td>', $report->transaction_date, '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
<div class="modal-footer">
<button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
<?php include 'includes/footer.php'; ?>
Try this:
foreach($reporting->results() as $report) {
echo '<tr>';
echo '<td>View</td>';
echo '<td>£', round($report->transaction_add, 2), '</td>';
echo '<td>£', round($report->transaction_redeem, 2), '</td>';
echo '<td>', $report->transaction_date, '</td>';
echo '</tr>';
}

Send table data to popup delete

I am trying to delete a field in my html table. When I click on the delete button I redirect the user to a popup window asking if they are sure if they want to delete that line in the table. Once they click yes it takes them to a delete.php file which deletes it from our database. I am not sure how I can send this value to our delete.php file.
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>User Name</th>
<th>Distinct Items</th>
<th>Total Occurrences</th>
<th>Last Occurrence</th>
<th>Environment</th>
<th>Control</th>
</tr>
</thead>
<tbody>
<?php
include ("config.php");
$sql = "SELECT * FROM newTable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['account_id'] . '</td>';
echo '<td>'. $row['user'] . '</td>';
echo '<td>'. $row['pass'] . '</td>';
echo '<td>'. $row['privs'] . '</td>';
echo '<td>'. $row['privValue'] . '</td>';
echo '<td>';
echo '<i class="fa fa-pencil"></i>';
echo '<i class="fa fa-trash-o "></i>';
echo '</td>';
echo '</tr>';
}
?>
<!-- Modal_delete HTML -->
<div id="Modal_delete" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Delete User</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this user?</p>
</div>
<form action="delete_user.php" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>"/>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</form>
</div>
</div>
</div>
The problem is to create the delete button I am referencing an html subroutine to create that the popup once that is clicked. How can I send the the table data to my delete.php file?
<?php
include ("test.php");
$tbl_name="newTable"; // Table name
$id = $_POST['id'];
echo $_POST['id'];
echo "$id";
// To protect MySQL injection (more detail about MySQL injection)
$sql="DELETE FROM tbl_name WHERE account_id='$id';";
mysql_query(sql);
header("Location: adminUser.php");
?>
Are you opposed to using jquery? (And sorry, I would ask that in a comment beforehand, but, tsk, not enough reputation, so here's my whole answer :) The following would be very simple:
$('.btn-danger').click(function(event) {
$('#Modal_delete').css('display', 'initial'); // swap this for however you're showing your delete confirmation popup. I just had it for testing.
var rowToDeleteID = $(event.currentTarget).parents('tr').attr('id'); // you'd need to add a unique id to each table row for this to work.
$('input[type="hidden"]').val(rowToDeleteID);
});
Of course, this would be much more robust (in fact, depending on your other code, it might only work [not to mention it's just a good idea]) if you add unique identifiers to the delete buttons (the a tags with fa-trash-o icons) and your hidden input field.
You can use the $row['account_id'] as an attribute in the link #Modal_delete and get it with jQuery..
<i class="fa fa-trash-o "></i>
$("a[href='#Modal_delete']").on("click", function(e) {
$("input[name='id']").val($(this).data("account-id"));
});

Categories