2016-10-10 2 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

사용'경우 ($ _POST [ 'update_stock'] == 'rem_stock') ...' –

답변

1

같은 코드에서 몇 가지 결함이있다 :

그것은 button하지 buton

  • 을해야

    <buton class=...</i></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']을 사용하십시오.

      // 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; 
      } 
      

      하고 제출 버튼은 다음과 같이해야합니다 :

    그래서 PHP 코드는 다음과 같이해야한다

    <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을 참조하십시오.

  • +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'] 올바른 값을 계속받을. –

    관련 문제