Jump to content


Photo

Database entry form


  • Please log in to reply
6 replies to this topic

#1 Rogue3

Rogue3
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 06 July 2006 - 12:07 PM

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:

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");



#2 hussain

hussain
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 06 July 2006 - 12:12 PM

give limit in query

#3 Rogue3

Rogue3
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 06 July 2006 - 12:50 PM

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:

$query="SELECT * FROM guides LIMIT 1";


#4 Rogue3

Rogue3
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 06 July 2006 - 01:00 PM

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

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 06 July 2006 - 04:19 PM

Alernatively, you can pull the specific record using LAST_INSERT_ID().
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#6 Rogue3

Rogue3
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 06 July 2006 - 04:28 PM

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.

#7 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 06 July 2006 - 05:00 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users