在php中提交表单时出现问题

时间:2015-02-12 18:34:05

标签: php forms post

我正在为大学做一个数据库项目,而我在这里遇到了问题。

我从上一页收到一个ID为$ _POST [' ids'],并且在表单中我会在隐藏字段中发送相同的值,以便它可以执行某种类型的操作。

但是当我点击提交按钮时,我在$ service_info上遇到了很多错误,并且页面上没有加载任何信息。我尝试过var_dump()所有内容,我只能在这里找不到问题。

<?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>";
                    }
                }            
        }
    }
}
?>

1 个答案:

答案 0 :(得分:3)

在表单内部更改此隐藏您创建的输入:

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

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

如果您没有回显此值,$ _POST [&#39; ids&#39;]将无法从表单中获取任何值。