更改按钮提交时的单元格值

时间:2016-10-10 22:12:37

标签: php

好的,我被迫让我的高层人士重新启用此功能。但是这次数据将在没有AJAX POST方法的情况下更新。

在我的表格中,我有一个减去按钮和一个加号按钮。每个都作为一个提交按钮,当一个值被添加到一个不同的输入字段时,计数将根据点击的按钮而改变,例如(如果10在输入字段中,则点击加号按钮时为+10)

我的表格没有更新,并且在错误发生时无法回复任何内容。 谢谢你的帮助。

<?php
if(isset($_GET['stock_id'])) {
    $the_stock_id = mysqli_real_escape_string($connection, $_GET['stock_id']);    
}      
$query = 'SELECT stock_id, sku_number, category, description, price, in_stock ';
$query .= 'FROM parts_stock AS a JOIN items AS b ON a.stock_id = b.s_id ';
$query .= "WHERE a.stock_id =".$the_stock_id;

$edit_sku = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($edit_sku)) {
    $stock_id = $row['stock_id'];    
    $sku      = $row['sku_number'];
    $category = $row['category'];
    $desc     = $row['description'];
    $price    = $row['price'];    
    $stock    = $row['in_stock'];
 }

if(isset($_POST['update_stock'])) {
    $price       = $_POST['price'];
    $mod_stock   = $_POST['mod_stock'];
    if(isset($_POST['rem_stock'])) {
        $stock -= $mod_stock;
    echo $stock;
    }elseif(isset($_POST['add_stock'])) {
        $stock += $mod_stock;
    echo $stock;    
    }
    $query = "UPDATE parts_stock, items SET ";
    $query .= "price = '$price', ";    
    $query .= "in_stock = '$stock' ";
    $query .= "WHERE stock_id ='$the_stock_id' ";

    $update_stock = mysqli_query($connection, $query);
    confirmQuery($update_stock);

  $alert = <<<DELIMETER
 <div class='alert alert-warning alert-dismissible fade in' role='alert'>
 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
 </button>
 <strong>Inventory Updated!</strong> <a href='inventory.php?view_all_inventory'>View All Inventory</a>
 </div>
DELIMETER;
}
?>
<div class="col-xs-12 col-sm-12 col-md-12">
    <h2>Edit Inventory Item</h2>
    <?php echo $alert; ?>
<hr>
<table class="table table-bordered table-responsive table-striped">
<thead class="thead-inverse">
    <tr class="alert alert-success">
        <th>SKU #</th>
        <th>Category</th>
        <th>Description</th>
        <th>Price</th>
        <th>Current Stock</th>
        <th>+ / - Stock</th>
        <th>Action</th>
    </tr>
</thead>
<tbody>
  <tr>
     <form role='form' action="" method="POST">
      <td><input value="<?php echo $sku; ?>" type="text" class="form-control" name="sku_number" readonly ></td>
      <td><input value="<?php echo $category; ?>" type="text" class="form-control" name="category" readonly></td>
      <td><input value="<?php echo $desc; ?>" type="text" class="form-control" name="description" readonly></td>
      <td><input value="<?php echo $price; ?>" type="text" class="form-control" name="price" ></td>
      <td><input value="<?php echo $stock; ?>" type="text" class="form-control" name="in_stock" readonly ></td>
      <td><input value="" type="text" class="form-control" name="mod_stock">    </td>
      <td class='btn-group'>
          <button class='btn btn-danger btn-sm' type='submit' name='update_stock' value='rem_stock'><i class='glyphicon glyphicon-minus'></i></button>
          <buton class='btn btn-success btn-sm' type='submit' name='update_stock' value='add_stock'><i class='glyphicon glyphicon-plus'></i></buton>
      </td>
      </form>
     </tr>                     
   </tbody>    
</table>                                                             

</div>    

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些缺陷,例如:

  • 请看以下一行,

    <buton class=...</i></buton>
     ^^^^^                ^^^^^
    

    应该是button,而不是buton

  • if(isset($_POST['rem_stock'])) {...}elseif(isset($_POST['add_stock'])) {错误。正确的if条件是,

    if($_POST['update_stock'] == 'rem_stock') { ...
    

    }elseif($_POST['update_stock'] == 'add_stock'){ ...
    
  • 您从$the_stock_id获得了$_GET['stock_id'],因此,一旦您提交表单,您就不会有任何$_GET['stock_id']将其存储在$the_stock_id中变量。因此,请在提交表单后使用$_POST['in_stock']$_POST['sku_number']

所以你的PHP代码应该是这样的:

// your code

if(isset($_POST['update_stock'])) {
    $price       = $_POST['price'];
    $mod_stock   = $_POST['mod_stock'];
    $stock = $_POST['in_stock'];
    $the_stock_id = $_POST['sku_number'];

    if($_POST['update_stock'] == 'rem_stock') {
        $stock -= $mod_stock;
    }elseif($_POST['update_stock'] == 'add_stock') {
        $stock += $mod_stock;    
    }
    $query = "UPDATE parts_stock, items SET ";
    $query .= "price = '$price', ";    
    $query .= "in_stock = '$stock' ";
    $query .= "WHERE stock_id ='$the_stock_id'";

    $update_stock = mysqli_query($connection, $query);
    confirmQuery($update_stock);

  $alert = <<<DELIMETER
    <div class='alert alert-warning alert-dismissible fade in' role='alert'>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <strong>Inventory Updated!</strong> <a href='inventory.php?view_all_inventory'>View All Inventory</a>
    </div>
DELIMETER;
}

,您的提交按钮应如下所示:

<button class='btn btn-success btn-sm' type='submit' name='update_stock' value='add_stock'><i class='glyphicon glyphicon-plus'></i></button>

注意:

  • 始终打开错误报告,在PHP脚本的最顶部添加这两行以调试任何与语法相关的问题。

    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
  • 了解prepared statements因为您的查询现在容易受到SQL注入的影响。另请参阅how you can prevent SQL injection in PHP