I want to insert if in the table is not same row with values, but if there is row with the same values I want to only update one that will add +1 to the current value. I have current code, but it doesn't seem to update values in row that exists.
INSERT INTO raport(id, wykonawca, tytul, czas_trwania, powtorzenia)
VALUES('','$wykonawca2','$tytul2','$czas_trwania2', '$powtorzenia2')
ON DUPLICATE KEY UPDATE wykonawca='$wykonawca2', tytul='$tytul2', czas_trwania='$czas_trwania2',
powtorzenia='$powtorzenia2'+1
Ensure your table has a column declared as UNIQUE or PRIMARY KEY and is not an Auto-increment column
If you specify an ON DUPLICATE KEY UPDATE clause and a row to be inserted would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row occurs. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have similar effect:
INSERT INTO t1 (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
UPDATE t1 SET c=c+1 WHERE a=1;
(The effects are not identical for an InnoDB table where a is an auto-increment column. With an auto-increment column, an INSERT statement increases the auto-increment value but UPDATE does not.)
MySQL Reference
EDIT :
Your id column is empty you need to pass a value
The actual answer to your initial question as to why the rows do not update is because you are not passing a value for the PRIMARY KEY - this is your AUTO-INCREMENT id column.
Every time you pass :
VALUES('','$wykonawca2','$tytul2','$czas_trwania2', '$powtorzenia2')
this means that your id column is blank, so there is no duplicate just a new row. If you want to have an ON DUPLICATE KEY UPDATE you'll need to pass in the id.
For some more info about ways to handle multiple indexes and DUPLICATE UPDATE check this question out: MySQL behavior of ON DUPLICATE KEY UPDATE for multiple UNIQUE fields
Also, you should read this and action it as soon as possible, you shouldn't be passing variables straight into sql - it's hugely outdated and very unsafe:
How can prepared statements protect from SQL injection attacks?
Related
I have a mysql on duplicate key statement.
mysql_query("INSERT INTO statistics (classify, apply) VALUES ('$classify', 1)
ON DUPLICATE KEY UPDATE apply = apply + 1");
id classify apply
1 A 1
but it didn't update the existing row and it keep add another row, Where is the problem?
It's probably the column classify is not unique. You need to have a UNIQUE field in the table to make ON DUPLICATE KEY UPDATE work. If you have not set one, you can execute this statement below.
ALTER TABLE statistics ADD CONSTRAINT tb_uq UNIQUE (classify)
ON DUPLICATE KEY will update a row only when you try to insert a record that would throw a duplicate keys error (like the name states). So this happens only if you are a using a unique key or a primary key for that column. It looks like you didn't created a unique key for the classify column.
I have a little problem.
This is the SQL code I get:
INSERT INTO matches (match_id, league_name, league_id, test_one)
VALUES (866860, 'Portugal',1,'testing')
ON DUPLICATE KEY
UPDATE league_name =
CASE match_id
WHEN 866860
THEN 'Portugal' END,
league_id =
CASE match_id
WHEN 866860
THEN 1 END,
test_one =
CASE match_id
WHEN 866860
THEN 'testing' END
The problem is, that it always insert a new row instead of updating the existing one.
Is it because I need to make the "match_id" as AUTO_INCREMENT or anything else (at the moment, my "id" field is AUTO_INCREMENT)
AUTO_INCREMENT is irrelevant here. But ON DUPLICATE KEY requires a key. More specifically, a unique key, such as primary key or a unique index.
Given the column names, I suspect that match_id fails to be a primary key.
Update: You also write a complicate set of CASE ... END constructs. Have a look at the VALUES() function.
You are telling your database to update information "ON DUPLICATE KEY"; if none of the other fields are UNIQUE keys, the database will insert a new row.
You can either SELECT the id before this query (or making it a sub-query) or add another key to this table that will result in a unique value for each row. I don't know about your schema, but you might be able to create a multi-part key which spans match_id and league_id.
If I run an insert query but it fails because of duplicated key error is there any way to get its primary key without doing another select?
Basically:
INSERT INTO tbl (field) VALUES ('myvalue')
This fails because there is already a record with ID:1 and field:myvalue.
Now I Want to know that ID:1 without doing another query:
SELECT id FROM tbl WHERE field = 'myvalue'
is it possibile?
Here is a link that provides four different ways to deal with this:
http://mikefenwick.com/blog/insert-into-database-or-return-id-of-duplicate-row-in-mysql/
There is a provision with each library to get the id of the row which is inserted.
Like: for simple mysql_query() use mysql_insert_id() after that to retrive the id of the last inserted row
Check this link
EDIT:
Please Check that if you have defined the unique index for field column.
If yes then you cannot insert duplicate in the column field
I have a mySQL query using ON DUPLICATE KEY UPDATE that I am runnining from a PHP script. I would like to test whether an UPDATE or an INSERT occurred. How can I do that?
From MySQL documentation:
"With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row and 2 if an existing row is updated."
You can use mysql_affected_rows() to determine the number of rows affected.
I had to figure this out one time, so I'm referring to my notes on the matter.
The following query:
INSERT INTO table ... ON DUPLICATE KEY UPDATE ..., id = LAST_INSERT_ID( id)
Where 'id' is the primary key on the table, allows you to call mysqli_affected_rows(), which will return:
0 - Row existed, nothing updated
1 - No row existed, inserted
2 - Row existed, something updated
I am building a rating system, and i want to insert a new row, if the name field does not already contain the name i want to insert, and if it does exist, i want to increase the count field by 1
For example, if i have a row the the name 'Tom' and i try to insert another row with the name 'Tom, then i want to +1 for the field count on the row that already exists.
If a row with the name 'Tom' does not exist, i want to insert a new one and set count to 1.
I know i could do this with about 3 SQL statements and some if statements, but that would slow down the script as 2/3 sql commands are being executed.
Any ideas?
Thanks!
see INSERT ... ON DUPLICATE KEY UPDATE
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed.
e.g.
INSERT INTO table (name,counter) VALUES ('Bob', 1)
ON DUPLICATE KEY UPDATE counter=counter+1