Page 1 of 1

Redirect to newly added record

Posted: Mon Dec 23, 2013 1:25 pm
by KillGorack
Greets..

I have a page with an add form. This form is adding to a table with a unique KEY "ID". Once I add the record I want to go to this record. The code below is how I'm doing it now. Just wanted to know if there is a better way.

Code: Select all

<?php
      mysql_query("INSERT INTO ".$targetapp." (".trim($fieldnames, ",").") VALUES(".trim($fielddata, ",").")");
      $SQL = "SELECT ID FROM ".$targetapp." ORDER BY ID DESC LIMIT 1";
      $result = mysql_query($SQL);
      while($row = mysql_fetch_array($result)){
        $lastID = $row['ID'];
      }
      mysql_close($result);
      echo $lastID;
      ?><meta http-equiv="refresh" content="0;URL='start.php?id=<?php echo $lastID ?>&con=det&app=<?php echo $targetapp ?>'" /><?php
?>

Re: Redirect to newly added record

Posted: Mon Dec 23, 2013 5:41 pm
by Christopher

Code: Select all

header("Location: http://mysite.com/start.php?id=$lastID&con=det&app=$targetapp");

Re: Redirect to newly added record

Posted: Tue Dec 24, 2013 8:12 am
by KillGorack
Yep but I have headers on the page.. Not exactly what I need.. My question is there a better way to obtain an ID of the record that I just added.

Re: Redirect to newly added record

Posted: Tue Dec 24, 2013 8:28 am
by Celauran
You could use mysql_insert_id(), though you really shouldn't be using mysql_ functions at all. I'd also use a header redirect over meta refresh.