douxi3977 2018-10-20 04:44
浏览 53

如何查询值并将其添加到日期

I have a problem, you see I borrow a book getting the book_id and the date borrow is set to the date today, but each book has a days_id value that I need to add to the day of the borrow_date now I'm having trouble how can I get that date_id from the book_id I chosen and add it inside my borrow_date so that I can generate my due_date automatically

here is my PHP code

<?php

    include 'connect.php';


     $librarian_id  =   $_POST['librarian_id'];
     $member_id     =   $_POST['member_id'];
     $book_id       =   $_POST['book_id'];
     $date_borrow   =   $_POST['date_borrow'];
     $status_id     =   $_POST['status_id'];   


    $days = "SELECT days_id, book_id FROM book_setup 
            WHERE book_id  = '$book_id' AND days_id = " . $row['book_id'] . " ";
    $result = mysqli_query($conn, $days);
    $due_date = date('Y-m-d', strtotime($date_borrow. ' +  $result days'));


if      ($librarian_id == '') {
    echo "librarian is empty";
}
else if     ($member_id == '') {
    echo "member id is empty";
}
else if     ($book_id == '') {
    echo "book is empty";
}
else if   ($date_borrow == '') {
    echo "date borrow is empty";
}

else if ($status_id == '') {
    echo "status is empty";
}
else if ($due_date == '') {
    echo "due date is empty";
}



else {



    $sql = " INSERT INTO borrow_book (librarian_id, member_id, 
            book_id, date_borrow, due_date, status_id) 
            VALUES 
            ('$librarian_id', '$member_id', '$book_id', 
             '$date_borrow', '$due_date',  '$status_id' ) ";

    if ($conn->query($sql) === TRUE) 
        {
            $message = "Borrowing book successful";
            echo "<script type='text/javascript'>alert('$message');</script>";

        } 

    else 

        {
            $message = "Borrowing book failed Failed";
            echo "<script type='text/javascript'>alert('$message');</script>";
        } 

    }

$conn->close();
?>

and this is my HTML code

<?php
    include 'php/header.php';
    require_once 'php/connect.php';

?> 
<form method="POST" action="php/borrow.php">
<h1>Borrow Book</h1>

    Transacted by:
    <br>
    <div>
    <select name="librarian_id">
    <option>Select Librarian
        <?php

        $sql = "SELECT * FROM librarian";
        $result = mysqli_query($conn, $sql);
        while ($row =mysqli_fetch_assoc($result)) {
        echo "<option value='" . $row['librarian_id'] ."'>" 
         . $row['Fname'] . " " . $row['Lname'] . "</option>";
        }
        ?>
    </option>
    </select><br><br>   
    </div>
    Select Member
    <br>
    <div>
        <select name="member_id">
            <option>Select member
                <?php

                    $sql = "SELECT * FROM members";
                    $result = mysqli_query($conn, $sql);
                    while ($row =mysqli_fetch_assoc($result)) {
                    echo "<option value='" . $row['member_id'] ."'>" 
                     . $row['Fname'] . " " . $row['Lname'] . "</option>";
                    }
                ?>
            </option>
        </select><br><br>   
    </div>

    <div>
        <select name="book_id">
            <option>Select a Book
                <?php

                    $sql = "SELECT book_setup.book_id, book.book_id, book.title FROM book_setup 
                    INNER JOIN book ON book_setup.book_id = book.book_id WHERE book_setup.status_id = '1' ";
                    $result = mysqli_query($conn, $sql);
                    while ($row =mysqli_fetch_assoc($result)) {
                    echo "<option value='" . $row['book_id'] . "'>" 
                     . $row['title'] . "</option>";
                    }

        ?>

            </option>
        </select><br><br>   
    </div>


    Borrow Date:
    <br><br>
    <input name="date_borrow"  value="<?php echo date('Y-m-d'); ?>" readonly>

    <br><br>

    Status:
  <div>
        <select name="status_id">
            <option>
                <?php

                    $sql = "SELECT * FROM borrower_status";
                    $result = mysqli_query($conn, $sql);
                    while ($row =mysqli_fetch_assoc($result)) {
                    echo "<option value='" . $row['status_id'] ."'>" 
                     . $row['status'] . "</option>";
                    }
                ?>
            </option>
        </select><br><br>   
    </div>
 <br>
 <br>
  <button class="button" type="submit">Submit</button>
  <button type="reset" value="Reset">Reset</button>
</form>

this is my database for borrowing book

and this is where I get the book that is available to borrow

  • 写回答

1条回答 默认 最新

  • dpb42021 2018-10-20 05:57
    关注

    Do this:

    $res = mysqli_query($con, " SELECT days_id FROM book_setup WHERE book_id  = '$book_id' ");
    
    $days = mysqli_fetch_assoc($res)['days_id'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看