2

Below is my mySQL insert:

$sql="INSERT INTO gj (name, phone, city, zipcode, description, dateadded, website, address1, other2, payment_options, Products, email,cat1,cat2,cat3)
    VALUES
    ('$companyname','$phone','$city','$zipcode','$description',curdate(),'$website','$address','$other','$payment','$products','$email','$select1','$select2','$select3')";

    if (!mysql_query($sql,$link))
      {
      die('Error: ' . mysql_error());
      }
    echo "<br/><h2><font color='green' style='font-size:15px;float:right'>1 business added</font></h2>";

    mysql_close($link);
}

    ?>

Now, instead of displaying "1 business added" in green, I'd like to redirect back to the page I was on before. Thanks.

0

3 Answers 3

4

If your script is always intended to go to the same place afterwards, then you can hard-code the location. For example:

header("Location: http://www.example.com/");

Note that per RFC 2616 (the HTTP/1.1 specification), the value of the Location header must be an absolute URI, not just a path.

If your script is for a specific purpose and always redirects to a certain section, but may vary which particular records are shown, then you can incorporate the data that was posted to your script into the URL. For example:

header("Location: http://www.example.com/widgets/" . urlencode($_POST['id']));

If your script is used in several different ways, then you can pass along the URL to redirect to from the page that posts to it in a hidden form field, for example:

<input type="hidden" name="redirect-url" value="http://www.example.com/">

More generally, you should follow the Post/Redirect/Get pattern.

Sign up to request clarification or add additional context in comments.

Comments

2

Add:

header("Location: page_you_want.php");

after your query has completed but before anything is echo'ed. You can only use the above code if headers have not already been sent including HTML etc.

1 Comment

Here is my code but I'm still getting an error $sql="INSERT INTO gj (name, phone, city, zipcode, description, dateadded, website, address1, other2, payment_options, Products, email,cat1,cat2,cat3) VALUES ('$companyname','$phone','$city','$zipcode','$description',curdate(),'$website','$address','$other','$payment','$products','$email','$select1','$select2','$select3')"; if (!mysql_query($sql,$link)) { die('Error: ' . mysql_error()); } mysql_close($link); header('Location: ' . $_SERVER['HTTP_REFERER']); }
0

I would do:

header('Location: ' . $_SERVER['HTTP_REFERER']);

after

mysql_close($link);

5 Comments

I get this error Warning: Cannot modify header information - headers already sent by (output started at /Users/Sites/gj/doctype.php:5) in /Users/Sites/gj/addbusiness.php on line 98....and line 98 is is where I put the header('location....stuff you just gave me?? Is there any thing I might be doing wrong?
location can be set to any web address. be sure not to output anything to the browser before setting a header or you will get an error (this includes blank lines not encased in <?php ?>).
yeah, that error means that you outputted something to the browser before you set the header. don't do that.
Don't do this, the Referer header is not reliable.
you need to get rid of the echo statement or the header wont work.

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.