Jump to content

Archived

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

Rogue3

Database entry form

Recommended Posts

I made a form so that I can add entries into my database, and I need help tweaking the end result a bit. Everything works just fine, the only part I can't figure out is when I enter an item in the database and click my submit button, the page that comes up shows all entries in the database. I'd like it to only show the one I JUST put in (for verification purposes).

Here is my code:

[code]mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM mytable";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>JTA Database Entry</center></b><br><br>";

$i=0;
while ($i < $num) {

$series=mysql_result($result,$i,"series");
$type=mysql_result($result,$i,"type");
$mfg=mysql_result($result,$i,"mfg");
$year=mysql_result($result,$i,"year");
$name=mysql_result($result,$i,"name");
$link=mysql_result($result,$i,"link");[/code]

Share this post


Link to post
Share on other sites
I added LIMIT to my query. I now get 1 result, however it is the first result in my table. How do I make it show the one I just entered? Here is what I changed:

[code]$query="SELECT * FROM guides LIMIT 1";[/code]

Share this post


Link to post
Share on other sites
OK, after fiddling around I think I solved it. Since I have my auto-incriment set for the id I just added this along with the LIMIT 1:

ORDER BY id DESC

Share this post


Link to post
Share on other sites
Alernatively, you can pull the specific record using LAST_INSERT_ID().

Share this post


Link to post
Share on other sites
Hey, that's even better. I don't know how my id's would get out of order, but at least this way it is definately going to pull what I just entered.

Share this post


Link to post
Share on other sites
It has nothing to do with IDs being out of order... it's just that there's no need to bother if all you want is the most recently inserted record.

Share this post


Link to post
Share on other sites

×

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.