• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

mysql - primary key

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 )

or

if you can tell me .. how can i get the primary key back to the application after the insert query ?!
0
mokatell
Asked:
mokatell
  • 3
  • 2
1 Solution
 
john-formbyCommented:
Hi,

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,

John
0
 
mokatellAuthor Commented:
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 ?!
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
indeed.
now, why do you need such a "primary key" value before actually inserting anything?

anyhow:
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 ...
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
mokatellAuthor Commented:
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 ?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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):
 http://php.net/manual/en/function.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.

clear?
0
 
mokatellAuthor Commented:
thanks a lot .. now everything is clear ....
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now