2016-10-10 3 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条件は、あなたがフォームを送信する一度ので、あなたがそれを格納するために、任意の$_GET['stock_id']を取得されることはありません、$_GET['stock_id']から$the_stock_idを取得している、

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

    }elseif($_POST['update_stock'] == 'add_stock'){ ... 
    
  • になり$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スクリプトの最上部にこれら2行を追加して、構文に関連する問題をデバッグします。

    ini_set('display_errors', 1); 
    error_reporting(E_ALL); 
    
  • 今すぐあなたのクエリはSQLインジェクションの影響を受けやすいのでに関するprepared statementsこちらをご覧ください。また、how you can prevent SQL injection in PHPを参照してください。

+0

ありがとう@Rajdeep。 $ _GET ['stock_id']で$ the_stock_idが定義されています。私のコードを見てください。 SQLインジェクションを防ぐためにすぐにここで私のコードを編集しています。 –

+0

@ cpt-crunchy '$ _GET ['stock_id']'から在庫IDを取得していますので、フォームを送信すると、 '$ _GET ['stock_'']はありません。つまり、フォーム提出後に '$ the_stock_id'は利用できません。私の答えで言ったように、 '$ _POST ['in_stock']'と '$ _POST ['sku_number']'を使いましょう。私は私の答えを更新しました。 –

+0

私はあなたが言っていることを理解しています。しかし、私のコードは正しく機能しており、フォームが送信された後も$ _GET ['stock_id']に正しい値を引き続き受け取ります。 –

関連する問題