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....
HOW TO DEAL WITH DATABASE IN PHP
Moderator: General Moderators
Re: HOW TO DEAL WITH DATABASE IN PHP
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
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?
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
Technically you could, but those should really be different functions since they do wildly different things.devd wrote:Can i write queries for both checking existing code and inserting code within one fution?
Use prepared statements.devd wrote:Also I wanted to insert multiple records...Means an array of records .
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 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?
Re: HOW TO DEAL WITH DATABASE IN PHP
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
Refer
INSERT INTO TABLE (VALUES) WHERE ID NOT IN (SELECT ID FROM TABLE)
INSERT INTO TABLE (VALUES) WHERE ID NOT IN (SELECT ID FROM TABLE)