HOW TO DEAL WITH DATABASE IN PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devd
Forum Commoner
Posts: 25
Joined: Tue Nov 18, 2014 9:14 am

HOW TO DEAL WITH DATABASE IN PHP

Post by devd »

hi,
I want to insert records to table .Before that need to check whether any records exists with the given id.

I am in a confusion on where to exactly write the code for this.

I have php page called sample.php, one another php page to write all db related functions and lastly one more php page for dbconnecton
1)sample.php
2)query.php.
3)dbconnect.php.

How to write code for the existing record in table and after that insertion .
How to write code correctly....
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HOW TO DEAL WITH DATABASE IN PHP

Post by Celauran »

Do a SELECT COUNT(*) against the ID in question. If the result is 1, you're dealing with an update. If it's 0, you're dealing with an insert. If it's 2, well, that's probably bad.
devd
Forum Commoner
Posts: 25
Joined: Tue Nov 18, 2014 9:14 am

Re: HOW TO DEAL WITH DATABASE IN PHP

Post by devd »

Yes...I know the code for checking duplicate entries...Can i write queries for both checking existing code and inserting code within one fution?

Also I wanted to insert multiple records...Means an array of records .I need to check for already existing record and after all insertion or cheching I want to display one message to user that which records already exists....is it possible?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HOW TO DEAL WITH DATABASE IN PHP

Post by Celauran »

devd wrote:Can i write queries for both checking existing code and inserting code within one fution?
Technically you could, but those should really be different functions since they do wildly different things.
devd wrote:Also I wanted to insert multiple records...Means an array of records .
Use prepared statements.
devd wrote:I need to check for already existing record and after all insertion or cheching I want to display one message to user that which records already exists....is it possible?
Sure. Check the existence of the items in your array, record the existing entries in their own array, insert the rest, and display a "some records could not be inserted" message to the user. As an example.
devd
Forum Commoner
Posts: 25
Joined: Tue Nov 18, 2014 9:14 am

Re: HOW TO DEAL WITH DATABASE IN PHP

Post by devd »

O.k thank you.its working
selvamuthu
Forum Newbie
Posts: 2
Joined: Thu Nov 27, 2014 9:03 am

Re: HOW TO DEAL WITH DATABASE IN PHP

Post by selvamuthu »

Refer
INSERT INTO TABLE (VALUES) WHERE ID NOT IN (SELECT ID FROM TABLE)
Post Reply