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 emptySoWhat– SoWhat2012-02-07 02:36:21 +00:00Commented Feb 7, 2012 at 2:36
-
@Jacob Don't forget to mark your chosen answer as accepted.Grexis– Grexis2012-02-07 04:00:28 +00:00Commented Feb 7, 2012 at 4:00
Add a comment
|
4 Answers
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;
}
5 Comments
cmbuckley
Not sure about the downvote; this seems like the best answer (except maybe the conditional syntax error present in the original question; fixed).
Prasad Rajapaksha
@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.
Jacob
@ 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'])) . "')");
Prasad Rajapaksha
Jacob. Please edit your question with exact requirement. It will easy to see then. Thanks.
Jacob
@Prasad Rajapaksha please see revised post above. code for my page can be seen here. pastebin.com/1D6PrmBv
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
cmbuckley
I'm always wary of recommending
or since many seem to assume it is synonymous with ||. Obviously your example works fine though.Shomz
Yeah, when there is no value assigning, this makes a more readable code for beginners.
Jacob
@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'])) . "')");
deceze
Don't forget to
die after redirecting.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
Jacob
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
Shomz
Would you like us to write you the whole app?
Grexis
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.