Log in

View Full Version : Redirecting page?



Moshambi
09-22-2008, 09:10 PM
ok I want to update some fields in my database then redirect the page.

I have tried using these methods:


http_redirect();



<?php session_start(); ?>

<?php
require("conn.php");

$user = $_SESSION['user'];

if(isset($_POST['btnEdit']))
{
$skills = $_POST['skills'];
$goals = $_POST['goals'];
$achievements = $_POST['achievements'];
$aboutme = $_POST['aboutme'];
$sql = "UPDATE profile SET skills = '$skills', goals = '$goals', achievements = '$achievements', aboutme = '$aboutme' WHERE username = '$user'";
$result = mysql_query($sql) or die ("ERROR UPDATING: " . mysql_error());

if($result)
{
echo "Profile Succesfully Updated!";
http_redirect("profile.php?" . $user);
}
}

?>


header();



<?php session_start(); ?>

<?php
require("conn.php");

$user = $_SESSION['user'];

if(isset($_POST['btnEdit']))
{
$skills = $_POST['skills'];
$goals = $_POST['goals'];
$achievements = $_POST['achievements'];
$aboutme = $_POST['aboutme'];
$sql = "UPDATE profile SET skills = '$skills', goals = '$goals', achievements = '$achievements', aboutme = '$aboutme' WHERE username = '$user'";
$result = mysql_query($sql) or die ("ERROR UPDATING: " . mysql_error());

if($result)
{
echo "Profile Succesfully Updated!";
header("Location: profile.php?" . $user);
}
}

?>


And finally ob_start(); ob_end_flush();



<?php session_start(); ?>

<?php
require("conn.php");

$user = $_SESSION['user'];

if(isset($_POST['btnEdit']))
{
$skills = $_POST['skills'];
$goals = $_POST['goals'];
$achievements = $_POST['achievements'];
$aboutme = $_POST['aboutme'];
$sql = "UPDATE profile SET skills = '$skills', goals = '$goals', achievements = '$achievements', aboutme = '$aboutme' WHERE username = '$user'";
$result = mysql_query($sql) or die ("ERROR UPDATING: " . mysql_error());

if($result)
{
echo "Profile Succesfully Updated!";
ob_start();
header("Location: profile.php?" . $user);
ob_end_flush();
}
}

?>


All three of these methods give me errors. I have no idea what to do now, beside just put all the code in one file.

schorhr
09-22-2008, 09:35 PM
A headerredirect only works if nothing else has been outputted;
Also there may be no html, no spaces and no linebreaks before the redirect.

If you want the message "Profile Succesfully Updated!" to appear, use javascript for redirection (and provide a link with "click here if you are not redirected within 5 seconds" or something like that"

Moshambi
09-22-2008, 10:14 PM
Thank you for the reply...how would I go about doing that? Would I use the <meta> tag for a time out or just use Javascripts setTimeout(); method?

schorhr
09-22-2008, 10:19 PM
Well, both kind of redirects can be disabled that's why I recomended including a link ... just in case ;-)

Usualy both will work, if you want to make sure put in both- one with 5 seconds, the other one with 12 (for example).

If you do not neccesarily need a message you can also use the headerredirect with php which would work in all cases. If you do need a message you could display it on the page that you redirect to, for example

redirectpage?bla=1&redirmessage=1

(quick example, you get the point :-) )