0

I have this if statement

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {

        $(document).on('submit', '#rentform', function () {
            var data = $(this).serialize()

            $.ajax({
                type: 'POST',
                url: 'newrent.php',
                data: data,
                success:(
                    function (data){$(".result").html(data)}

                )
            });
            return false;
        });
    } )

    $(document).ready(function () {
        $('#rent').change(function(){
            var x =($('#rent option:selected').data('sireal'))
            $('#itemsn').val(x);
        });
    } )
</script>


<form method="post" id="rentform" action="" novalidate="novalidate">
    <label for="item">REnter Name</label>
    <select name="rnt_name" id="name_rent">
        <option selected disabled>SELECT Renter Name</option>
        <?php foreach ($nameq as $res) : ?>
            <option value="<?= $res['user_name']?>"><?= $res['user_name'] ?></option>
        <?php endforeach; ?>
    </select>
    <br/>
    <br/>

    <label for="item">Cameras</label>
    <select name="item" id="rent">
        <option selected disabled>SELECT ITEM</option>
<?php foreach ($rentq as $row) : ?>
    <option value="<?= $row['item_brand']?>" data-sireal="<?= $row['s_n'] ?>"><?= $row['item_brand'] ?></option>
   <?php endforeach; ?>
    </select><br/>
    <br/>
<input type="hidden" name="sn" id="itemsn" value="">
    <label name="from_date">RENT FROM</label>
    <input type="date" name="from_date"><br/>
    <label name="from_hour">RENT FROM Hour</label>
    <input type="time" name="from_hour"><br/>
    <label name="to_date">RENT TO</label>
    <input type="date" name="to_date"><br/>
    <label name="to_hour">RENT FROM Hour</label>
    <input type="time" name="to_hour"><br/>
<input type="submit" name="submit" value="Send Rent">
</form>

<div class="result">

</div>

if($_POST){

 $name = $_POST['rnt_name'];
 $item = $_POST['item'];
 $fdate = $_POST['from_date'];
 $fhour = $_POST['from_hour'];
 $tdate = $_POST['to_date'];
 $thour = $_POST['to_hour'];
 $sn = $_POST['sn'];


 $csheck = "SELECT * FROM rent";
 $ckq = mysqli_query($link,$csheck);

    foreach ($ckq as $res) {}

   $count = "SELECT item_brand = '$item',s_n = '$sn',COUNT(*)
FROM item
WHERE status = 1 AND item_brand = '$item' AND s_n = '$sn'";
    $cnt = mysqli_query($link,$count);
 foreach ($cnt as $itm);

$itmq = "SELECT rent_item = '$item',COUNT(*)
FROM rent ";
$itemres = mysqli_query($link,$itmq);

foreach ($itemres as $missitm){};

if(($fdate >= $res['rent_from_d'] && $res['rent_to_d'] >= $tdate && $missitm['COUNT(*)'] >= $itm['COUNT(*)']   ) || ($fdate > $res['rent_from_d'] && $res['rent_to_d'] > $tdate && $fdate > $res['rent_from_d'] || $fdate <= $res['rent_to_d'] && $fhour <= $res['rent_to_t'] && $missitm['COUNT(*)'] >= $itm['COUNT(*)'] ) )  {
    echo 'no good Item Rented To '.$res['renter'] ;

 }else{
     $newrent = "INSERT INTO rent (renter,rent_item,rent_from_d,rent_from_t,rent_to_d,rent_to_t) 
VALUES ('$name','$item','$fdate','$fhour','$tdate','$thour')";
     mysqli_query($link,$newrent);

    echo 'pass';

 }

this is what I get in my database after form submit

rent_id renter  rent_item   sn  rent_from_d rent_from_t rent_to_d   rent_to_t
115    dror        x70     1234 2018-11-29  10:00     2018-11-29    14:00

What I am trying to do is create a rent system. My problem is how to validate if items are selected between two dates and time. Every time new rent is created, the status of the specific item that has been chosen set to 0, so that the system will no longer available.

Thanks in advance.

dror shalit
  • 318
  • 2
  • 12

0 Answers0