1

I would like to create page that if someone has already entered their data it redirect them to a page where it says 'sorry you have already made your selection'. The easiest field would be attendance1 where the value is either a 'Yes' or a 'No' so if it is either one of these values it will redirect to the page i want. Code for my page is here: http://pastebin.com/1D6PrmBv

2
  • your're checking if the value is yes AND no. It can't be both yes and no. better still. use if($rows['food']) if you want to redirect only if the var is empty Commented Feb 7, 2012 at 2:36
  • @Jacob Don't forget to mark your chosen answer as accepted. Commented Feb 7, 2012 at 4:00

4 Answers 4

2

Try this.

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

or you can write it this way too.

include('connect.inc');
    if (in_array($rows['food'], array("yes","no"))) {
    header('location: mypagelocation.html');
    exit;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Not sure about the downvote; this seems like the best answer (except maybe the conditional syntax error present in the original question; fixed).
@cbuckley: It's OK. It's not only conditional issue. The line header(location: mypagelocation.html'); also incorrect. And the exit; statement should be inside the if condition scope.
@ Prasad Rajapaksha it depends on what user is connected. I have usernames and passwords with a 'food' field. so I only want to check if a particular user has 'Yes'/'No' in 'food field'. Is the right code for this? and if so where do i insert it: mysql_query("SELECT * FROM users WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "')");
Jacob. Please edit your question with exact requirement. It will easy to see then. Thanks.
@Prasad Rajapaksha please see revised post above. code for my page can be seen here. pastebin.com/1D6PrmBv
2
include('connect.inc');
if (isset($rows['food'])) {
    header(location: mypagelocation.html');
    exit;
}
else{
    // $rows['food'] is not set
}

Comments

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

Make sure there is no output before that.

4 Comments

I'm always wary of recommending or since many seem to assume it is synonymous with ||. Obviously your example works fine though.
Yeah, when there is no value assigning, this makes a more readable code for beginners.
@cbuckley it depends on what 'user' is connected. I have usernames and passwords with a 'food' field. where do i add: mysql_query("SELECT * FROM users WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "')");
Don't forget to die after redirecting.
0

Full Code:

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

3 Comments

awesome, before i like it. 1 more thing promise. i need code, if attendance1 has not got value yes/no it will move onto next field attendance2. if attendance1 field is compelte attendance2 will not be and vice versa. pastebin.com/eXCZA1s7
Would you like us to write you the whole app?
No kidding. I don't mind helping, but show some initiative. There is enough available from your questions and on SO to figure out the rest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.