Page 1 of 1
HOW TO DEAL WITH DATABASE IN PHP
Posted: Fri Nov 21, 2014 7:57 am
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....
Re: HOW TO DEAL WITH DATABASE IN PHP
Posted: Fri Nov 21, 2014 8:40 am
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.
Re: HOW TO DEAL WITH DATABASE IN PHP
Posted: Fri Nov 21, 2014 9:14 am
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?
Re: HOW TO DEAL WITH DATABASE IN PHP
Posted: Fri Nov 21, 2014 9:58 am
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.
Re: HOW TO DEAL WITH DATABASE IN PHP
Posted: Sun Nov 23, 2014 1:25 pm
by devd
O.k thank you.its working
Re: HOW TO DEAL WITH DATABASE IN PHP
Posted: Thu Nov 27, 2014 9:20 am
by selvamuthu
Refer
INSERT INTO TABLE (VALUES) WHERE ID NOT IN (SELECT ID FROM TABLE)