Value inserting on last row - php

Shown in the image above, I have two entries on my stock_transfer table.
As an example, I will insert two different values to show you that it's working. (5 stocks on Product 1 and 10 stocks on Product 2)
Now, when I try to insert the stocks requested remaining on Product 1 and leave the Product 2, it will be successful.
Here comes the problem. When I try to insert value (290 stocks) on my Product 2, it will insert the value in the Product 1 table.
Can anyone help me with this? Here's my query code:
if (isset($_POST['addCart']) && $_POST['addCart']=="Confirm Stock Transfer") {
foreach($_POST['qtyBuy'] as $index=>$value){
if($value > 0){
$cartProd_id = $_POST['product_id'][$index];
$cartProd_name = $_POST['product_name'][$index];
$cartProd_date = $_POST['product_exp'][$index];
$cartProd_desc = $_POST['product_desc'][$index];
$cartProd_transid = $_POST['transfer_id'][$index];
//Update stock_transfer requests
$update_stock = "UPDATE stock_transfer SET stocks_transferred = stocks_transferred + $value WHERE transfer_id = $cartProd_transid;";
$update_stockE = mysqli_query($connection, $update_stock);
Here's is my code:
<table class="table table-striped table-bordered table-hover results table-fixed table-condensed" id="example">
<thead>
<tr>
<th>#</th>
<th>Product Name</th>
<th>Description</th>
<th>Sell Price</th>
<th>Expiry Date</th>
<th>Instock</th>
<th>Stocks Requested Remaining</th>
<th>Stocks Transferred</th>
<th>Quantity to Transfer</th>
</tr>
</thead>
<tbody>
<?php
$getReq = "SELECT * FROM stock_transfer INNER JOIN td_products ON stock_transfer.transfer_product_id = td_products.product_id WHERE stock_transfer.transfer_number = $tid AND stock_transfer.transfer_tobranch = '$capitalPrefix';";
$getReqE = mysqli_query($connection, $getReq);
while ($row = mysqli_fetch_array($getReqE)) {
$transfer_id = $row['transfer_id'];
$transfer_number = $row['transfer_number'];
$transfer_status = $row['transfer_status'];
$transfer_frombranch = $row['transfer_frombranch'];
$transfer_tobranch = $row['transfer_tobranch'];
$transfer_product_id = $row['transfer_product_id'];
$transfer_quantity = $row['transfer_quantity'];
$transfer_date = $row['transfer_date'];
$stocks_transferred = $row['stocks_transferred'];
$remainingStocks = $transfer_quantity - $stocks_transferred;
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$product_price = $row['sell_price'];
$description = $row['description'];
$quantity = $row['quantity'];
$expiry_date = $row['expiry_date'];
echo $transfer_id;
?>
<tr>
<td class="text-center"><?php echo $product_id; ?>
<input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>">
</td>
<input type="hidden" name="transfer_id[]" value="<?php echo $transfer_id; ?>">
<td><?php echo $product_name; ?>
<input type="hidden" name="product_name[]" value="<?php echo $product_name; ?>">
</td>
<td><?php echo $description; ?>
<input type="hidden" name="product_desc[]" value="<?php echo $description; ?>">
</td>
<td>₱ <?php echo number_format($product_price, 2); ?></td>
<td><?php echo $expiry_date; ?>
<input type="hidden" name="product_exp[]" value="<?php echo $expiry_date; ?>">
</td>
<td><strong><?php echo $quantity; ?></strong></td>
<td><?php echo $remainingStocks; ?></td>
<td><?php echo $stocks_transferred; ?></td>
<td>
<?php if (!$remainingStocks == 0){ ?>
<input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>">
<?php } else{ ?>
<i class="glyphicon glyphicon-check"></i> Completed
<?php } ?>
</td>
</tr>
<?php }?>
</tbody>
</table>
<div class="form-group">
<input type="submit" name="addCart" value="Confirm Stock Transfer" class="btn btn-info pull-right">
Back to Request List
</div>
Tags
Here's my schema:

See this:
<td>
<input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>" <?php echo ($remainingStocks == 0') ? 'Disabled=true' : '';?> value="0">
</td>

for security issues, I advise you to use ajax and submit just the qty input for one row each time:
<table class="table table-striped table-bordered table-hover results table-fixed table-condensed" id="example">
<thead>
<tr>
<th>#</th>
<th>Product Name</th>
<th>Description</th>
<th>Sell Price</th>
<th>Expiry Date</th>
<th>Instock</th>
<th>Stocks Requested Remaining</th>
<th>Stocks Transferred</th>
<th>Quantity to Transfer</th>
</tr>
</thead>
<tbody>
<?php
$getReq = "SELECT * FROM stock_transfer INNER JOIN td_products ON stock_transfer.transfer_product_id = td_products.product_id WHERE stock_transfer.transfer_number = $tid AND stock_transfer.transfer_tobranch = '$capitalPrefix';";
$getReqE = mysqli_query($connection, $getReq);
while ($row = mysqli_fetch_array($getReqE)) {
$transfer_id = $row['transfer_id'];
$transfer_number = $row['transfer_number'];
$transfer_status = $row['transfer_status'];
$transfer_frombranch = $row['transfer_frombranch'];
$transfer_tobranch = $row['transfer_tobranch'];
$transfer_product_id = $row['transfer_product_id'];
$transfer_quantity = $row['transfer_quantity'];
$transfer_date = $row['transfer_date'];
$stocks_transferred = $row['stocks_transferred'];
$remainingStocks = $transfer_quantity - $stocks_transferred;
$product_id = $row['product_id'];
$product_name = $row['product_name'];
$product_price = $row['sell_price'];
$description = $row['description'];
$quantity = $row['quantity'];
$expiry_date = $row['expiry_date'];
echo $transfer_id;
?>
<tr>
<td class="text-center"><?php echo $product_id; ?>
</td>
<td><?php echo $product_name; ?>
</td>
<td><?php echo $description; ?>
</td>
<td>₱ <?php echo number_format($product_price, 2); ?></td>
<td><?php echo $expiry_date; ?>
</td>
<td><strong><?php echo $quantity; ?></strong></td>
<td><?php echo $remainingStocks; ?></td>
<td><?php echo $stocks_transferred; ?></td>
<td>
<?php if (!$remainingStocks == 0){ ?>
<input type="number" name="qtyBuy[]" id="qty_buy_<?= $transfer_id ?>" min="1" max="<?php echo $remainingStocks;?>"><a class="btn btn-sm add_qty" data-id="<?= $transfer_id ?>"></a>
<?php } else{ ?>
<i class="glyphicon glyphicon-check"></i> Completed
<?php } ?>
</td>
</tr>
<?php }?>
</tbody>
</table>
<div class="form-group">
<input type="submit" name="addCart" value="Confirm Stock Transfer" class="btn btn-info pull-right">
Back to Request List
</div>
<script>
$(function() {
$('.add_qty').click(function() {
var id = $(this).data('id');
var qty = $("#qty_buy_"+id).val();
$.ajax({
type: 'GET',
url:'/your_action',
data: {id : id, qty : qty},
dataType: 'JSON',
}).done(function(json){
// code to update the values in your row
});
});
});
</script>

The problem was on the
<?php if (!$remainingStocks == 0){ ?>
<input type="number" name="qtyBuy[]" id="<?php echo "qtyBuy" . $b++; ?>" min="1" max="<?php echo $remainingStocks;?>">
<?php } else{ ?>
<i class="glyphicon glyphicon-check"></i> Completed
<?php } ?>

I think the problem is with your input names. You have assigned arrays as names of your inputs. Try to make them unique using the product id.

Related

Values from textbox array not updating

I'm new in this forum and I'm having a hard time updating selected value from checkbox coming from the textbox value.
Scenario:
I want to update selected items, For example, I want to update Item-2 and the textbox from item received will be enabled, I will enter a number for example 1 and the textbox from total receive will automatically sum using AJAX.
The problem is after submitting the value from total receive the records that were updating is blank, but when I try to check and print_r the value is there.
And one more thing if all checkboxes are checked and enter a number for each item received, the value that it only get is the last value and will be updated will all selected checkbox.
Notes:
The checkbox is an array
Textbox from total receive is an array
Here's my UI:
Controller:
public function recitem_insert(){
$this->load->model('dbquery');
$check = $this->input->post('check');
$total_rec = $_POST['total_rec'];
if(isset($check)){ //Check if check is checked
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$updateData = array('rec_qty' => $total_rec[$j] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$i]);
}
}//end for loop
echo "<script type='text/javascript'>
alert('Successfully Added!');
window.close();
</script>";
}else{ //End If
echo 'Please select a checkbox';
}
}
View:
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<div class="box">
<div class="box-header">
<h3 class="box-title">System ID: <b><?php echo $process_id; ?></b></h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Action</th>
<th>Item Code</th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $row){
?>
<tr>
<td><input type="checkbox" name="check[]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[]"></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
Model:
public function modUpdatedynamicval($table, $column, $data, $equal_to){
$this->db->where($column, $equal_to);
$this->db->update($table, $data);
}
Edit
Let's assume:
I've input 1 in item_receive textbox one and the total receive will be 10,
2 in item_receive textbox two and the total receive will be 11,
3 in item_receive textbox three and the total receive will be 12,
CODE:
$check = $this->input->post('check');
$total_rec = $_POST['total_rec'];
echo 'Check Value';
print_r($check);
echo '<br><br>';
echo 'Total Recieve';
print_r($total_rec);
OUTPUT:
Check ValueArray ( [0] => 1 [1] => 2 [2] => 3 )
Total RecieveArray ( [0] => 10 [1] => 11 [2] => 12 )
But If I only input the second textbox here's the output:
Check ValueArray ( [0] => 2 )
Total RecieveArray ( [0] => [1] => 11 [2] => )
From what I see, you are having a problem to determine which input index is which on the back end. You could set an index key on each of the input like this :
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $key => $row){ //added key to be used as inputs key
?>
<tr>
<td><input type="checkbox" name="check[<?php echo $key ?>]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[<?php echo $key ?>]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[<?php echo $key ?>]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[<?php echo $key ?>]"></td>
</tr>
<?php
}
?>
</tbody>
After that, then replace the for loop :
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$updateData = array('rec_qty' => $total_rec[$j] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$i]);
}
}//end for loop
With this foreach :
foreach ($check as $key => $item) {
$updateData = array('rec_qty' => $total_rec[$key] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$key]);
}
So you don't manually check for the total_rec value but instead it only checks on submitted check post data.
from what i can see, like everybody else is saying, you have trouble in finding the correct rows of checked item.
I suggest you edit the way you name the form in multidimensional array. bet you didnt know that?
so in view.php
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<div class="box">
<div class="box-header">
<h3 class="box-title">System ID: <b><?php echo $process_id; ?></b></h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Action</th>
<th>Item Code</th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
//WTF?? it's 2019. Say hello to SQL Injection
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $i=>$row){
?>
<tr>
<td><input type="checkbox" name="data[<?php echo $i;?>][check]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="data[<?php echo $i;?>][last_item_rec]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="data[<?php echo $i;?>][item_rec]" id="txt" disabled=""></td>
<td><input type="text" name="data[<?php echo $i;?>][total_rec]"></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
and you can easily cycle through the data for each rows while at the same time validating if the checkbox is checked
<?php
//simple way of traversing the data
if ( isset( $_POST['data'] ) )
{
echo '<table>';
foreach ( $_POST['data'] as $d )
{
//you have all the data here
//start checking on checked box
if(isset($d['check']))
{
echo '<tr>';
echo ' <td>', $d['last_item_rec'], '</td>';
echo ' <td>', $d['item_rec'], '</td>';
echo ' <td>', $d['total_rec'], '</td>';
echo '</tr>';
}
}
echo '</table>';
}
Can try with row key (item id). Update your array key with $row->id.
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $row){
?>
<tr>
<td><input type="checkbox" name="check[]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[<?php echo $row->id; ?>]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[<?php echo $row->id; ?>]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[<?php echo $row->id; ?>]"></td>
</tr>
<?php
}
?>
</tbody>
Scenraio Like:
1 item Id "2"
2 item Id "3"
3 item Id "5"
If you check first st row and first row item_id ($row->id) is 2 and enter 7 than you will get array like :-
print_r($_POST['total_rec']);
Output Like:
Array([2]=>7)
You can easy to find data using array key
Update:
<form method="post" action="<?php echo base_url(); ?>">
<div class="box">
<div class="box-header">
<h3 class="box-title">
System ID: <b> <?php echo '12'; ?></b>
</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th> Action</th>
<th>Item Code </th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
//$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
$new_item_list = array(
array('id'=> '1','item_code'=> 'ITEM-1','description' => 'ITEM Desc', 'qty_order'=> '2','rec_qty'=> '2'),
array('id'=> '2','item_code'=> 'ITEM-2','description' => 'ITEM Desc 2', 'qty_order'=> '3','rec_qty'=> '3' ),
array('id'=> '3','item_code'=> 'ITEM-3','description' => 'ITEM Desc', 'qty_order'=> '5','rec_qty'=> '4' ),
array('id'=> '4','item_code'=> 'ITEM-4','description' => 'ITEM Desc', 'qty_order'=> '1','rec_qty'=> '3' )
);
foreach ($new_item_list as $row){
?>
<tr>
<td><input type="checkbox" name="<?php echo $row['id']; ?>[check]" id="opt" value="<?php echo $row['id']; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row['item_code']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['qty_order']; ?></td>
<td><?php echo $row['rec_qty']; ?></td>
<input type="hidden" name="<?php echo $row['id']; ?>[last_item_rec]" value="<?php echo $row['rec_qty']; ?>">
<td><input type="text" name="<?php echo $row['id']; ?>[item_rec]" id="txt" value='' ></td>
<td><input type="text" name="<?php echo $row['id']; ?>[total_rec]" value=''>
</td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
Submit Action: For example
foreach($_POST as $key => $value){
if(isset($value['check'])){
//Row ID of item table $key
$total = $value['total_rec'] * $value['item_rec'];
echo "Total Number :".$total."</br>";
#here is your update query
}
}
Link:- https://prnt.sc/mswdoc
If check checkbook and submit from then you will get output like
Output:- https://prnt.sc/mswefv
Your checkboxes all have the same ID, which always leads to problems. Change the id of the checkbox in the loop.
<td><input type="checkbox" name="check[]" id="opt-<?php echo $row->id; ?>" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>

Can't delete without getting "Undefined Offset" error

I'm doing a very simple project for my assignment and it's about shopping cart. I've created a cart page by using SESSION with array and index to keep track user's order. The problem is whenever I'm deleting a meal from my cart list, there'll be an error which is "Notice: Undefined offset: 1", "Notice: Undefined offset: 2", etc. I'm attaching my codes here:
This is my cart page. The error said it has something to do on the line that I have commented:
<form method="POST" action="save_cart.php">
<table class="table">
<tbody>
<thead>
<th></th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
</thead>
<?php
//initialize total
$total = 0;
if(!empty($_SESSION['cart'])){
//create array of initail qty which is 1
$index = 0;
if(!isset($_SESSION['qty_array'])){
$_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
}
$sql = "SELECT * FROM meal WHERE meal_id IN (".implode(',',$_SESSION['cart']).")";
$query = $conn->query($sql);
while($row = $query->fetch_assoc()){
?>
<tr>
<td class="removebtn" style="text-align: center;">
<a href="delete_item.php?id=<?php echo $row['meal_id']; ?>&index=<?php echo $index; ?>" >Remove</a>
</td>
<td><?php echo $row['meal_name']; ?></td>
<td>RM <?php echo number_format($row['meal_price'], 2); ?></td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td class="qtyinput" ><input type="number" min="1" max="5" class="form-control" value="<?php echo $_SESSION['qty_array'][$index]; ?>" name="qty_<?php echo $index; ?>"></td>
//THE ERROR IS HERE
<td>RM <?php echo number_format($_SESSION['qty_array'][$index]*$row['meal_price'], 2); ?></td>
<?php $total += $_SESSION['qty_array'][$index]*$row['meal_price']; ?>
</tr>
<?php
$index ++;
}
}
else{
?>
<tr>
<td colspan="5" class="text-center" style="text-align: center;">No Meal in Cart</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="left"><b>Total</b></td>
<td><b>RM <?php echo number_format($total, 2); ?></b></td>
</tr>
</tr>
Here's my delete page:
session_start();
//remove the id from our cart array
$key = array_search($_GET['meal_id'], $_SESSION['cart']);
unset($_SESSION['cart'][$key]);
unset($_SESSION['qty_array'][$_GET['index']]);
//rearrange array after unset
$_SESSION['qty_array'] = array_values($_SESSION['qty_array']);
$_SESSION['message'] = "<script>alert('Meal is deleted from your cart');</script>";
header('location: cart.php');
I really need your help. Thanks in advance!
Its an index error because index doesn't exists in the array replace your html php code inside while loop with this one.
<tr>
<td class="removebtn" style="text-align: center;">
<a href="delete_item.php?id=<?php echo $row['meal_id']; ?>&index=<?php echo $index; ?>" >Remove</a>
</td>
<td><?php echo $row['meal_name']; ?></td>
<td>RM <?php echo number_format($row['meal_price'], 2); ?></td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td class="qtyinput" ><input type="number" min="1" max="5" class="form-control" value="<?php echo (isset($_SESSION['qty_array'][$index]) ? $_SESSION['qty_array'][$index] : 0); ?>" name="qty_<?php echo $index; ?>"></td>
//THE ERROR IS HERE
<td>RM <?php
$qty = (isset($_SESSION['qty_array'][$index]) ? $_SESSION['qty_array'][$index] : 0);
echo number_format($qty *$row['meal_price'], 2); ?></td>
<?php $total += $qty*$row['meal_price']; ?>
</tr>

Generate multiple table (For viewing)

I would like to ask on how to make my table to appear as following:
But I only manage to get my table to appear like this:
As you can see, the table is separated according to the different year or semester of when they stored their stuff. It is basically a history storage of that particular person. Unfortunately, I don't know how to make the generated table to attach together for the one with same year and semester instead of separating it. Even the numbering is affected. Below is the code that I have so far:
<?php
include("connect.php");
include("header.php");
if(isset($_GET['invstuview_bag'], $_GET['invstuview_name']))
{
$get_id = $_GET['invstuview_bag'];
$get_name = $_GET['invstuview_name'];
?>
<br/>
<ul class="breadcrumb">
<li class="breadcrumb-item">View History</li>
<li class="breadcrumb-item-active">Baggage Detail History</a></li>
</ul>
<div id="cssword">Baggage Detail History For <?php echo $get_name; ?>(<?php echo $get_id; ?>)</div>
<div class="container" style="width:70%;">
<div class="table-responsive">
<?php
$sql_join = "SELECT student.*,location.*, inventory.*, baggage.*
FROM
student,location, inventory, baggage
WHERE
student.student_id = inventory.invstu_id AND
baggage.baggage_id = inventory.invbag_id AND
location.location_id = inventory.invloc_id AND
invstu_id = '$get_id'
ORDER BY inventory_id";
$result_join= mysqli_query($connect,$sql_join);
$prev_year = "";
$prev_sem = 0;
$get_year = "";
$get_sem = 0;
while($row_join=mysqli_fetch_assoc($result_join))
{
$counter = 1;
$prev_year = $row_join["invstu_year"];
$prev_sem = $row_join["invstu_sem"];
//if the data is of same year and semester
if(($prev_year!="") && ($prev_sem!=0) && ($prev_year == $get_year) && ($prev_sem == $get_sem))
{
$get_year = $row_join["invstu_year"];
$get_sem = $row_join["invstu_sem"];
?>
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row_join["baggage_type"]; ?></td>
<td><?php echo $row_join["invbag_quantity"]; ?></td>
<td><?php echo $row_join["location_house"]; ?></td>
</tr>
<?php
$counter++;
echo'</table>';
}
//if data is not of same year or semester
else
{
$get_year = $row_join["invstu_year"];
$get_sem = $row_join["invstu_sem"];
?>
</br></br>
Room: <?php echo $row_join["invstu_room"]; ?><br/>
Year: <?php echo $row_join["invstu_year"]; ?><br/>
Semester: <?php echo $row_join["invstu_sem"]; ?><br/>
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row_join["baggage_type"]; ?></td>
<td><?php echo $row_join["invbag_quantity"]; ?></td>
<td><?php echo $row_join["location_house"]; ?></td>
</tr>
<?php
$counter++;
echo'</table>';
}
}
?>
</div>
<div align="right">
<button type="button" name="back" class="btn btn-success" id="back" style="width:200px; display:inline-block; margin-top:2%; margin-bottom:1%; background-color: #4CAF50" onclick="window.location.href='view.php'">Back</button>
</div>
</div>
<?php
}
?>
Any ideas is highly appreciated.
Move these out of the while loop:
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
echo'</table>';

Trying to update column values by submit button

I'm trying to update status on my leaves table by pressing the approve/reject button. Here's my table schema:
So far this is what i've done:
<?php
if(isset($_POST['approved']))
{
echo "Approved";
$status=$_POST['approved'];
}
if(isset($_POST['rejected']))
{
echo "Rejected";
$status=$_POST['rejected'];
}
?>
<!-- Begin page content -->
<div class="container">
<div class="page-header">
<h3>Employee Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Status</th>
<th>---</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM leaves order by id DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['full_name']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update.php">
<input type="hidden" value="<?php echo $row_message['id']; ?>" />
<input type="submit" value="Approved" name="approved"></input>
<input type="submit" value="Rejected" name="rejected"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
and here's my update.php
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'leaves');
$sql = "UPDATE leaves SET status = :status WHERE id = :id";
if(mysqli_query($con, $sql))
header("refresh:1; url=view-leave.php");
else
echo "Error";
?>
I'm getting the "error" when I click on approve/reject. I think the problem is with the update.php. I'm not sure what it is tho or where.
Try this : update.php
<?php
if(isset($_POST['approved']))
{
$msg = "Approved";
$status=$_POST['approved'];
}
if(isset($_POST['rejected']))
{
$msg = "Rejected";
$status=$_POST['rejected'];
}
$id=$_POST['id'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'leaves');
$sql = "UPDATE newusers SET username = '$status' WHERE id = '$id'";
if(mysqli_query($con, $sql))
header("refresh:1; url=index.php?msg=$msg");
else
var_dump(mysqli_error($con));
?>
and view-leave.php
<?php
if(isset($_GET['msg']))
{
echo $_GET['msg'];
}
?>
<!-- Begin page content -->
<div class="container">
<div class="page-header">
<h3>Employee Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Status</th>
<th>---</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM leaves order by id DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['full_name']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<?php echo $row_message['id']; ?>" />
<input type="submit" value="Approved" name="approved"></input>
<input type="submit" value="Rejected" name="rejected"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
You have to write database name here:
mysqli_select_db($con, 'dbname');
Did you try to declare a default value for the $msg=''; and $status = '';?

Select Query incomplete

I have 17 records inserted in my table "book".
<?php
include 'config.php';
session_start();
$sql = "SELECT * from book";
$result = mysqli_query($db, $sql);
$count = mysqli_num_rows($result);
if($count == 0){
$error = "There is no books in the list";
}
?>
<html>
<body>
<table border="2" style="width:80%;" align="center">
<tr>
<th>ISBN</th>
<th>Title of the book</th>
<th>Cost</th>
<th>Copies</th>
<th>Edition</th>
<th>Publisher</th>
<th>Copy Year</th>
<th>Shelf Number</th>
<th>Subject</th>
</tr>
<?php while($row = mysqli_fetch_array($result)){
$ISBN = $row['ISBN'];
$Title = $row['Title'];
$Cost = $row['Cost'];
$Copies = $row['Copies'];
$Edition = $row['Edition'];
$Publisher = $row['Publisher'];
$CopyYr = $row['CopyYr'];
$Shelf = $row['ShelfNo'];
$Subject = $row['SubName'];
?>
<tr>
<td><?php echo $ISBN; ?></td>
<td><?php echo $Title; ?></td>
<td><?php echo $Cost; ?></td>
<td><?php echo $Copies; ?></td>
<td><?php echo $Edition; ?></td>
<td><?php echo $Publisher; ?></td>
<td><?php echo $CopyYr; ?></td>
<td><?php echo $Shelf; ?></td>
<td><?php echo $Subject; ?></td>
</tr>
<?php
}
?>
</table>
<br>
<table align="center" style="width: 30%">
<tr>
<th><form action="AddBook.php" method="post">
<input type="submit" value="Add Book"/>
</form></th>
<th><form action="editbook.php" method="post">
<input type="submit" value="Edit List"/>
</form></th>
<th><form action="deletebook.php" method="post">
<input type="submit" value="Delete Book/s"/>
</form></th>
<th><form action="AdminSummary.php" method="post">
<input type="submit" value="Back"/>
</form></th>
</tr>
</table>
</center>
</body>
</html>
This will display 17 records in the table. But when I add another record to the table, It will display an incomplete record, a "500 server error", or an empty table.
I have been in the same problem for days, searching for a solution, and I can't find any.
I don't know what the problem is, so any help would be appreciated. Thank You :)

Categories