Jump to content

[SOLVED] using header location to display submitted data


retro

Recommended Posts

I have a page with a form for entering or editing customer details.  This is controlled by using $_GET['id'] and therefore determining whether to create a blank record or edit an existion one (calling the `id` field from the database).

 

This form actions save.php, the code of which is as follows:

 

<?php
header("Location: ./");
?>

<?
//Connect to DB & check for errors
include("dbinfo.inc.php");
$connection = mysql_connect($server,$username,$password);
if (!$connection)
{
 echo mysql_errno().": ".mysql_error()."<br/>";
 exit;
}
if(!mysql_select_db($database))
{
 echo("Database not found<br>");
}

//retrieve post data
$id = $_POST["id"];
$FirstName = $_POST["firstname"];
$MiddleName = $_POST["middlename"];
$LastName = $_POST["lastname"];
$Town = $_POST["town"];
$Street = $_POST["street"];
$PostCode = $_POST["postcode"];
$houseno = $_POST["houseno"];
$MobilePhone = $_POST["mobilephone"];
$HomePhone = $_POST["homephone"];
$OfficePhone = $_POST["officephone"];
$Email = $_POST["email"];
$Homepage = $_POST["homepage"];

if ($id != '') 
{
 //Edit current record
 $query = "UPDATE customers SET firstname='" . $FirstName . "',middlename='" . $MiddleName . "',lastname='" . $LastName . "',
 town='" . $Town . "',street='" . $Street . "',postcode='" . $PostCode . "',houseno='" . $houseno . "',
 mobilehone='" . $MobilePhone . "',homephone='" . $HomePhone . "',officephone='" . $OfficePhone . "',
 email='" . $Email . "',homepage='" . $Homepage . "' WHERE id=" . $id;
}
else 
{
 //Add new record
 $query = "INSERT INTO customers(firstname,middlename,lastname,town,street,postcode,houseno,mobilephone,homephone,officephone,email,homepage)
 VALUES
 (
 '" . $FirstName . "',
 '" . $MiddleName . "',
 '" . $LastName . "',
 '" . $Town . "',
 '" . $Street . "',
 '" . $PostCode . "',
 '" . $houseno . "',
 '" . $MobilePhone . "',
 '" . $HomePhone . "',
 '" . $OfficePhone . "',
 '" . $Email . "',
 '" . $Homepage . "'
 )";
}
//Update DB
$result = mysql_query ($query) or die("SQL Error: " . mysql_error());
mysql_close();
?>

 

What I want to do is open up the record once it has been saved.  To do this, I need to open the page:

 

contact.php?id=x

 

Where x is the id number.

 

As the id number has not been generated yet (it is an auto increment field in the database), I assume I would have to put

 

$insert = mysql_insert_id();

 

after the $result at the end.  Indeed, when I tried echoing this with the header disabled, it gave me the correct id.

 

However, I cannot get it to redirect to the correct page.  I tried using both of the following:

 

header("Location: ./contact.php?id=" . $insert . "");
header('Location: ./contact.php?id='.$insert);

 

And both times, it redirected to contact.php?id=

 

(i.e. it didn't fill in the id number).

 

Is this because $insert is not defined until the end of the document?

 

I tried moving the header code to the end of the document, but it did not like this, giving the following error:

 

Warning: Cannot modify header information - headers already sent by (output started at /*******/dbinfo.inc.php:6) in /*******/save.php on line 66

 

Can anyone suggest how I could get this working as intended?

 

Thanks in advance for any advice offered!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.