Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-03-2010, 02:32 AM   #1
LQ Newbie
Registered: Dec 2009
Posts: 7

Rep: Reputation: 0
PHP/MySQL | insert on duplicate key run php function??

I am fairly new to mysql, so any bright ideas are happily welcomed.

I have a database with customer information from multiple sources. (email, name, address, phone, etc). The users email is the primary id. If a user already exists in the database, and they sign up from a new source, I want to run a function to evaluate which data i want to save, based on which entry is most recent, and which information is most complete.

email | fname | lname | home_phone | mobile_phone | source | bob | smith | 1234567890 | | source1

Using the above example, if bob signed up again from a new source, only this time bob gave a mobile_phone, and NO home_phone I would want to keep the existing data with the home phone and ONLY add the mobile phone.

With INSERT ON DUPLICATE KEY UPDATE i have only been able to update the information with the new entry.

I would like to be able to possibly exit the sql query and run a php function to evaluate the data, and THEN do a sql insert on duplicate key update.

I should be able to write the php function, i just don't know how to exit a sql query and call a php function.

My apologies if I rambled, and Thank you very much if anyone has any ideas.


syntax )
Old 07-03-2010, 02:37 AM   #2
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
This is AFAIK not possible (and even if it was, it would be bad design for mixing business logic into the data persistence layer).

Just try to fetch the existing user first using a standard SELECT query, if not found, do an INSERT, if found, update the fields in PHP and do an UPDATE on the database.
Old 09-10-2010, 03:14 AM   #3
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Rep: Reputation: 11
From my point of view, here's what I would do:

Put the existing row in an array using the email in the where clause to get the right row.
Put the new row you just received from Bob entering his details also in an array.
Compare arrays based on NULL values in either array, if one has a NULL value use the other, if both exist use the new row.
Use the array to make an update SQL statement.

If it's not clear, please let me know. I'll try and contact you or even provide some code (though I am not that good in PHP).

Edit: I just found this post via a friend. It might be a late reply, but none the less is a reply. Sorry, if this got to you too late.

Last edited by Mark1986; 09-10-2010 at 03:15 AM.


mysql, php

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP with MySQL INSERT problem althage Programming 6 03-22-2009 07:19 AM
php/mysql data INSERT IGNORE problem frieza Programming 2 09-03-2008 03:42 PM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM
php mysql insert null value spoody_goon Programming 2 03-28-2004 10:20 AM
I need to insert and post PHP MYSQL DropHit Programming 2 02-18-2004 11:21 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:47 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration