2017-08-15 91 views
-1

嗯,我想減去數據庫中的值,只是嘗試任何方法,但它仍然fail.I不知道下面的查詢中的代碼是否工作。請向我解釋清楚,因爲我很難理解代碼的邏輯。我非常感謝,我會竭盡全力謝謝!如何使用數據庫中的複選框減去值

borrow.php

<form method="post" action="borrow_save.php"> 
截止日期 借
  <div class="span8"> 
        <div class="alert alert-success"><strong>Select Book</strong></div> 
         <table cellpadding="0" cellspacing="0" border="0" class="table" id="example"> 

          <thead> 
           <tr> 

            <th>Acc No.</th>         
            <th>Book title</th>         
            <th>Category</th> 
            <th>Author</th> 
            <th>Publisher name</th> 
            <th>status</th> 
            <th>Add</th> 

           </tr> 
          </thead> 
          <tbody> 

           <?php $user_query=mysqli_query($dbcon,"select * from book where status != 'Archive' ")or die(mysqli_error()); 
           while($row=mysqli_fetch_array($user_query)){ 
           $id=$row['book_id']; 

           $copy=$row['book_copies']; 


           $cn=count($id); 
           for($i=0; $i < $cn; $i++) 

             if($copy > 0){ ?> 
              <tr class="del<?php echo $id ?>"> 


           <td><?php echo $row['book_id']; ?></td> 
           <td><?php echo $row['book_title']; ?></td> 
           <td><?php echo $row ['catalog']; ?> </td> 
           <td><?php echo $row['author']; ?> </td> 
           <td><?php echo $row['publisher_name']; ?></td> 
            <td width=""><?php echo $row['status']; ?></td> 
           <?php include('toolttip_edit_delete.php'); ?> 
           <td width="20"> 
              <input id="" class="uniform_on" name="selector[]" type="checkbox" value="<?php echo $id; ?>"> 

           </td> 

           </tr> 
           <?php } 
           ?> 

           <?php } ?> 
          </tbody> 
         </table> 

      </form> 

<script>   



$(".uniform_on").change(function(){ 
var max= 3; 
if($(".uniform_on:checked").length == max){ 

    $(".uniform_on").attr('disabled', 'disabled'); 
      alert('3 Books are allowed per borrow'); 
    $(".uniform_on:checked").removeAttr('disabled'); 

}else{ 

    $(".uniform_on").removeAttr('disabled'); 
} 
}); 

borrow_save.php

<?php 


    $id=$_POST['selector']; 
$member_id = $_POST['member_id']; 
$due_date = $_POST['due_date']; 

if ($id == ''){ 
header("location: borrow.php"); 

}else{ 




mysqli_query($dbcon,"insert into borrow (member_id,date_borrow,due_date) values ('$member_id',NOW(),'$due_date')")or die(mysqli_error($dbcon)); 
$borrow_id = $row['borrow_id']; 
$borrow_id=$dbcon->insert_id; 
mysqli_query($dbcon,"UPDATE book SET book_copies = book_copies - 1 where book_id='$book_id'"); <!- this is the query that I don't know if it's work --> 


$N = count($id); 

for($i=0; $i < $N; $i++) 
{ 


mysqli_query($dbcon,"insert borrowdetails (book_id,borrow_id,borrow_status) values('$id[$i]','$borrow_id','pending')")or die(mysqli_error($dbcon)); 
} 

} 
?> 
+0

你傳入'$ book_id'作爲一個字符串到查詢, book_id是一個整數。你是否已經調試了其餘所有代碼,以確保您的輸入在傳遞到查詢時也是有效的?在插入數據時(尤其是客戶端輸入),您還應該使用[prepared statements](http://php.net/manual/en/mysqli.prepare.php) – MinistryofChaps

+0

您在減法查詢中定義了$ book_id的位置? –

+0

對不起,我將使用準備語句時,此代碼已經運作,所以我會知道如何防止在SQL注入,我不知道該怎麼做在數據庫中添加+ 1的值很容易,但減去這麼難,只是不知道該怎麼做對不起 – Liza

回答

0

您已經在收放線在borrowdetailsbook_idfor -loop內的基礎上,所以最簡單的解決辦法是到更新語句移動到環太:

for($i=0; $i<$N; $i++) { 
    $stmt = mysqli_prepare($dbcon,"UPDATE book SET book_copies = book_copies - 1 where book_id=?") or die(mysqli_error($dbcon)); 
    mysqli_stmt_bind_param($stmt, "i", $id[$i]); 
    mysqli_stmt_execute($stmt) or die(mysqli_error($dbcon)); 

    // Your insert-query (still NEEDS protection against SQL-injection!!!) 
    mysqli_query($dbcon,"insert borrowdetails (book_id,borrow_id,borrow_status) values('$id[$i]','$borrow_id','pending')")or die(mysqli_error($dbcon)); 
} 
+0

嗨!對不起我遲到的回覆,是的,它減去,但當我借了一本書,它減去數據庫中的2本書,順便說一句,感謝您的回覆我真的很感激我很高興看到從您的代碼減去書籍:D – Liza

+0

,當我回來了2本書歸還的書我怎樣才能把它變成1借只2本書謝謝我的朋友! – Liza

+0

「它在數據庫中減去2本書」:你的數組中有2個項目(也會在'borrowdetails'中插入兩行),或者你更新代碼中其他地方的'book'表 - 我不會不明白你的第二個問題:S –

相關問題