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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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