Jump to content

Add/Display Database Data


Dysan

Recommended Posts

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)
?>

Link to comment
https://forums.phpfreaks.com/topic/85803-adddisplay-database-data/
Share on other sites

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.