Eiolon Posted December 23, 2009 Share Posted December 23, 2009 I am trying to make it so when a record is created the user will be redirected to that record once submitted. It is redirecting to the proper page but it is not pulling up the record. I'm not sure how to get the $id to pass through using the header('Location') redirect. if (empty($errors)) { $insert = "INSERT INTO companies (name, phone1, phone2, fax, website, customer, contract) VALUES ('$n','$p1','$p2','$f','$w','$cu','$co')"; $result = mysql_query($insert) OR die ('Could not add the company to the database.'); $id = mysql_result((mysql_query("SELECT * FROM companies WHERE id = '$id'")), 0, 'id'); if ($insert) { header('Location: company.php?id=".$id."'); exit; } } Link to comment https://forums.phpfreaks.com/topic/186169-last-insert-id-and-redirect/ Share on other sites More sharing options...
p2grace Posted December 23, 2009 Share Posted December 23, 2009 First, to get the last insert id, just execute the following: $id = mysql_insert_id(); That will return the id of the last insert statement performed. Then to redirect just do the following: header("Location: company.php?id=$id"); Link to comment https://forums.phpfreaks.com/topic/186169-last-insert-id-and-redirect/#findComment-983174 Share on other sites More sharing options...
Eiolon Posted December 23, 2009 Author Share Posted December 23, 2009 Thanks, but the value is still not being passed to company.php. In the address bar it is showing: http://localhost/contacts/company.php?id=$id Instead of: http://localhost/contacts/company.php?id=1 Link to comment https://forums.phpfreaks.com/topic/186169-last-insert-id-and-redirect/#findComment-983178 Share on other sites More sharing options...
p2grace Posted December 23, 2009 Share Posted December 23, 2009 If it's in double-quotes it'll pass the value of the variable, not the variable itself. If its in single quotes, it'll only pass the variable string. Make sure it's in double quotes. Link to comment https://forums.phpfreaks.com/topic/186169-last-insert-id-and-redirect/#findComment-983180 Share on other sites More sharing options...
Eiolon Posted December 23, 2009 Author Share Posted December 23, 2009 Oh, thanks very much Working! Link to comment https://forums.phpfreaks.com/topic/186169-last-insert-id-and-redirect/#findComment-983185 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.