Solved

Problem with ON DUPLICATE query

Posted on 2012-12-25
9
231 Views
Last Modified: 2012-12-30
Hi,

Something is wrong with this query, but don't know what :(
The query should update an entry if it already exists (country code) or insert it not.

$sql = "
INSERT INTO currency_exchange_rates
	(base, country, value, updated, updated_datetime) 
VALUES
	('{$this->base}', '$code','$item','{$exchangeRates->timestamp}','".date("Y-m-d H:i:s",$exchangeRates->timestamp)."')
ON DUPLICATE
	country
UPDATE
	value='$item', updated_datetime='{$exchangeRates->timestamp}', updated_datetime='".date("Y-m-d H:i:s",$exchangeRates->timestamp)."'
WHERE
	country='$code'
";

Open in new window

0
Comment
Question by:kgp43
  • 3
  • 3
  • 2
9 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38720435
Something is wrong with this query
What is the Something in that statement?  Please tell us a little more.  Is the column country an index?  Is there an error message?
0
 
LVL 3

Expert Comment

by:wadehults
ID: 38720444
Here is an article that may be useful to you:
Duplicate Key Update
0
 

Author Comment

by:kgp43
ID: 38720470
Ray_Paseur;

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'country UPDATE value='3.672971', updated_datetime='1356480009', updated_d' at line 6

country is an index.

Corrected an error, but still the same error message
$sql = "
INSERT INTO currency_exchange_rates
	(base, country, value, updated, updated_datetime) 
VALUES
	('{$this->base}', '$code','$item','{$exchangeRates->timestamp}','".date("Y-m-d H:i:s",$exchangeRates->timestamp)."')
ON DUPLICATE
	country
UPDATE
	value='$item', updated='{$exchangeRates->timestamp}', updated_datetime='".date("Y-m-d H:i:s",$exchangeRates->timestamp)."'
WHERE
	country='$code'
";

Open in new window


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'country UPDATE value='3.672971', updated='1356480009', updated_datetime='' at line 6

Open in new window

0
 

Author Comment

by:kgp43
ID: 38720479
The "Is the column country an index?" question kinda 'tricked' it for me, think I fixed the issue. This seems to work:

$sql = "
INSERT INTO currency_exchange_rates
	(base, country, value, updated, updated_datetime) 
VALUES
	('{$this->base}', '$code','$item','{$exchangeRates->timestamp}','".date("Y-m-d H:i:s",$exchangeRates->timestamp)."')
ON DUPLICATE KEY UPDATE
	value='$item', updated='{$exchangeRates->timestamp}', updated_datetime='".date("Y-m-d H:i:s",$exchangeRates->timestamp)."'
";

Open in new window


I guess "ON DUPLICATE country UPDATE value='$item', updated=....." is not valid.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38720498
Make a Google search for "on duplicate key update mysql php" and see if the answers win the day.  Best regards and merry Christmas, ~Ray
0
 
LVL 3

Expert Comment

by:wadehults
ID: 38720585
The only thing I have to add to this is that the ON DUPLICATE KEY UPDATE command line only works with Unique or Primary Key values.
0
 

Author Comment

by:kgp43
ID: 38720791
Thanks Wadehults, will make sure that is the case :)
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38721043
Man Page here: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
Important Note here:  In general, you should try to avoid using an ON DUPLICATE KEY UPDATE clause on tables with multiple unique indexes.

That tells me that you want country to be the index and the only index.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now