2016-10-10 68 views
0

好吧,我被強制將此功能備份到我的上級。但是這次數據將會在沒有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>  
+0

使用'if($ _POST ['update_stock'] =='rem_stock')...' –

回答

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'){ ... 
    
  • 你從$_GET['stock_id']越來越$the_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> 

說明(S):

  • 總是打開錯誤報告,將這兩行添加到PHP腳本的最頂部,以調試任何與語法相關的問題。

    ini_set('display_errors', 1); 
    error_reporting(E_ALL); 
    
  • 瞭解prepared statements因爲現在你的查詢是容易受到SQL注入。另見how you can prevent SQL injection in PHP

+0

Thanks @Rajdeep。我有用$ _GET ['stock_id']定義的$ the_stock_id。看我的代碼。我將很快看到在這裏編輯我的代碼,以防止SQL注入。 –

+0

@ cpt-crunchy看到那裏,你從'$ _GET ['stock_id']'獲得庫存ID,所以一旦你提交表單,你就不會有任何'$ _GET ['stock_id']',這意味着表單提交後'$ the_stock_id'將不可用。正如我在回答中所說的,使用'$ _POST ['in_stock']'和'$ _POST ['sku_number']''。我已經更新了我的答案。 –

+0

我明白你在說什麼。但是,即使在表單提交之後,我的代碼運行正常,並且我仍繼續在$ _GET ['stock_id']上收到正確的值。 –