This topic is locked

redirect to edit page from add page

11/6/2006 10:00:41 AM
PHPRunner General questions
T
thesofa author

I have the users adding a new record, it then needs to be saved and passed to the edit page.

The reason is that there can be a lot of miss-hits with records being entered with duplicate dates for the event to happen.

To avoid this, I have the edit page set to show all future events for the pupil whose record we are adding.

So the add page puts the record in with a date well into the future, then I need the record that has just been saved to be opened in the edit page.

Suggestions please as to how I should do this.

The problem that is hitting me is the primary key field for this table is an autonumber field, so the value is only added when the record is saved.

All help much appreciated.

D
Dale 11/6/2006

Hi thesofa,
You hit it right on the head when you say the primary key is not yet filled when adding the record.
The problem is multi users adding records at the same time. If you rely on the last record added, you may not get the one you want back to edit.
A solution would be to add another field on the record that on adding, you could populate with a datetimestamp plus maybe a user id. I would try and concat the two values, stripping the dashes or separators from the date and then appending the user id to it.
Then you would have a solid reference to look for when you are getting the record back to edit.
I once used an application, that on creating a record, it would increment the primary key and then reserve it before sending the response back to the user. If two people added a record they would each have their own incremented id. Would be a nice feature to add to phprunner.
DaleM

T
thesofa author 11/6/2006

I have tried this

function AfterAdd()

{
//********** Custom code ************

// put your custom code here

global $conn;

//get current record

$where="select * from detentions where Lunchtime='N'";

$rs1 = db_query($where,$conn);

$data1 = db_fetch_array($rs1);
$det_id = $data1["ID"];

echo $det_id;
//********** Redirect to another page ************

header("Location: detentions_edit.php?editid=".$data1["ID"]);

exit();

}



having set a field called Lunchtime in the record to a value of 'N'.

the detention ID, stored in $det_id and echoed to the screen is shown, the record is saved from the previous events. I have tried both $det_id and $data1["ID"]. I have now removed all output to the screen from the echo command as alexy states that it prevents the redirection

BUT I get this message

PHP error happened

Technical information

Error type 2

Error description Cannot modify header information - headers already sent by (output started at D:\htdocs\tesool\include\detentions_events.php:80)

URL intranet/tesool/detentions_add.php?

Error file D:\htdocs\tesool\include\detentions_events.php

Error line 83

SQL query insert into `detentions` (`DateGiven`, `TutorGroup`, `detainee`, `DayForDetention`, `Origin`, `reason`, `ID`, `Lunchtime`, `MoreInfo`, `Comment`, `sess`, `department`, `donor`) values ('2006-11-06 00:00:00', '7J', 378, '2222-12-31', 183, 62, NULL, 'N', 'Boisterous Behaviour', '', 3, 31, 192)



I have searched here for this error and found various javascript work arounds, what I cannot work out is how to pass the value of the ID field, stored in $det_id, to the javascript so it will call the page properly.

Can this be done or am I banging my head against a brick wall?
More Detail

i have tried hardcoding the ID of the record into the redirection command and it works just fine, so I have proved it will do the job in principle.

Admin 11/7/2006

Hi,
just remove this line from your code.

echo $det_id;


Please note that we don't support the code written by our customers.

Next time you need to debug your code by your own.

T
thesofa author 11/7/2006

Hi,

just remove this line from your code.
Please note that we don't support the code written by our customers.

Next time you need to debug your code by your own.



Thanks for the help, I had actually altered the post to say that I had removed that line of code and it still threw a wobbler.

However, further digging in google led me to this as an answer

function AfterAdd()

{
//********** Custom code ************

// put your custom code here

global $conn;

//get current record

$where="select * from detentions where depreset=4";

$rs1 = db_query($where,$conn);

$data1 = db_fetch_array($rs1);

$det_id = $data1["ID"];
//********** Redirect to another page ************
?>

<script language="javascript">

window.location = "detentions_edit.php?editid=<?php echo($det_id);?>";

</script>

<?php

exit();

}



This works and picks up the last entry with no chance of duplicates, because I reset 'depreset' in the edit page.

By the way, Alexy, I realise that you do not support custom code, but there has been a lot of help offered from others, not just you and Jane.

Many thanks for your help.