1

I'm doing a database project for university and I'm having a problem in here.

I receive from a previous page an id as $_POST['ids'] and in the form I send that same value in a hidden field so it can do a sort of a cicle.

But when I click the submit button I got a lot of errors on $service_info and no information is loaded on the page. I tried do var_dump() everything and I just can't find what is the problem in here.

<?php
//error_reporting();
require 'core/init.php';
require 'db/connect.php';
require 'functions/security.php';
?>

<html>
    <head>
        <title>Make a reservation</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="css/common.css">
    </head>
    <body>

        <?php require 'parts/header.php'; ?>
        <hr>

        <?php
        $query = "SELECT * FROM service WHERE id=" . $_POST['ids'];         
        if ($result = $db->query($query)) {
            if ($result->num_rows) {

                $service_info = $result->fetch_object();
                $result->close();
            }
        }


        $query = "SELECT name FROM tour WHERE id =" . $service_info->idtour;
        if ($result = $db->query($query)) {
            if ($result->num_rows) {

                $tour_name = $result->fetch_object();
                $result->close();
            }
        }

        $query = "SELECT SUM(nrseats) AS res_seats FROM reservation_service WHERE idservice =" . $service_info->id;
        $nr_reservations_info = $db->query($query);
        $nr_reservations = $nr_reservations_info->fetch_row();
        $nr_reservations_info->close();
        $count = $service_info->nrseats - $nr_reservations[0];
        if($count === 0){
                echo "<script>alert('There are no more places available for this tour. You are being redirected for the main page!')</script>";
                echo "<script>window.open('index.php','_self')</script>";
            }
        else{
            $count = $service_info->nrseats;
        }
        ?> 

        <form action="" method="POST">
            <div class="registering">
                <table>
                    <tbody>    
                        <tr>
                            <td>
                                <label for="tname">Related tour</label>
                            </td>
                            <td>
                                <label for="splace"><br>Service name</label>
                            </td><p><br></p>
                    </tr>
                    <tr>
                        <td>
                            <input type="text" readonly="" name="tour" id="tour" required="" autofocus="" value="<?php echo $tour_name->name ?>">
                        </td>
                        <td>
                            <input type="text" readonly="" name="name" id="name" required="" value="<?php echo $service_info->name ?>">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="sprice"><br>Price (€)</label>
                        </td>
                        <td>
                            <label for="sdescription"><br>Description</label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="number" name="price" id="price" readonly="" required="" value="<?php echo $service_info->price ?>">
                        </td>
                        <td>
                            <input type="text" name="description" id="description" required="" readonly="" value="<?php echo $service_info->description ?>">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="sseats"><br>Seats left</label>
                        </td>
                        <td>
                            <label for="snreservations"><br>Number of reservations (people)</label>
                        </td>
                    </tr>
                    </tr>
                    <tr>
                        <td>
                            <input type="number" name="nrseats" id="nrseats" required="" value="<?php echo $count ?>" readonly="">
                        </td>
                        <td>
                            <input type="number" name="nrreservations" id="nrreservations" required="" value="1">
                        </td>
                        <td>
                            <input type="hidden" name="ids"  required="" value="<?php $service_info->id ?>">
                        </td>
                    </tr>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <label for="next"><br></label>
                            <input type="submit" value="Next">
                        </td>
                    </tr>
                    </tbody>
                </table>            
            </div>
        </form>
    </body>
</html>

<?php

if (!empty($_POST)) {

    if (isset($_POST['name'], $_POST['ids'], $_POST['tour'], $_POST['price'], $_POST['description'], $_POST['nrseats'], $_POST['nrreservations'])) {

        $_POST = array_map("trim", $_POST);

        $name = $_POST['name'];
        $tour = $_POST['tour'];
        $price = $_POST['price'];
        $description = $_POST['description'];
        $nrseats = $_POST['nrseats'];
        $nrreservations = $_POST['nrreservations'];
        $ids = $_POST['ids'];

        if (!empty($name) && !empty($ids) && !empty($tour) && !empty($price) && !empty($description) && !empty($nrseats) && !empty($nrreservations)) {

            $query = "SELECT id FROM customer WHERE email='" . $_SESSION['user_email'] . "'";
            if ($result = $db->query($query)) {

                $id_user = $result->fetch_object();
                $result->close();
            }

            $query = "SELECT id FROM reservation WHERE idtour={$service_info->idtour} AND idcustomer={$id_user->id}";
            if ($result = $db->query($query)) {
                if ($result->num_rows) {

                    $id_reservation = $result->fetch_object();
                    $result->close();
                }
            }

            $query = "SELECT * FROM reservation_service WHERE idservice=" . $service_info->id;
            if ($result = $db->query($query)) {

                if ($result->num_rows) {
                    $reservation_service_exists = $result->fetch_object();

                if ($nrreservations < 1) {
                    echo "<script>alert('Your  must make a reservation for, at least, one person!')</script>";
                    echo "<script>window.open('new_reservation_service.php','_self')</script>";
                } else if ($count - $nrreservations < 0) {
                    echo "<script>alert('You can not make the reservation because there are only " . $count . " seats available in this tour!')</script>";
                    echo "<script>window.open('new_reservation_service.php','_self')</script>";
                } else if ($result->num_rows) {

                    $query = "SELECT * FROM reservation WHERE idcustomer= '" . $id_user->id . "' AND idtour= '" . $service_info->idtour . "'";
                    if ($result = $db->query($query)) {

                        if ($result->num_rows) {
                            $reservation_exists = $result->fetch_object();
                            $result->close();

                            if ($reservation_exists->idcustomer === $id_user->id) {
                                if ($reservation_exists->id === $reservation_service_exists->idreservation) {
                                    echo "<script>alert('You already made a reservation for this service. Please see your reservation panel!')</script>";
                                    echo "<script>window.open('reservations.php','_self')</script>";
                                }
                            }
                        }
                    } 
                }
            }else {

                        $query = "INSERT INTO reservation_service (idreservation, idservice, date, nrseats) VALUES (?, ?, NOW(), ?)";
                        $insert = $db->prepare($query);
                        $insert->bind_param('iii', $id_reservation->id, $service_info->id, $nrreservations);
                        $insert->execute();
                        echo "<script>alert('You successfully made a reservation! You are being redirected to your reservations page')</script>";
                        echo "<script>window.open('reservations.php','_self')</script>";
                    }
                }            
        }
    }
}
?>
9
  • what kind of error are you facing it? Commented Feb 12, 2015 at 18:35
  • First: you form action are empty(action=""). second, nóis é BR mano o/ Commented Feb 12, 2015 at 18:37
  • 1
    Oh GOD. What the Heck you trying to do with this code? Its totally unstructured. I found many possible errors in it. Commented Feb 12, 2015 at 18:42
  • @bcesars Undefined variable: service_info , Trying to get property of non-object , Fatal error: Call to a member function fetch_row() on a non-object. But this only happens after I click the submit button and that's strange. Commented Feb 12, 2015 at 18:42
  • @ErasmoOliveira the action is empty because I'm not targeting the info to another page. The insert query is in the bottom. Commented Feb 12, 2015 at 18:43

1 Answer 1

3

change inside your form this input hidden you created:

 <input type="hidden" name="ids"  required="" value="<?php $service_info->id ?>">

to

 <input type="hidden" name="ids"  required="" value="<?php echo $service_info->id ?>">

If you don't echoing this value, $_POST['ids'] won't be get any value passed from form.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.