when i view the records from the database. the first record doesn't show on the page. when I add the second record and on, they are displayed on the page except the first record that I entered. i don't know why is that. i attached code i tried so so.
$last_insert_id = $_GET['last_id']; this id is coming from purchase page. get the id and put select statement and view relevent record match the id.
$sql = "select i.pur_id,i.prod_id,i.buyprice,i.qty,i.total,p.date,p.total,p.pay,p.due from purchase_item i, purs p where i.pur_id = p.id and i.pur_id = '278' ";
there are 7 row data include in 278 id. but display only 6 rows
<?php
$x = 1;
$last_insert_id = $_GET['last_id'];
$sql = "select i.pur_id,i.prod_id,i.buyprice,i.qty,i.total,p.date,p.total,p.pay,p.due from purchase_item i, purs p where i.pur_id = p.id and p.id = $last_insert_id ";
$orderResult = $conn->query($sql);
$orderData = $orderResult->fetch_array();
while ($row = $orderResult->fetch_array()) {
?>
<tr>
<td class="text-center">
<?php echo $x; ?>
</td>
<td class="text-center">
<?php echo $row[6]; ?>
</td>
<td class="text-center">
<?php echo $row[3]; ?>
</td>
<td class="text-center"><?php echo $row[2]; ?></td>
<td class="text-right"><?php echo $row[4]; ?></td>
</tr>
<?php
$x++;
}
?>
Related
I'm making something for a stocktake.
I have a form that generates with 4 fields. item_code, item_name, packing, quantity
<form action="goods.php" method="post">
<table>
<!-- Headers -->
<tr>
<td><b>Item Code</b></td>
<td><b>Description</b></td>
<td><b>Packing</b></td>
<td><b>Quantity</b></td>
</tr>
<?php
for ($i = 0; $i <= 50; $i++) {
?>
<tr>
<td>
<INPUT TYPE="TEXT" NAME="item_code[<?php echo $i; ?>]" SIZE="6" VALUE="
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $_POST["item_code"][($i)];
}
?>"></td>
<td><?php
if (!empty($_POST["item_code"][($i)])) {
$result = FetchData($_POST["item_code"][($i)]);
echo $result['category'];
}
?>
</td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['item_name'];
}
?></td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['packing'];
}
?></td>
<td><INPUT TYPE="TEXT" NAME="quantity[<?php echo $i; ?>]" SIZE="5" VALUE="
<?php
if (!empty($_POST["quantity"][($i)])) {
echo $_POST["quantity"][($i)];
} else {
echo "";
}
?>"></td>
A js function for the button
<script>
function fillForm(value) {
document.getElementById('value').innerHTML = value;
}
</script>
and a list that is generated with 3 fields. item_code, item_name, packing
<?php
$dbh = dbh_get();
$sql = 'SELECT * FROM goods as goods(item_code, sort) order by human_sort(goods.item_code)';
$v = array();
$stmt = $dbh->prepare($sql);
$stmt->execute();
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) break;
print '
<tr>
<td class="buttonL" id="<php ' . $r['item_code'] . ' ?>" onclick="fillForm()">' . $r['item_code'] . '</td>
<td>' . $r['item_name'] . '</td>
<td>' . $r['packing'] . '</td>
</tr>' . "\n";
}
dbh_free($dbh);
?>
}
I want to put a button on each list row and when it's clicked it populates the first three fields in the form, leaving quantity to be filled out. Then when another is clicked it populates the next form row etc,. It's working fine manually entering from the list, but the list is nearly 5000 items so it's a hassle to keep searching then scrolling up and entering the values.
I don't see how to do this with PHP so I assume I need a javascript function, which is where I'm lost. Let me know if you need more info.
So I'm trying to use a table to update some records in my database but each time I click on update it won't work and it won't do anything. A part of the code below was found in an another topic but it was incomplete so I added some other things.
Js script
$(function(){
$("#loading").hide();
var message_status = $("#status");
$("td[contenteditable=true]").blur(function(){
var field_userid = $(this).attr("id") ;
var value = $(this).text() ;
$.post('update.php' , field_userid + "=" + value, function(data){
if(data != '')
{
message_status.show();
message_status.text(data);
//hide the message
setTimeout(function(){message_status.hide()},1000);
}
});
});
});
This is the table fetching the rows from the database, however everything works besides updating.
HTML & PHP
<form method="post" action="update.php">
<div class="col-sm-12">
<div class="table-responsive">
<table class="table table-striped table-dark">
<tr bgcolor="#df4662" style="color:#FFFFFF;">
<td>ID</td>
<td>Nickname</td>
<td>Name</td>
<td>Rank</td>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td contenteditable="true" id="id:<?php echo $row["id"]; ?>"><?php echo $row["id"]; ?></td>
<td contenteditable="true" id="username:<?php echo $row["username"]; ?>"><?php echo $row["username"]; ?></td>
<td contenteditable="true" id="name:<?php echo $row["steamid"]; ?>"><?php echo $row["steamid"]; ?></td>
<td contenteditable="true" id="ranks:<?php echo $row["ranks"]; ?>"><?php echo $row["ranks"]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
After a few errors I've been able to have a clean error_logs, but now I don't get any error even after pressing the update button.
update.php
<?php
include '../database.php'
?>
<?php
if(!empty($_POST))
{
foreach($_POST as $field_name => $val)
{
$field_id = strip_tags(trim($field_name));
$split_data = explode(':', $field_id);
$id = $split_data[1];
$field_name = $split_data[0];
if(!empty($id) && !empty($field_name) && !empty($val))
{
$affected_rows = mysqli_query($mysqli,"UPDATE users SET $field_name = '$val' WHERE id = $id");
echo $affected_rows;
echo "Updated";
} else {
echo "Invalid Request";
}
}
}
else {
echo "Invalid Requests";
}
?>
EDIT: Thanking Sam now the problem is just that the record won't update at all
OK, so I'm trying to solve this problem from last week and haven't found a solution yet I ask the Question so many times but not able to get the Code working so I'm putting all details in this one Question.
I'm Generating an HTML table with PHP and Mysql query (Table look like this Table)
DB ID EmpID Date Username Computername State Minutesatstate Statestarttime Stateendtime Timestamp
704634 303836 02-06-2019 user PC-818 Idle 2 13:44 13:46 2019-02-06 13:46:46
704599 303836 02-06-2019 user PC-818 Active 16 13:28 13:44 2019-02-06 13:44:46
704340 303836 02-06-2019 user NIPL-1220 Active 2 13:27 13:28 2019-02-06 13:28:48
704313 303836 02-06-2019 user PC-818 Active 13 13:15 13:27 2019-02-06 13:27:31
I want to add another column with PHP or Jquery or Javascript to subtract Statestarttime column of 1st row with Stateendtime column of 2nd row to find out the time difference.
I just want min difference column to be displayed on the page.
example of expected results
StateStarTtime StateEndTime Min Difference
03:57 03:58 00:03
03:53 03:54 00:04
03:46 03:49 null
I'm able to find so many example and help for subtracting StateStarTtime from StateEndTime in the first row but i want to subtract 1st row cell value of StateStarTtime from 2nd row cell value of StateEndTime to find the Minute Difference.
I'm beginner in HTML and PHP but completely new to JS/JQ so any direction on how i can do this is much appreciated. Below are the PHP and HTML codes snippet i'm using. Let me know if you want complete code.
Geting data from Mysql
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
$query = "SELECT * FROM time WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= Timestamp AND EmpID='".$valueToSearch."' ORDER BY `time`.`Timestamp` DESC";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM time WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= Timestamp ORDER BY `time`.`Timestamp` DESC";
$search_result = filterTable($query);
}
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "timetracker");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
Table PHP Code
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td align="Center"><?php echo $row['id'];?></td>
<td align="Center"><?php echo $row['EmpID'];?></td>
<td align="Center"><?php echo $row['Date'];?></td>
<td align="Center"><?php echo $row['Username'];?></td>
<td align="Center"><?php echo $row['Computername'];?></td>
<td align="Center"><?php echo $row['State'];?></td>
<td align="Center"><?php echo $row['MinutesatState'];?></td>
<td align="Center"><?php echo $row['StateStarttime'];?></td>
<td align="Center"><?php echo $row['StateEndtime'];?></td>
<td align="Center"><?php echo $row['Timestamp'];?></td>
</tr>
<?php endwhile;?>
tried solutions - 1
<?php while($row = mysqli_fetch_array($search_result)):?>
<?php $startTime[] = $row['StateStarttime'];?>
<?php $endTime[] = $row['StateEndtime'];?>
<?php endwhile;?>
<?php
$i = 0;
while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td align="Center"><?php echo $row['id'];?></td>
<td align="Center"><?php echo $row['EmpID'];?></td>
<td align="Center"><?php echo $row['Date'];?></td>
<td align="Center"><?php echo $row['Username'];?></td>
<td align="Center"><?php echo $row['Computername'];?></td>
<td align="Center"><?php echo $row['State'];?></td>
<td align="Center"><?php echo $row['MinutesatState'];?></td>
<td align="Center"><?php echo $row['StateStarttime'];?></td>
<td align="Center"><?php echo $row['StateEndtime'];?></td>
<td align="Center"><?php echo $row['Timestamp'];?></td>
//your calculation goes here like diff
<td align="Center">
<?php
$mycalc = ($startTime[$i] - $endTime[$i+1]);
echo $mycalc;
?>
</td>
</tr>
$i++;
<?php endwhile;?>
tried solutions - 2
<?php for($i = 0; $i < count($results); $i++): ?>
<?php
$datetime1 = DateTime::createFromFormat('i:s', $results[$i]['StateStarttime']);
$datetime2 = DateTime::createFromFormat('i:s', $results[$i+1]['StateEndtime']);
if($datetime2)
$interval = $datetime1->diff($datetime2)->format('%I:%S');
else
$interval = 'unknown';
?>
<tr>
<td align="Center"><?php echo $results[$i]['id'];?></td>
<td align="Center"><?php echo $results[$i]['EmpID'];?></td>
<td align="Center"><?php echo $results[$i]['Date'];?></td>
<td align="Center"><?php echo $results[$i]['Username'];?></td>
<td align="Center"><?php echo $results[$i]['Computername'];?></td>
<td align="Center"><?php echo $results[$i]['State'];?></td>
<td align="Center"><?php echo $results[$i]['MinutesatState'];?></td>
<td align="Center"><?php echo $results[$i]['StateStarttime']; ?></td>
<td align="Center"><?php echo $results[$i]['StateEndtime']; ?></td>
<td align="Center"><?php echo $results[$i]['Timestamp'];?></td>
<td align="Center"><?php echo $interval; ?></td>
</tr>
<?php endfor; ?>
Before Anyone suggest me this
<td align="Center"><?php echo($row['StateEndtime'] - $row['StateStarTtime ']) ?></td>
This will give me the Minute Difference of StateStarTtime and StateEndtime in the same row i want to subtract 1st row cell value of StateStarTtime from 2nd row cell value of StateEndTime and so on to find the Minute Difference.
Try something along the lines of this. Only a guideline, the real implementation is up to you. Also I'd recommend you use {} rather than endwhile.
<?php
$last_row = $mysqli_fetch_array($search_result));
while($row = mysqli_fetch_array($search_result)){
//this is pseudocode, you will need to convert to date objects
$timediff = $last_row['StateStarttime'] - $row['StateEndtime'];
?>
<tr>
<td align="Center"><?php echo $row['id'];?></td>
<td align="Center"><?php echo $row['EmpID'];?></td>
<td align="Center"><?php echo $row['Date'];?></td>
<td align="Center"><?php echo $row['Username'];?></td>
<td align="Center"><?php echo $row['Computername'];?></td>
<td align="Center"><?php echo $row['State'];?></td>
<td align="Center"><?php echo $row['MinutesatState'];?></td>
<td align="Center"><?php echo $row['StateStarttime'];?></td>
<td align="Center"><?php echo $row['StateEndtime'];?></td>
<td align="Center"><?php echo $row['Timestamp'];?></td>
// echo $timediff
</tr>
<?php
$last_row = $row;
}?>
I built a form where user can enter Country Name and Country's Dialing Code. That form submits to Database and then I pull the record from database in a Table showing Country Name, Country's Dialing Code and two more options of EDIT and DELETE (having GET URL Link e.g. www.abc.com/country.php?country=Pakistan)
I want to add AJAX to it so that when user clicks on EDIT or DELETE link a relevant pop-up open with data from GET URL.
Following is my Dynamic Table in PHP
<div>
<?php
$q = "SELECT * FROM country";
$result = mysqli_query($conn, $q);
echo "<table border=2><tr><th>Country Name</th><th>Country Code</th><th></th><th></th></tr>";
while($a = mysqli_fetch_array($result)) {
$cn = $a['cname'];
$cc = $a['ccode'];
?>
<tr>
<td><?php echo $cn ?></td> <td><?php echo $cc; ?></td>
<script type="text/javascript">
var a = 0;
var cname = new Array("<?php echo $cn;?>");
a++;
</script>
<td>
<a href='#' onclick='javascript:editWin(cname[a]); return(false);'>Edit</a>
</td>
<td id="<?php echo $cn;?>">
<a href='#' onclick='javascript:delWin(); return(false);'>Remove</a>
</td>
</tr>
<?php
}
?>
</div>
My external Javascript Function is as follows
function editWin(e) {
window.open('edit.php?country='+e,'','height=400, width=600, top=100,
left=400, scrollable=no, menubar=no', '');
};
In GET Url it says undefined when popup window opens.
I got the solution
My PHP Code is as follows
<div> <?php
$q = "SELECT * FROM country";
$result = mysqli_query($conn, $q);
echo "<table border=2><tr><th>Country Name</th><th>Country Code</th><th></th><th></th></tr>";
while($a = mysqli_fetch_array($result)) {
$cn = $a['cname'];
$cc = $a['ccode'];
?>
<tr>
<td><?php echo $cn ?></td> <td><?php echo $cc; ?></td>
<td><a href='#' id="<?php echo $cn; ?>" onclick='javascript:editWin(this.id); return(false);'>Edit</a></td>
<td><a href='#' id="<?php echo $cn; ?>" onclick='javascript:delWin(this.id); return(false);'>Remove</a></td></tr>
<?php
}
?>
</div>
and my Javascript is as follows
function editWin(e) {
window.open('edit.php?country='+e,'','height=400, width=600, top=100, left=400, scrollable=no, menubar=no', '');
};
function delWin(e) {
window.open('del.php?country='+e,'','height=400, width=600, top=100, left=400, scrollable=no, menubar=no', '');
};
I've got the following table on a webpage:
<table class="table table-striped table-condensed">
<thead>
<tr>
<center><th>Name</th><th>Rank</th><th>MOS</th><th>Tag</th><th>MOSTs</th><th>Prom. Date</th><th>DI</th><th>CMO</th><th>MCRC</th><th>Medals</th><th>Leave</th></center>
</tr>
</thead>
<tbody>
<?php
$rank = 'O-10';
$result->execute();
while($row = $result->fetch()) {
?>
<tr>
<td id="habbo_name:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['habbo_name']);?></td>
<td id="rank:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['rank']);?></td>
<td id="rating:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['rating']);?></td>
<td id="tag:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['tag']);?></td>
<td id="asts:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['asts']);?></td>
<td id="promotion_date:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['promotion_date']);?></td>
<td id="rdc_grade:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['rdc_grade']);?></td>
<td id="cnl_trainings:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['cnl_trainings']);?></td>
<td id="mcrc:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['mcrc']);?></td>
<td id="medals:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['medals']);?></td>
<td id="leave:<?php echo htmlspecialchars($row['user_id']); ?>" contenteditable="true"><?php echo htmlspecialchars($row['leave']);?></td>
</tr>
<?php
}
?>
</tbody>
</table>
I have this code which sends a request to ajax.php whenever a td is clicked:
<script type="text/javascript">
$(function(){
$("td[contenteditable=true]").blur(function(){
var field_userid = $(this).attr("id") ;
var value = '$(this).text()' ;
$.post('ajax.php' , field_userid + "=" + value, function(data){
});
});
});
</script>
ajax.php:
<?php
include 'functions/user.php';
if(!empty($_POST))
{
foreach($_POST as $field_name => $val)
{
$field_userid = strip_tags(trim($field_name));
$split_data = explode(':', $field_userid);
$user_id = $split_data[1];
$field_name = $split_data[0];
if(!empty($user_id) && !empty($field_name))
{
$query = "UPDATE `personnel` SET $field_name = '$val' WHERE user_id = $user_id";
$result = $con->prepare($query);
$result->execute();
} else {
echo "Invalid Requests";
}
}
} else {
echo "Invalid Requests";
}
?>
The problem I have is with 3 of the fields - MCRC, Medals and Leave - the others all update fine.
The MCRC field will either be empty or contain the + symbol. Using firebug, this is the POST request when trying to send a +:
As you can see, the + is removed in the parameters. The same goes for medals.
Leave seems to have a different issue. The parameters are fine but the database is not updated with the [TEST]
If anyone could explain a way for me to send symbols/see what's going wrong with leave that would be great. Thanks.
Got it working by changing
var value = $(this).text();
to
var value = encodeURIComponent($(this).text());