tags:

views:

62

answers:

5

Hey, I am trying to make an if statement that redirects them to a different page if true, simple right?

I am not sure why this is not working but I am using:

if ($_POST['accounttype']=='Paid User - £2 p/m'){
    $userid = strtolower($_SESSION['X2X2']);
    $getuser = mysql_query("SELECT * FROM XXXXXX WHERE X2X2 = '$userid'");
    $info = mysql_fetch_array($getuser);
    $id = $info['X3X3'];
    mysql_query("UPDATE members SET payment = '" . mysql_real_escape_string("XXXXXXXX"). "' WHERE X3X3 = $id");
    header('Location: http://beta.XXXXX.co.uk/purchase.php');
    mysql_close($con);
}

When I put

<?
echo $_POST['accounttype'];
?>

And I get back

Paid User - £2 p/m

Which is correct?

Any help would be appreciated, Thanks.

+3  A: 

Looks like you want to call exit() before the close brace on your if statement.

The documentation for header has example code like this:

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

The end bit of your if statement really ought to be:

mysql_query("UPDATE members SET payment = '" . mysql_real_escape_string("XXXXXXXX"). "' WHERE X3X3 = $id");
mysql_close($con); // do this before sending a redirect header
header('Location: http://beta.XXXXX.co.uk/purchase.php');
exit();

Also, header doesn't work if you've already sent any output, per this warning from the documentation for header:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Dominic Rodger
But the if statement is coming back as that it doesn't equal 'Paid User - £2 p/m'. I have tried putting the exit(); but still doesn't work.I have tried putting an echo in the if statement but that doesn't come up.
Crazyd22
Then Felix's comment on your question is probably relevant - what happens if you remove the `£` character? What does view source look like where the `£` is displayed - is it `£`, `£` or something else?
Dominic Rodger
I have tried adding <?echo $_POST['accounttype'];?>After it, but and it comes back with the correct answer
Crazyd22
+1  A: 

You haven't by any chance already output something to the browser have you? If you modify the location header after using the echo or print statements, it will issue a warning which you probably won't see unless you have verbose errors or logging turned on.

I know this can happen with UTF-8 files in some versions of PHP - the byte order mark (BOM) of the UTF-8 file are output before the PHP script starts execution, which prevents the location header from being sent.

Andy E
I have an if statement that would echo something if there was an error, but that doesn't echo anything unless there is. Not sure weather that would?
Crazyd22
If it is echoing something before your header is output, then it could be the cause of the issue. Are your files UTF-8 encoded?
Andy E
Yeah, but it isn't echoing anything before the statement. When I put an echo statement into the if statement it doesn't even echo that? Doesn't that mean that there is something wrong with the statement?
Crazyd22
Yeah, it's most likely a problem with your if statement. You should look at cballou's answer, the trim function might help.
Andy E
Yeah I have added that into my code, but still not working
Crazyd22
It's something to do with the £ sign, I deleted it and it works, any ideas how to get around this?
Crazyd22
A: 

Altering the HTTP header with header requires that the HTTP header has not been sent yet. This can be one reason for why it doesn’t work for you as the HTTP header is sent together with the first output of your script (any output including text before <?php).

When you set error_reporting to E_ALL and display_errors to true, PHP will display you all errors immediately. This can help you to determine the cause of you error.

Gumbo
How can I set these? 'error_reporting to E_ALL and display_errors to true'
Crazyd22
@Crazyd22: Just follow the links and read their description.
Gumbo
A: 

My first inclination would be to check if there are any extra characters on your POST data by trying the following:

if (trim($_POST['accounttype']) == 'Paid User - £2 p/m') {
cballou
Thanks, but still doesn't work
Crazyd22
+2  A: 
Felix Kling
Yes, this is perfect, Thanks!!
Crazyd22