0

Purpose: To update an inventory database by using the onchange function by modifying the data displayed in a PHP table.

I am pulling my data from a database and displaying it in a table. I have the data displayed in text fields so they are editable. Once the data is edited my function uses the data provided by POST, preferably the item ID and the value, will be used to update the inventory.

Here is my 'inventory.php' code:

   $db = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
   $inventory = mysqli_query($db, "SELECT item_id, item_name, item_quantity, item_cost, item_price FROM inventory");
   $num_rows = mysqli_num_rows($inventory);
   mysqli_close($db);
      echo "<form id=\"update_add_inventory\" action=\"\" method=\"post\">";
      echo "<table>";
      echo "<tr>";
      echo "<th>Item ID</th>";
      echo "<th>Item Name</th>";
      echo "<th>Item Quantity</th>";
      echo "<th>Item Cost</th>";
      echo "<th>Item Price</th>";
      echo "</tr>";
      $i = 1;
         while($row = mysqli_fetch_array($inventory))
         {
            echo "<tr>";
            echo "<td>".$row['item_id']."</td>";
            echo "<td><input type=\"text\" name=\"item_".$i."_name\" onchange=\"updateInventory('update_inventory.php')\" value='".$row['item_name']."' /></td>";
            echo "<td><input type=\"text\" name=\"item_".$i."_quantity\" onchange=\"updateInventory('update_inventory.php')\" value='".$row['item_quantity']."' /></td>";
            echo "<td><input type=\"text\" name=\"item_".$i."_cost\" onchange=\"updateInventory('update_inventory.php')\" value='".$row['item_cost']."' /></td>";
            echo "<td><input type=\"text\" name=\"item_".$i."_price\" onchange=\"updateInventory('update_inventory.php')\" value='".$row['item_price']."' /></td>";
            echo "</td></tr>";
            $i++;
         }

      echo "</table><br>";
      echo "</form>";

Here is my 'onchange function':

function updateInventory(action)
{
  document.getElementById('update_add_inventory').action = action;
  document.getElementById('update_add_inventory').submit();
}

Here is my 'update_inventory.php' code:

     $db = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
     $inventory = mysqli_query($db, "SELECT item_id, item_name, item_quantity, item_cost, item_price FROM inventory");
     $num_rows = mysqli_num_rows($inventory);

     $i = 1;

     for ($n=1; $n<=$num_rows; $n++) {
        if (isset($_POST['item_'.$i.'_name'])) {
           $item_id = $_POST['item_'.$i.'_id'];
           $item_name = $_POST['item_'.$i.'_name'];
           $result = mysqli_query($db, "UPDATE inventory SET item_name='".$item_name."' WHERE item_id='".$item_id."'");
        } else if (isset($_POST['item_'.$i.'_quantity'])) {
           $item_id = $_POST['item_'.$i.'_id'];
           $item_name = $_POST['item_'.$i.'_quantity'];
           $result = mysqli_query($db, "UPDATE inventory SET item_quantity='".$item_quantity."' WHERE item_id='".$item_id."'");
        } else if (isset($_POST['item_'.$i.'_cost'])) {
           $item_id = $_POST['item_'.$i.'_id'];
           $item_name = $_POST['item_'.$i.'_cost'];
           $result = mysqli_query($db, "UPDATE inventory SET item_cost='".$item_cost."' WHERE item_id='".$item_id."'");
        } else if (isset($_POST['item_'.$i.'_price'])) {
           $item_id = $_POST['item_'.$i.'_id'];
           $item_name = $_POST['item_'.$i.'_price'];
           $result = mysqli_query($db, "UPDATE inventory SET item_price='".$item_price."' WHERE item_id='".$item_id."'");
        }
     $i++;
     }
     mysqli_close($db);
     header('Location: inventory.php');

What I'm having trouble with is that I have been unable to find a way to pass the 'item ID' and the value being modified to the update script. I can do it by passing the data of what I need through the value and then using list and explode to separate them but by doing that I'm displaying the item ID in each text field, which is not good.

If you know of a way to pass both pieces of data to the script, using the onchange function, I'd appreciate the assistance.

4

1 回答 1

1

You can add it as a parameter to your onChange function, then update a hidden field on your form with this value before it is submitted.

PHP

updateInventory('update_inventory.php', ".$row['item_id'].")

JavaScript

function updateInventory(action, item_id){
    // update hidden form value with item_id
    document.getElementById('item_id').value = item_id;
    document.getElementById('update_add_inventory').action = action;
    document.getElementById('update_add_inventory').submit();
}
于 2013-05-18T01:53:20.337 回答