Dysan Posted January 13, 2008 Share Posted January 13, 2008 Hi, I have the following code, that stores form data into a MySQL database. How do I provide a link on this page, that navigates to a page called "card.php", and displays the appropriate id for that person? <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die(mysql_error()); } mysql_select_db("db", $con); $sql="INSERT INTO users (id, name, account) VALUES ('$_POST[id]','$_POST[name]','$_POST[account]')"; if (!mysql_query($sql,$con)) { die(mysql_error()); } echo $_POST[name]."'s Account Created Successfully!"; mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/85803-adddisplay-database-data/ Share on other sites More sharing options...
tinker Posted January 13, 2008 Share Posted January 13, 2008 Get the (last) insert id using 'mysql_insert_id()' and use a $_GET on your link, e.g. 'index.html?id=555' Quote Link to comment https://forums.phpfreaks.com/topic/85803-adddisplay-database-data/#findComment-437913 Share on other sites More sharing options...
monkeytooth Posted January 13, 2008 Share Posted January 13, 2008 Note though you may know this... mysql_insert_id() will only work if your using a table structure that auto increments upon inserting. Which i didn't look at your code above so it might. Anyway that said mysql_insert_id() will from my experience at least only work for the query in use at the time of inserting.. if you go back at a later time to call for the card.php your result for mysql_insert_id() will either be 0 or false or null.. Quote Link to comment https://forums.phpfreaks.com/topic/85803-adddisplay-database-data/#findComment-437922 Share on other sites More sharing options...
monkeytooth Posted January 13, 2008 Share Posted January 13, 2008 worse comes to worse you could always after the insert part of your code go back and query for a row count on that table.. find the row count call the id for that last row inserted and make your link that way.. select * from users order by id desc limit 1; now i am tired but if i typed that right what it should do is ultimately call up the last row in the table.. but then again if this is a multi user form your doing this for with a high volume of users this wont work.. could potentially call up someone else's data if you have more then 1 person filing at once.. another thing you could try is after the insert query make it search for the account it inserted (assuming this is a user specific/unique thing per user) and have it snag the id that way to.. I dunno im just babbling now but hope this somehow helps.. Quote Link to comment https://forums.phpfreaks.com/topic/85803-adddisplay-database-data/#findComment-437925 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.