dsgk40568 2017-08-15 18:19
浏览 17
已采纳

如何使用数据库中的复选框减去该值

Well I want to subtract the value in database and just try any method but it still fail.I don't know if the codes in query in below is working. Please explain it to me fluently because I'm hard to understanding the logic of codes. I really appreciate and I'll try to do my best Thank you!

borrow.php

    <form method="post" action="borrow_save.php">
Due Date Borrow
            <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));
}

}  
?>
  • 写回答

1条回答 默认 最新

  • duancaishi1897 2017-08-17 16:59
    关注

    You are already inserting in borrowdetails on a per book_id basis within your for-loop, so the easiest solution would be to move that update statement to that loop too:

    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));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写