473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How can i retrieve the last inserted record Using insert and select query

semanticnotion
Hi guys how can i retrieve the last inserted record from datebase. my primary key is not auto_increment its type is bigint because i want to insert SSN no as a primary key.
when i retrive the data using select query it retrieve the first record store in database but i want to display the last inserted record because i want then to print that record.
so any idea plz help me

my queries are
Expand|Select|Wrap|Line Numbers
  1. $query="INSERT INTO form1 VALUES('$plan','$subsidiaries', '$ssn','$dob','$initial','$App_lName','$FName','$Middle_Initial','$spouse_LName','$spouse_FName','$Spouse_Middle_Initial','$app_num','$Mailing_address','$city','$state','$zip','$cell','$business_phone','$ext','$home_phone','$email', '$today_date')";
  2.  
  3. mysql_query($query) or die ("Error in query: $query. ".mysql_error());
  4.  
,
Expand|Select|Wrap|Line Numbers
  1. $query = "select * FROM form1";
  2. $result = mysql_query($query) or mysql_error($link);
  3.  
  4. $rs=mysql_fetch_array($result)
  5.  
Sep 30 '10 #1
4 2487
dlite922
1,584 Expert 1GB
ssn is your ID?
You inserted it just recently?

Why can't you use that to get it back?

You should have an integer auto-increment ID. SSN should not be Primary key just because it's unique.

Here's a discussion about that:
http://people.planetpostgresql.org/d...ey-Wisely.html

In my opinion, there's no good substitute for last_insert_id()


Dan
Sep 30 '10 #2
Thanks for your reply Dan but now my query is change to and it works fine

Expand|Select|Wrap|Line Numbers
  1.  
  2. $ssn = $_POST['ssn'];
  3.  
  4. $link = mysql_connect("localhost", "root", "root");
  5.  
  6. mysql_select_db('wordpress1', $link) or die ("Error in query: $query. ".mysql_error());
  7.  
  8. //$ssn1=mysql_insert_id($link);
  9. //$query = "select * FROM form1";
  10.  
  11. $query="SELECT * FROM form1 where ssn=$ssn";
  12. $result = mysql_query($query) or mysql_error($query);
  13.  
  14. $rs=mysql_fetch_array($result)
  15.  
Oct 1 '10 #3
dlite922
1,584 Expert 1GB
Do you know what SQL Injection attack is?

What if I posted a SSN that had a value of "1; DELETE FROM form1";

You query would look like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * FROM form1 where ssn=1; DELETE FROM form1;
  3.  
  4.  
I just deleted your entire database and that's not the worst thing I could do.

use mysql_real_escape_string() on any variable before putting them into a query, like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $clean_ssn = mysql_real_escape_string($_POST['dirty_ssn']); 
  3.  
  4.  
Hope you learned something new,


Dan
Oct 1 '10 #4
Thanks for your precious tip Dan.
Oct 4 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Richard N. | last post by:
Hi, I'm using mySQL and wanted to get the ID of the record that I have just inserted in to the table (sentmessages). There is a function in mySQL that appears to be able to perform this task but...
1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
3
by: lostboy | last post by:
Hi All, Using classic ASP applciation with DB2 (just adding it to the apps quiver of DBs). I am trying to do an insert and then retrieve the auto-increment field's value using the standard ADO...
2
by: sheenaa | last post by:
Hi, I want to display the last recorded record in the database of SQL SERVER 2005. How can i display it with the select query in the grid view... The database contains the...
2
by: cherylwalsh35 | last post by:
I am working in access - using a select Query. I am including a memo field. The table has all of the data in the memo field displayed - however when I run a select query and include the memo...
13
rajiv07
by: rajiv07 | last post by:
Hi to all, I want to know how to select a last inserted record which is the primary key is not an integer. my table ramstr(Primary)----name--service XTC01-------Rajiv---service ...
0
by: Stardust | last post by:
Hello everyone! I am new to VB.NET so please accept my apologies if the question is too easy. I created a concise code to explain the problem I have. I have an Access database that contains...
1
by: atishrg | last post by:
Hello All, I am facing one strange problem.. I am using a select query in my function which will return specified product details according to product code selected by user.. here is the...
4
by: scottie | last post by:
Hi Everyone. First time here and what a site, my hat off to all the boffins here. I have looked though the forum and have found many interesting threads and could spend hours just crusing though the...
5
by: aniketk | last post by:
How can i get last inserted record. if i don't have any date column in table?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.