为什么我的页面刷新了?

时间:2014-04-15 11:06:44

标签: php

我有一个小时段预订系统,在那里我可以点击一个名为:Reserve的链接,然后我保留给定的时间。

但是,点击保留后,页面不会刷新。因此,用户可以两次单击相同的保留链接。他们不应该这样做。

if (isset ( $_GET ['reserved'] )) { 
    $sqlreserve = "INSERT INTO calendar (eventDate, timeslot) VALUES ('" . $dateToCompare . "','" . intval($_GET['t']) . "');";
    $resultreserve = mysqli_query ( $mysqli1, $sqlreserve );
    if ($resultreserve) {
        header('Location: '.$_SERVER['PHP_SELF']);
    } else {
        echo "Event Failed to add";
    }
}

如果我的插入有效,那么我打电话给:header('Location: '.$_SERVER['PHP_SELF']);

我正在研究localhost,如果有什么可说的话吗?

编辑:

我创建链接的方式和预订广告位的文字是这样的:

    if (mysqli_num_rows ( $result ) == 0) {
        echo "<a href='" . $_SERVER ['PHP_SELF'] . "?month=" . $month . "&day=" . $day . "&year=" . $year . "&t={$time}&v=true&f=true&reserved=true'><h3 style='color: rgb(255,0,0);'>Reserve</h3></a>";
    } else {
        echo "<h3>Not Available, taken by:</h3>";
        while ( $row = mysqli_fetch_array ( $result ) ) {
            echo "<br />";
        }

}

EDIT。我的错误:

无法修改标题信息 - 已经发送的标题(输出已启动.....)

for($i = 1; $i < $numDays; $i ++, $counter ++) {
                $timeStamp = strtotime ( "$year-$month-$i" );
                if ($i == 1) {
                    $firstDay = date ( "w", $timeStamp );
                    for($j = 0; $j < $firstDay; $j ++, $counter ++) {
                        echo "<td>&nbsp;</td>";
                    }
                }
                if ($counter % 7 == 0) {
                    echo "</tr><tr>";
                }
                $monthstring = $month;
                $monthlength = strlen ( $monthstring );
                $daystring = $i;
                $daylength = strlen ( $daystring );
                if ($monthlength <= 1) {
                    $monthstring = "0" . $monthstring;
                }
                if ($daylength <= 1) {
                    $daystring = "0" . $daystring;
                }

                $todaysDate = date ( "m/d/Y" );
                $dateToCompare = $monthstring . '/' . $daystring . '/' . $year;
                echo "<td align='center' ";
                if ($todaysDate == $dateToCompare) {
                    echo "class='today'";
                } else {
                    $sqlCount = "SELECT * FROM calendar WHERE eventDate='" . $dateToCompare . "'";
                    $noOfEvent = mysqli_num_rows ( mysqli_query ( $mysqli1, $sqlCount ) );
                    if ($noOfEvent >= 1) {
                        echo "class='event'";
                    }
                }
                echo "><a href='" . $_SERVER ['PHP_SELF'] . "?month=" . $monthstring . "&day=" . $daystring . "&year=" . $year . "&v=true'>" . $i . "</a></td>";
            }

受影响的行是:

echo "><a href='" . $_SERVER ['PHP_SELF'] . "?month=" . $monthstring . "&day=" . $daystring . "&year=" . $year . "&v=true'>" . $i . "</a></td>";

在另一个文件中,我有我的日历,其中我链接到特定日期,我不想预订时间段:

enter image description here

2 个答案:

答案 0 :(得分:6)

试试这个..

header('Location: '.$_SERVER['PHP_SELF']);
exit;

我认为您的代码仍在继续......

可以在此处找到更深入的解释:Why I have to call 'exit' after redirection through header('Location..') in PHP?

修改

现在很清楚,Milen Georgiev的回答是正确的。在到达标题()之前将内容输出到浏览器;您需要在PHP代码的顶部移动if语句以避免标头内容错误。

if (isset ( $_GET ['reserved'] )) { 
    $sqlreserve = "INSERT INTO calendar (eventDate, timeslot) VALUES ('" . $dateToCompare . "','" . intval($_GET['t']) . "');";
    $resultreserve = mysqli_query ( $mysqli1, $sqlreserve );
    if ($resultreserve) {
        header('Location: '.$_SERVER['PHP_SELF']);
        exit;
    } else {
        echo "Event Failed to add";
    }
}

答案 1 :(得分:2)

你是否在标题之前发送(输出/回显)任何内容('Location:'。$ _ SERVER ['PHP_SELF']); ?

如果是这种情况,则必须打开对象缓冲区。使用标题,您必须先发送标题,否则它将无效。