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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semanticnotion
    New Member
    • Sep 2010
    • 66

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

    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
    Code:
    $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')";
     
    mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    ,
    Code:
    $query = "select * FROM form1";
    $result = mysql_query($query) or mysql_error($link);
    
    $rs=mysql_fetch_array($result)
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    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:


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


    Dan

    Comment

    • semanticnotion
      New Member
      • Sep 2010
      • 66

      #3
      Thanks for your reply Dan but now my query is change to and it works fine

      Code:
      $ssn = $_POST['ssn'];
      
      $link = mysql_connect("localhost", "root", "root");
      
      mysql_select_db('wordpress1', $link) or die ("Error in query: $query. ".mysql_error());
      
      //$ssn1=mysql_insert_id($link);
      //$query = "select * FROM form1";
      
      $query="SELECT * FROM form1 where ssn=$ssn";
      $result = mysql_query($query) or mysql_error($query);
      
      $rs=mysql_fetch_array($result)

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        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:

        Code:
        SELECT * FROM form1 where ssn=1; DELETE FROM form1;
        I just deleted your entire database and that's not the worst thing I could do.

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

        Code:
        $clean_ssn = mysql_real_escape_string($_POST['dirty_ssn']);
        Hope you learned something new,


        Dan

        Comment

        • semanticnotion
          New Member
          • Sep 2010
          • 66

          #5
          Thanks for your precious tip Dan.

          Comment

          Working...