Kindly help me update the page which shows data after fetching using a dropdown?

1.This is my view page where I select from dropdown and data filters using it.


<?php
 $con = mysqli_connect("localhost","root","","loginsystem");
 $sql = "select * from stock";

 $res = mysqli_query($con, $sql);
?>


<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="fetch.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
    </head>
<body>
<b class="d-inline p-2 bg-primary text-white">Update Stock Here</b><hr>    
<b>Select Item name here to update the Stock :</b> 
    <select id="itemname" onchange="selectItem()">
        <option value="Select Item here" selected>Select Item here</option>
    <?php while ($rows = mysqli_fetch_array($res)){
    ?>
        
    <option value="<?php echo $rows['itemname']; ?>"><?php echo $rows['itemname']; ?></option>
    <?php    
}
    ?>   
    </select><br><br>
    <table class="table table-bordered">
    <thead class="table-primary">
                <th>Stock ID</th>
                <th>Purchase ID</th>
                <th>P.O Quantity</th>
                <th>Unit Number</th>
                <th>Entry Date</th>
                <th>Stock Quantity</th>
                <th>Delivery Dates</th>
                <th>Update Stocks</th>
        </thead>
        <tbody id="ans"></tbody>
    </table>
    </body>
</html>
  1. This is my script
function selectItem(){
    
    var x = document.getElementById("itemname").value;
    
    $.ajax({
       url:"showItem.php",
        method: "POST",
        data:{
            id : x
        },
        success:function(DataView){
        $("#ans").html(DataView);
    }
})
}
  1. This is my table where i fetch data’s
<?php
 $k = $_POST['id'];
 $k = trim($k);
 $con = mysqli_connect("localhost","root","","loginsystem");
 $sql = "select * from stock where itemname ='{$k}'";
 $res = mysqli_query($con, $sql);
 while($rows = mysqli_fetch_array($res)){
?>

  <tr>
      <td><?php echo $rows['id'];?></td>
      <td><?php echo $rows['purchaseid'];?></td>
      <td><?php echo $rows['poqty'];?></td>
      <td><?php echo $rows['unitno'];?></td>
      <td><?php echo $rows['entrydate'];?></td>
      <td><?php echo $rows['stockqty'];?></td>
      <td><?php echo $rows['deliver'];?></td>
       <td><a href="stockedit.php?id=<?php echo $row["id"]; ?>">Update</a></td>
  </tr>
<?php
                                        }
echo $sql;
?>

Now on the table i want to edit and then update my data kindly help. thanks

What have you tried so far? You’ll need to draw a form, either one for all items, or it might be more suitable to have an individual form for each row. Add suitable html to present the data that needs to be edited in form fields with the original value shown, then add a submit button. Do you want to edit each one individually in stockedit.php, or add in-line editing to the list you’re creating with this code?

When you run into trouble, add more detail showing where you’re having trouble and someone will be able to help.

While you’re in the PHP code, there are a couple of things you should really address:

  1. Don’t use select * when you only use certain columns, select them individually. This has the added bonus that you can look at the query to see which column names are being retrieved.

  2. Use prepared statements instead of concatenating user-supplied variables into your query as you do. You’re leaving yourself wide open for SQL attacks, and prepared statements also deal properly with things like single-quotes which will break your query as it stands now.

I’d also suggest that you use the unique ID of the stock item rather than the description to retrieve the information, the same as you would when it comes to storing linked information such as stock transactions, invoices etc. If you have a unique constraint on the description then I guess it doesn’t matter so much.

i want to edit each individually in stockedit.php can you be kind enough to show me some code examples ?

You’ve got a bit of a disconnect using GET id here

"stockedit.php?id=<?php echo $row["id"]; ?>"

And "Grabbing with POST, i.e. $k = $_POST['id'];

But then you query with this value against the field itemname.
So yes, besides the already mentioned suggestions there are so other issues to fix before you can even expect things to start working.

yes sir i am not being able to update it how do i edit using $k = $_POST['id']; kindly help please

I’m happy to help with your code as much as I am able to, but not to write it from scratch for you. Break it down into steps and have a go.

  1. In stockedit.php, retrieve the id from either $_GET or $_POST, however you pass it in.
  2. Get the information out of your database and display it in a html form for the user to enter new values where appropriate.
  3. Accept the submitted form data and update your database, either in stockedit.php or a separate script file.

As you’ve already got some code to retrieve all of the items in the code you posted, steps 1 and 2 should be relatively easy, and there are plenty of examples of how to get form data and update a database with it, it’s just a case of adapting one to suit your needs. If you find an example that doesn’t use prepared statements, though, by-pass it and look for something more up to date.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.