Jump to content

SESSION lost from same page when POST?


Raz3rt
Go to solution Solved by Psycho,

Recommended Posts

Well i have a session['rmanr'] a have set after a submit containing the date and unique id.

I have to be able to do multiple submits on that same form but if they submit multiple times the same rmanr have to be used...

 

I tried the following:

$date         = date("Y-m-d");
$datestrip    = str_replace("-",  "", $date);
$lastdetid     = $dbh->lastInsertId();
if(!isset($_SESSION['rmamr'])){
            $rmanr                   = $datestrip;
            $rmanr                  .= $lastdetid;
            $_SESSION['rmanr']       = $rmanr;
        } else {
            $rmanr                   = $_SESSION['rmanr'];
        }
    $uprmastmt = $dbh->prepare("UPDATE rma SET r_nr = '$rmanr' WHERE r_id = ?");
    $uprmastmt->bindParam(1, $lastid);
    $uprmastmt->execute();

But i keep getting a new rmanr every time i submit... Could it be that my form submit reset my SESSION?

Link to comment
Share on other sites

Are you starting the session on each page load? E.g. session_start().

 

But, aside from that, one thing in your code makes no sense. Why are you creating a date string in the format YYYY-mm-dd only to then strip out the dashes? WHy not create the date in that manner to begin with?

Link to comment
Share on other sites

Are you starting the session on each page load? E.g. session_start().

 

But, aside from that, one thing in your code makes no sense. Why are you creating a date string in the format YYYY-mm-dd only to then strip out the dashes? WHy not create the date in that manner to begin with?

 

 

Yes i started with session_start() and the reason why i did it that way in my date string is because i use $date to input with the "-" into my database and after that i replace the "-" with nothing to make the beginning of my id

Link to comment
Share on other sites

here you have all of the code :

session_start();
if (!isset($_SESSION['k_name']) && !isset($_SESSION['c_firstname'])){
    header('Location: index.php?pagina=login');
    
}
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    
        require('incl/functions.php');
        
        $date         = date("Y-m-d");
    $datestrip    = str_replace("-",  "", $date);
        
        
        
        if(!empty($_POST['discipline']) && !empty($_POST['article'])) {
    
    $article        = $_POST['article'];
    $serialnr       = $_POST['serialnumber'];
    $error          = $_POST['errorinput'];
    $discipline     = $_POST['discipline'];
    $issue          = $_POST['issue'];
    
    $new_rma_query = "INSERT INTO rma (r_datum, r_klantid, r_contactid) VALUES (:rdatum, :rklantid, :rcontactid)";
    $new_rma_stmt = $dbh->prepare($new_rma_query);
    $new_rma_stmt->execute(array(
            ':rdatum'=>$date,
            ':rklantid'=>$_SESSION['k_id'],
            ':rcontactid'=>$_SESSION['c_id']
        ));
        
        
    $lastid = $dbh->lastInsertId();
    
    $new_rma_det_query = "INSERT INTO rma_detail (rd_rma_id, rd_artikel_code, rd_rma_aard, rd_serienr, rd_foutomschr, rd_typerma) VALUES (:rmaid, :articlecode, :rmaaard, :serialnr, :error, :typerma)";
    $new_rma_det_stmt = $dbh->prepare($new_rma_det_query);
    $new_rma_det_stmt->execute(array(
            ':rmaid'=>$lastid,
            ':articlecode'=>$article,
            ':rmaaard'=>$discipline,
            ':serialnr'=>$serialnr,
            ':error'=>$error,
            ':typerma'=>$issue
        ));
    
    $lastdetid     = $dbh->lastInsertId();
    
        if(!isset($_SESSION['rmamr'])){
            $rmanr                   = $datestrip;
            $rmanr                  .= $lastdetid;
            $_SESSION['rmanr']       = $rmanr;
        } else {
            $rmanr                   = $_SESSION['rmanr'];
        }
    
    $up_rma_stmt = $dbh->prepare("UPDATE rma SET r_nr = '$rmanr' WHERE r_id = ?");
    $up_rma_stmt->bindParam(1, $lastid);
    $up_rma_stmt->execute();
        
    header('Location: index.php?pagina=rma-new');        
    
        }
    }

and under my form i made this to fill in a table that HAVE to contain the submitted rma's with the same number and that is why it is important to use the same number if the person stays on the same page

<?php if (!empty($_SESSION['rmanr'])){
    $get_rma_rows = $dbh->prepare("SELECT rma_detail.rd_artikel_code, rma_detail.rd_rma_aard, rma_detail.rd_serienr, rma_detail.rd_typerma FROM rma LEFT JOIN rma_detail ON rma_detail.rd_rma_id=rma.r_id WHERE r_nr = ?");
    $get_rma_rows->bindparam(1, $_SESSION['rmanr']);
    $get_rma_rows->execute();
    while($get_rma_rows_row = $get_rma_rows->fetch(PDO::FETCH_ASSOC)){ ?>
      <td><?php echo $get_rma_rows_row['rd_artikel_code']; ?></td>
      <td><?php echo $get_rma_rows_row['rd_rma_aard']; ?></td>
      <td><?php echo $get_rma_rows_row['rd_serienr']; ?></td>
      <td><?php echo $get_rma_rows_row['rd_typerma']; ?></td>
    <?php    }
             }?>
Edited by Raz3rt
Link to comment
Share on other sites

  • Solution

Check your spelling!

if(!isset($_SESSION['rmamr'])){
    $rmanr                   = $datestrip;
    $rmanr                  .= $lastdetid;
    $_SESSION['rmanr']       = $rmanr;
} else {
    $rmanr                   = $_SESSION['rmanr'];
}

The isset() check is using the index 'rmamr' whereas the index you use set is 'rmanr'. You might also consider giving that index a more descriptive value anyway.

Edited by Psycho
Link to comment
Share on other sites

Check your spelling!

if(!isset($_SESSION['rmamr'])){
    $rmanr                   = $datestrip;
    $rmanr                  .= $lastdetid;
    $_SESSION['rmanr']       = $rmanr;
} else {
    $rmanr                   = $_SESSION['rmanr'];
}

The isset() check is using the index 'rmamr' whereas the index you use set is 'rmanr'. You might also consider giving that index a more descriptive value anyway.

 

I'm sorry to have bothered you for such a stupid fault... It really was just that! Thank you for the help anyway and hope to come with better questions in the future!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.