Redirect to newly added record

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KillGorack
Forum Newbie
Posts: 4
Joined: Thu Dec 19, 2013 9:30 pm

Redirect to newly added record

Post 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
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Redirect to newly added record

Post by Christopher »

Code: Select all

header("Location: http://mysite.com/start.php?id=$lastID&con=det&app=$targetapp");
(#10850)
KillGorack
Forum Newbie
Posts: 4
Joined: Thu Dec 19, 2013 9:30 pm

Re: Redirect to newly added record

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Redirect to newly added record

Post 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.
Post Reply