ON DUPLICATE KEY MYSQL Not Work - mysql

I have read for dozens thread but I dunno I could not figure it. I hope this thread won't duplicate.
So, I tried to insert data but in the end if the data has been on the table. It would update the value if it is not, then the data would be inserted into the table. I have read that I have to use
INSERT INTO table() VALUES() ON DUPLICATE KEY data1='$data1', data2='$data2'
Then, here is my code
$sql = "INSERT INTO niche_new(gamename,domain,url,type,date) VALUES('$gamename','$websiteurl','$url','download','$waktu') ON DUPLICATE KEY UPDATE domain='$websiteurl',url='$url'";
$process = $conn->query($sql) or die("Error: ".$conn->error);
In the end of the query.
The data won't be updated, but it is inserted. It makes my table has
lots of duplicate data.
I really appreciate any answers. Thank you :)
Note: It was a great mistake. I forgot to set unique key on my table.
To whom who want to use ON DUPLICATE KEY, make sure you set UNIQUE KEY
by picking which ROW as your parameter. It works now.

If it is inserting duplicate data, most likely, none of the columns you are inserting have the UNIQUEor PRIMARY KEY modifier.

Please specify the KEY column and it should work

Related

Multiple insert in mysql table, some of which may be repeated

I'm developing a php script in which I insert multiple data into a mysql table, but some of this data may already be inserted. I can try to insert each of the individual data and detect the #1062 error (duplicate entry), but it would be very inefficient since it can be more than 100 entries. So, is there any way to do this in one query or must I use a query for each entry to be inserted?
Thanks a lot.
You'll want to use the INSERT ... ON DUPLICATE KEY UPDATE command to accomplish this. In the "ON DUPLICATE KEY UPDATE" section, you can just do some kind of update that changes nothing. For example, "SET fieldName=fieldName", just so nothing is actually changed.
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

INSERT Batch, and if duplicate key Update in codeigniter

Is there any way of performing in batch Insert query and if the key already exists, UPDATE that row in codeigniter?
I have gone through the documentation and found only insert_batch and update_batch. But how to update the row with duplicate key in active records? And what happens if one row fails to be inserted or updated in batch_insert? All insertion fails or only that row?
You will have to go with little custom query by adding "ON DUPLICATE" statement
$sql = $this->db->insert_string('YourTable', $data) . ' ON DUPLICATE KEY UPDATE duplicate=duplicate+1';
$this->db->query($sql);
$id = $this->db->insert_id();
Also please check this out, it will give you better solution
For Codeigniter 3, you can use this library. This allows you to be able to supply an array of key-value pairs to be inserted into separate rows and will auto-update data if a duplicate key occurs in DB.
https://github.com/amir-ranglerz/CodeIgniter-Insert-Batch-on-DUPLICATE-KEY-Update

Check for the respective data in a specific column and if not detected, then insert. Otherwise update

I need to insert new row into table foo. But before insert those data, I need to check there was already inserted a row for the respective user name. If there has been already inserted, then I need to update the current data with the new data.
I know to do this using PHP if condition. But I love to do this using MySQL functions/statements by just a one line. Please can anyone help me?
For the example, kindly use the following statement. It should be updated.
$in = "insert into foo(username, text) values('user-x', 'user-x-text')";
Mysql_query($in);
When searching for similar questions, I got this post: Similar question with an answer. But I was struggle to use that solution since I don't know, the process occur by that code snippet will get down the server resources like speed etc. Because this script will run about 20 times per user.
Thank you.
I think INSERT ... ON DUPLICATE KEY UPDATE should be able to work
Make username a UNIQUE index, it doesn't have to be a primary key
If I'm not mistaken, DUPLICATE KEY will run only when you have a collision in any of the columns you supply that is either a primary key or unique index. In your case, text column is neither so it will be ignored for collisions.
INSERT... ON DUPLICATE KEY UPDATE works on unique indexes as confirmed by the MySql docs

mysql with duplicate key

I tried searching online for this...I had a row that I inserted into the database. I removed it. The table has a Unique Key on a particular column. When I try to insert a new row with the same value for the unique key, it fails saying duplicate entry. However, there is no duplicate entry since the row is not there! Is there a way to reset this?
I would like the table to accept values that are unique to what is there right now. I tried to remove the unique key constraint from the table to see if that would work, however, when I added it back, it was having the same issue.
Maybe you perform dirty reading? and the deletion did not commit? try use the read commit option, I think it's called isolation level.

Is it possible to declare to mysql queries?

I'm trying to create a code for a single button where it will perform either of two actions where it will add to the database if the user currently don't have the record while it will update the user's record if the user has records already. I've done it like this:
if() {
mysql_query("INSERT INTO table...");
}
else {
mysql_query("UPDATE table SET...");
}
Is it possible?
Yes, what you've written will work. If you have a way to know if there already exists a row or not without making an additional query just for this bit of code, then do exactly as you wrote.
If, however, you planned to first SELECT from the table to see if a row exists, then conditionally INSERT or UPDATE, you will perform more queries than necessary.
It would be better to either:
Have a PRIMARY KEY or other constraint on the table prevent duplicate INSERTs. Then issue an INSERT ... ON DUPLICATE KEY UPDATE query. This will attempt to INSERT the row, and if it is a duplicate, automatically perform the specified UPDATE to that row instead.
Issue the UPDATE query and check mysql_affected_rows to see if it updated an existing row. If not, then issue the INSERT query to create the new row.
Which one is more appropriate depends on your application.
you can use INSERT ... ON DUPLICATE KEY UPDATE Syntax like:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
If you have properly set unique keys, you should use REPLACE so you could remove the if.
REPLACE INTO table VALUE (...);
Pay attention that this is a MySQL extension, thus not portable to other DBs.
Yes, you could try the insert then if it fails try the update.
But you could use the MYSQL sql "REPLACE" keyword, which will insert a new record if it doesn't exist or delete the existing record and insert your new one if it does.
You could also use the INSERT ... ON DUPLICATE KEY UPDATE syntax
(explained here - Link to MYSQL ref which seems to be the closest fit to your requirement.
yes it is possible
first write a query for check that record is already exist or not.
Yes it is possible , it will work