I have a multi-part form where pressing either “Back” or “Next” will cause that part to be submitted and stored into the database (MySQL); only difference is if it goes to the next or previous form.
Take note that the subsequent parts (second part onwards) are child tables of the table of the first part. They have ‘ePARno’ as a foreign key.
The first part executes this snippet:
if( isset( $_SESSION[ 'eparno' ] ) ) { //if it's an edit, replace
$eparno = $_SESSION[ 'eparno' ];
$insert_query_0 = mysql_query ("
REPLACE INTO form
(ePARno, caseno, accomplishedby)
VALUES
('$eparno', '$caseno', '$accomplishedby')
");
}
else { //otherwise, it's a fresh insert
$insert_query_0 = mysql_query ("
INSERT INTO form
(caseno, accomplishedby)
VALUES
('$caseno', '$accomplishedby')
");
$eparno = mysql_insert_id();
}
My problem arises when I go press back on the second form, bringing me to the first form, and return to it by hitting next. What happens when I do that is the inserted row of the second form gets inserted (by virtue of pressing Back) but it disappears from the table when I press Next from the first form. (I view the database through phpmyadmin simultaneous with the pressing of back and next, step-by-step)
I suspect that this has something to do with the first form replacing the parent row. If it executes the REPLACE, I suspect that the child row(s) which is/are already there, such as that of the second form, get deleted.
I must also say that if I don’t go back, things go on smoothly. No rows disappearing.
Does my suspicion have any merit? If so, how do I remedy this? If not, what could be the problem?
REPLACEdoes aDELETEandINSERT. I would guess that theDELETEis cascading to the child tables.Try
INSERT...ON DUPLICATE KEYinstead.Is ‘caseno’ your primary key? If so, maybe it’s this: