LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 07-03-2010, 03:32 AM   #1
xsyntax
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
email@domain.com | 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.

Thanks,

syntax )
 
Old 07-03-2010, 03:37 AM   #2
zirias
Member
 
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, 04:14 AM   #3
Mark1986
Member
 
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 04:15 AM.
 
  


Reply

Tags
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 08:19 AM
php/mysql data INSERT IGNORE problem frieza Programming 2 09-03-2008 04:42 PM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 01:08 PM
php mysql insert null value spoody_goon Programming 2 03-28-2004 11:20 AM
I need to insert and post PHP MYSQL DropHit Programming 2 02-19-2004 12:21 AM


All times are GMT -5. The time now is 09:01 AM.

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