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
KillGorack
Forum Newbie
Posts: 4 Joined: Thu Dec 19, 2013 9:30 pm
Post
by KillGorack » Mon Dec 23, 2013 1:25 pm
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
?>
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Dec 23, 2013 5:41 pm
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
Post
by KillGorack » Tue Dec 24, 2013 8:12 am
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.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Tue Dec 24, 2013 8:28 am
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.