mysql - primary key

Posted on 2011-10-29
Last Modified: 2012-05-12
hello dear experts ,
i am making an application , but i have a small problem ..
i am making a web application which is connected to a mysql database .
once the web application is loaded i want to load or to have a primary key from the database before doing anything !
how could this be done while i am not making any insertion query . !
as if like it want to have vise versa ( the primary key before insertion )


if you can tell me .. how can i get the primary key back to the application after the insert query ?!
Question by:mokatell
    LVL 14

    Expert Comment


    If I understand correctly, you want to load a page and get the primary key value before doing a full insert?

    I use PHP and MySQL and the way I would do it is to do an insert when the page is first loaded which just sets the primary key and then use mysql_insert_id to grab the last inserted id.

    Hope this helps,


    Author Comment

    ok fine ,, i thought of this solution ..
    but what about the applicant didnt continue the process or just lets say closed the browser .. in this case i will be creating useless rows in the database , right ?!
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    now, why do you need such a "primary key" value before actually inserting anything?

    you might create the row with a status column of "draft", so if the application closes the browser etc, it remains as draft.
    you might delete those drafts after a while, or keep them for statistical information ...

    Author Comment

    ok i am gonna tell you why !
    i have two tables . table A and table B , but table B is related to table A
    i wanna insert data in table A and in table B at the same time .
    so in this case  i must get the primary key inorder to make the matching correctly , got me now ?
    LVL 142

    Accepted Solution

    sure... now the question is: do you want to insert into both tables in the same form or not?
    if it's in the same form:
     you do the insert, then use mysql_insert_id (php):
     and with that returned value, you perform the second insert.

    if it's not in the same form:
     you do the insert in the first form, fetch the id same as above, and then put that value into the session data, and in the second form use that session data.


    Author Closing Comment

    thanks a lot .. now everything is clear ....

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi ( had suggested a “sed” way, I actually shell …
    Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now