Jump to content

Correct method to validate MySQL Inserts?


hoopz

Recommended Posts

Hey everyone! Thanks in advance for your help!

 

I am writing a function that Inserts data into 2 tables within a database. The Pseudo code is as follows:

 

- insert into user_table ......

- get last id

- insert into shipping_info ......

 

I'm a self taught in PHP/MySQL so I don't know all the proper coding practices. My question is this: if either of those inserts fail, especially the 2nd one, I need to remove any data that has been inserted and then generate an error message for the user.

 

Here's what I'm thinking, please let me know if there is a better way to do this:

 

- insert into user_table .....

- if success then continue else generate error message and return

- get last id

- insert into shipping_info .....

- if success then continue else remove data from previous insert, generate error message, and return

 

Is this typically how it's done?

 

Thanks again,

- H

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.