如果mysql字段完成,则重定向的PHP代码

时间:2012-02-07 01:04:02

标签: php mysql

我想创建一个页面,如果某人已经输入了他们的数据,则会将其重定向到一个页面,其中显示“抱歉,您已经做出了选择”。最简单的字段是出勤1,其值为“是”和“是”。或者“不是”所以,如果它是这些值中的任何一个,它将重定向到我想要的页面。我的页面代码位于:http://pastebin.com/1D6PrmBv

4 个答案:

答案 0 :(得分:2)

试试这个。

include('connect.inc');
if (( $rows['food'] == "yes" ) || ( $rows['food'] == "no" )) {
header('location: mypagelocation.html');
exit;
}

或者你也可以这样写。

include('connect.inc');
    if (in_array($rows['food'], array("yes","no"))) {
    header('location: mypagelocation.html');
    exit;
    }

答案 1 :(得分:2)

include('connect.inc');
if (isset($rows['food'])) {
    header(location: mypagelocation.html');
    exit;
}
else{
    // $rows['food'] is not set
}

答案 2 :(得分:1)

include('connect.inc');
if ( $rows['food'] == "yes" OR $rows['food'] == "no" ) {
    header('location: mypagelocation.html');
}

确保之前没有输出。

答案 3 :(得分:0)

完整代码:

<?php
session_start();
require_once("connect.inc");
if(!isset($_SESSION['username'])){
    header('Location: connect.php');
    exit;
}else{
    $sql = "SELECT attendance1 FROM user WHERE username = '".mysql_real_escape_string($_SESSION['username'])."'";
    $res = mysql_query($sql);
    $row = mysql_fetch_array($res);
    if(($row[0] == "Yes") || ($row[0] == "No")){
        header("Location: redirect_page.html");
        exit;
    }
}
if(isset($_POST['submit'])){
    $sql = "UPDATE user SET attendance1= '" . mysql_real_escape_string($_POST['attendance1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
    mysql_query($sql) or die("Error in SQL: " . mysql_error());
    $sql = "UPDATE user SET gender= '" . mysql_real_escape_string($_POST['gender']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
    mysql_query($sql) or die("Error in SQL: " . mysql_error());
    header("Location: index.html", true, 303); // Look up "303: See Other"
    exit;
}
?>