How do i make this?

How do I make it when I add a qty number, say that qty number have been added from this date or say have been update qty number from this date?
For example, there are 32 in the database and I add 5 more and that say 5 more were added on this date

source

<?php include_once 'header.php'; ?>
<?php include_once 'sidebar.php'; ?>
<?php include_once 'navtop.php'; ?>
<?php
if ($_SESSION['admin_type'] != "admin") {
  header("Location: addPatient.php?add");
}
 $del = output(@$_GET['del']);
if(isset($del) && $del!=""){
  if(isset($_SESSION['admin_type'])) {
    if($_SESSION['admin_type'] == 'admin') {
     $stm = $db->prepare("DELETE FROM implant WHERE id=:id");
     $stm->bindParam(":id", $del, PDO::PARAM_STR);
     $stm->execute();
     re("success"," thank you "," deleted successfully. ");
     direct("imp_list.php");
    }
   }
 }
 ?>
                <div class="container-fluid">
                  <div class="card shadow mb-4">
                    <div class="card-header py-3">
                      <h6 class="m-0 font-weight-bold text-gray-800">Implant System</h6>
                    </div>
                   <div class="card-body">
                     <?php
                     $stm = $db->prepare("SELECT * FROM implant ORDER BY id ASC");
                     $stm->execute();
                     $rowCount = $stm->rowCount();
                     if($rowCount > 0){
                     ?>
                     <div class="table-responsive">
                         <table class="table table-bordered" cellspacing="0" id="datatl">
                             <thead>
                                 <tr>
                                     <th>#</th>
                                     <th>Type</th>
                                     <th>Quantity</th>
                                     <th>add Quantity</th>
                                     <th>Update Qty Date</th>
                                     <th>Registration Date</th>
                                     <th>Action</th>
                                 </tr>
                             </thead>
                             <tbody>
                               <?php
                               $nu = 0;
                               while($row = $stm->fetch(PDO::FETCH_ASSOC)) {
                                 ?>
                                 <tr>
                                     <td><?php echo $row['id']; ?></td>
                                     <td><?php echo $row['type']; ?></td>
                                     <td>
                                       <?php
                                       if ($row['qty'] <= 5) {
                                         echo '<span class="badge badge-pill badge-danger">'.$row['qty'].' دانە</span>';
                                       }elseif ($row['qty'] > 5 AND $row['qty'] <= 10) {
                                         echo '<span class="badge badge-pill badge-warning">'.$row['qty'].' دانە</span>';
                                       }else {
                                         echo '<span class="badge badge-pill badge-success">'.$row['qty'].' دانە</span>';
                                       }
                                        ?>
                                     </td>
                                     <td>
                                       <input type="hidden" class="pid" value="<?= $row['id'] ?>">
                                       <input type="number" class="form-control itemQty" value="<?= $row['qty'] ?>" style="width:75px;">
                                     </td>
                                     <td><?php echo $row['update']; ?></td>
                                     <td><?php echo $row['date_created']; ?></td>
                                     <td>
                                       <a href="edit_implant.php?id=<?php echo htmlentities($row['id']);?>" class="btn btn-warning btn-sm"><i class="fa fa-edit"></i></a>
                                       <a href="imp_list.php?del=<?php echo $row['id']; ?>" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a>
                                     </td>
                                 </tr>
                                 <?php $nu++;}?>
                             </tbody>
                         </table>
                     </div>
                     <?php
                     }else {
                       echo 'not avalable';
                     }
                      ?>
                  </div>
                </div>
              </div>
              <script type="text/javascript">
              $(document).ready(function() {

                // Change the item quantity
                $(".itemQty").on('change', function() {
                  var $el = $(this).closest('tr');

                  var pid = $el.find(".pid").val();
                  //var pprice = $el.find(".pprice").val();
                  var qty = $el.find(".itemQty").val();
                  location.reload(true);
                  $.ajax({
                    url: 'action.php',
                    method: 'post',
                    cache: false,
                    data: {
                      qty: qty,
                      pid: pid
                      //pprice: pprice
                    },
                    success: function(response) {
                      console.log(response);
                    }
                  });
                });

              });
              </script>
<?php include_once 'footer.php'; ?>

By inserting a new row for every transaction that affects a value, with all the - Who (caused the row to be inserted), What (was changed), When (the row was inserted), Where (the row was inserted from - ip address), and Why (the the row was inserted) information about the transaction.

1 Like

can you give more information.?

There are 4 operations you can perform on data - Create (Insert), Read (Select), Update, and Delete (CRUD.) Any learning resource you use should teach these fundamentals. For the three operations that perform an action on the data (CUD), you would use a post method form (the code you have posted is using a link/get request for the delete operation, this is incorrect usage.)

For what you are asking, keeping track of the quantity of items, you would have an item table, that defines the items that are available. The id (autoincrement primary index) in this table becomes the item id. You use the item id in any related table, such as the inventory table that would hold a row for each transaction that affects the basic receiving/loss of inventory of items.

As you ‘consume’ items, such as selling them, or perhaps using them for a patient in a medical situation, you would have an order table, that holds the one-time/unique information about each order. This table would have a patient_id column to track who the medical items within each specific order were used for. The id in this table becomes the order id that would be used in any related tables, such as an order_item table that holds a row for each item in an order.

To get the current quantity for any/all item ids, you would use a UNION query between the inventory table and the order_item table to SUM() the quantities for the id(s) you are interested in.

There are some issues in the posted code -

  1. The post method form processing code should be above the start of the html document.
  2. You should use ‘require’ for things you code must have for it to work. Forget about using ‘include’ or ‘include_once’ in these cases.
  3. You should not store use permissions in session variables. Only store the user id in a session variable, then query on each page request to get any other user information.
  4. Every redirect needs an exit/die statement to stop php code execution.
  5. After you have checked the user permissions and halted code execution if the user is not allowed to access a page, you know that the user is authorized to access the rest of the page and you don’t need to check the user permissions in that code.
  6. As already mentioned above, you should use a post method form for the delete operation.
  7. Whatever the output() function is doing, it is probably inappropriate for input data. The only thing you should do to input data before using it is to trim it.
  8. Don’t ever use the @ error suppressor.
  9. In real life applications, data is almost never actually deleted. It has a ‘status’ column that is updated to indicate it is no longer used and is excluded from most database operations.
  10. If you build sql query statements in a php variable, it makes debugging easier.
  11. If you use a ? prepared query place-holder, it simplifies all the typing needed for a query.
  12. If you use implicit binding, by supplying an array of data to the ->execute([…]) call, it simplifies the code needed for each query.
  13. Any ‘get method’ code that gets/produced data needed to display the page should be above the start of the html document. Fetch an data from this query into an appropriately named php variable, then test/loop over this variable at the appropriate location in the html document.
  14. If you set the default fetch mode to assoc when you make the database connection, you don’t need to specify it in each fetch statement, simplifying the code.
  15. Any dynamic value that you output in a html context should have htmlentities() applied to it to help prevent cross site scripting.
  16. The current operation of performing an ajax request on each change in the quantity value will cause repeated changes, for example, if you change 32 to 3, when you delete the leading 3 it will cause an ajax request using 2 as the value, when you delete the 2 it will cause an ajax request using an empty value, and when you type the final 3 it will cause an ajax request using 3 as the value.
Sponsor our Newsletter | Privacy Policy | Terms of Service