/    Sign up×
Community /Pin to ProfileBookmark

Problem with redirecting

Hi all,

I have searched on this and might be missing something. I have a script that echo’s some html if a condition is met. The html is used to submit and move a file, I want to then redirect the page once this has happened. The problem lies with setting header, usually the header is set then php closes and you have your static html. In this case mine is echo’d and putting a header to redirect anywhere gets me the usual errors on headers sent. Here is the code:

[code=php]
<?php
include_once(“../../private/supplierconfig.php”);
checkLoggedIn(“yes”);
error_reporting (E_ALL);
echo “Welcome $username, “;
//Retrieve details from database to start

$id = $_SESSION[‘username’];
$query = “select image1, username from suppliers where username=’$id'”;

//now we pass the query to the database
$result=mysql_query($query, $link) or die(“MySQL query $query failed. Error if any: “.mysql_error());

//get the first (and only) row from the result
$row = mysql_fetch_array($result, MYSQL_ASSOC);

$username=$row[‘username’];
$image1 = $row[‘image1’];

if($image1 !== “”)

{

print ‘You have an image already in this slot. Click the Delete button to clear it, then upload a new one:’;

$string = ‘<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01//EN “http://www.w3.org/TR/html4/strict.dtd”>
<html lang=”en”>
<form name=”form1″ method=”post” action=”deleteimage1.php” enctype=”multipart/form-data”>
<input type=”hidden” name=”delete”>
<p></td><td><input name=”submit” type=”submit” value=”submit”></p>
</table>
</form>’;

echo “$string”;
}
else
{
echo “no image loaded”;

}

if(isset( $submit ) )
{
// exit;
//move image1 to the delete folder.

rename (“files/”.$image1, “temp/”.$image1)
or die (“Could not delete”);

//set image1 back to NULL
$image1 = ‘NULL’;

$query = “UPDATE `suppliers`
SET `image1` = ‘$image1’ WHERE `username` = ‘”. mysql_real_escape_string($_SESSION[‘username’]). “‘
LIMIT 1″;
$result = mysql_query($query, $link) or die(‘Update failed: ‘ . mysql_error());
echo $query;
//print_r($query);
mysql_info($link) ;
if(mysql_affected_rows($link) == 0);
header(“Location: suppliers.php?”.session_name().”=”.session_id());
}

else {
echo “<br><br>”;

exit;
}

?>
[/code]

Do I still use a header? If so where does it go?

Thanks,

G

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@chazzyOct 21.2006 — you need to guarantee that there is mutual exclusiveness in your processing.

you have an if like this:
[code=php]
if($image1 !== "")

{
//a whole bunch of code
}[/code]


You don't have an else after this block. In your second block of code, you need to have an else/elseif to guarantee to the PHP processor that it is mutually exclusive.

[code=php]
if($image1 !== "")

{
//a whole bunch of code
}
else{
//the other block of code
}[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyOct 22.2006 — the header [B]header("Location: suppliers.php?".session_name()."=".session_id());[/B] needs to come before anything is printed out on the page, so it needs to go near the top.
Copy linkTweet thisAlerts:
@knowjOct 22.2006 — put this at the top of the php page

ob_start();

and this at the bottom

ob_end_flush();
Copy linkTweet thisAlerts:
@chazzyOct 22.2006 — put this at the top of the php page

ob_start();

and this at the bottom

ob_end_flush();[/QUOTE]


Why though? This solution is used to compensate for poorly designed applications.
×

Success!

Help @genista spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...