updating 'image-path' column in database with latest updated image by user - mysql

I store path of profile picture of my users in database and images are saved with the id of user as a suffix.Now as the same user again uploads its image,I want the path to be updated.I have three columns in the image table.
1.username
2.path
3.date
I tried to use REPLACE instead of INSERT like this
$query="REPLACE INTO img_upload(username,path,created) values('".$session_user['college_id']."','".$target."','".date('Y-m-d H:i:s',time())."')";
but it didn't worked.
username is primary key.

I can't really tell from your question if you want INSERT INTO ON DUPLICATE KEY UPDATE or or an UPDATE statement. This will insert a row if the key is not a duplicate and UPDATE it otherwise.
INSERT INTO img_upload (username,path,created)
VALUES ('".$session_user['college_id']."','".$target."','".date('Y-m-d H:i:s',time())."')"
ON DUPLICATE KEY UPDATE path=$target, created=date('Y-m-d H:i:s',time())."')
Or a simple update.
UPDATE img_upload SET path=$target, date=date('Y-m-d H:i:s',time())."') WHERE username=$session_user['college_id']

Related

Mysql: Insert if row doesnt exist safely with key and unique attribute

Background:
I built a scraper in python (not sure if that matters). I scrape the website and update my html table. The main table stores the autogenerated_id, url, raw_html, date_it_was_scrapped, last_date_the_page_was_updated (provided by the website). My table has many duplicate urls which it shouldnt so i am planning on making urls unique in the database.
Desired outcome:
I only want to insert a row if the url doesnt exist and update the html if last_date_the_page_was_updated > date_it_was_scrapped.
Solution:
The following stackoverflow post shows how.
I havent tested it because of the selected answers warning: INSERT ... ON DUPLICATE KEY UPDATE statement against a table having more than one unique or primary key is also marked as unsafe.
What I plan to do based on the stackoverflow question.
INSERT INTO html_table (url, raw_html, date_it_was_scrapped, last_date_the_page_was_updated)
VALUES (the data)
ON DUPLICATE KEY UPDATE
url = VALUES(url),
raw_html = VALUES(raw_html),
date_it_was_scrapped = VALUES(date_it_was_scrapped),
last_date_the_page_was_updated=VALUES(last_date_the_page_was_updated)
WHERE last_date_page_was_update > date_it_was_scrapped
Question:
What is unsafe about it and is there a safe way to do it?
From the description of bug 58637, which is linked in the MySQL documentation page that flags the INSERT ... ON DUPLICATE KEY UPDATE as unsafe :
When the table has more than one unique or primary key, this statement is sensitive to the order in which the storage engines checks the keys. Depending on this order, the storage engine may determine different rows to mysql, and hence mysql can update different rows [...] The order that the storage engine checks keys is not deterministic.
I understand that your table has an autoincremented primary key, and that you are planning to add a unique key on the url column. Because the primary key is autoincremented, you will not pass it as a parameter for INSERT commands, as shown in your SQL command. Hence MySQL will not need to check for duplicate on this column ; it will only check for duplicates on url. As a consequence, this INSERT should be safe.
Other remarks regarding your question.
you don't need to update the url command on duplicate keys (we know it is the same)
The purpose of the WHERE clause in your query is unclear, are you sure that it is needed ?
You will need to remove the duplicates before you enable the unique constraint on URL.

Which technique is more efficient for replacing records

I have an app that has to import TONS of data from a remote source. From 500 to 1500 entries per call.
Sometimes some of the data coming in will need to replace data already stored in the dB. If I had to guess, I would say once in 300 or 400 entries would one need to be replaced.
Each incoming entry has a unique ID. So I am trying to figure out if it is more efficient to always issue a delete command based on this ID or to check if there is already an entry THEN delete.
I found this SO post where it talks about the heavy work a dB has to do to delete something. But it is discussing a different issue so I'm not sure if it applies here.
Each incoming entry has a unique ID. So I am trying to figure out if it is more efficient to always issue a delete command based on this ID or to check if there is already an entry THEN delete.
Neither. Use INSERT ... ON DUPLICATE KEY UPDATE ....
Since you are using MySQL and you have a unique key then let MySQL do the work.
You can use
INSERT INTO..... ON DUPLICATE KEY UPDATE......
MySQL will try to insert a new record in the table, is the unique value exists in the table then MySQL will update all the field that you have set after the update
You can read more about the INSERT INTO..... ON DUPLICATE KEY UPDATE...... syntax on
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

Update if the field don't contain a sub value

I record the ip address of visitor and store it into mysql database. If the visitor gets a different ip, it will also update the record. I use the following code to do the update and values in following example is just for testing.
insert into visiter_info values ('1344594088179','0','100.100.100.100','china','300x600','IOS','firefox','')
ON DUPLICATE KEY UPDATE
ip_address=concat(ip_address,'|','100.100.100.100'),
location=concat(location,'|','china'),
screen_res=concat(screen_res,'|','300x600'),
os=concat(os,'|','IOS'),
brower=concat(brower,'|','firefox')
It works, but now problem comes, how can I check if there is a record in the database? like this: visitor comes again, with ip 100.100.100.100. Mysql don't know there is a record, and it will re-record. How to check that if contains a sub string before insert?
if not exists(select * from visiter_info where ip_address='100.100.100.100')
insert into visiter_info values ('1344594088179','0','100.100.100.100','china','300x600','IOS','firefox','')
ON DUPLICATE KEY UPDATE
ip_address=concat(ip_address,'|','100.100.100.100'),
location=concat(location,'|','china'),
screen_res=concat(screen_res,'|','300x600'),
os=concat(os,'|','IOS'),
brower=concat(brower,'|','firefox')

use trigger with update and insert query both in mysql

i have login page with user name and password, if the user forgot his/her password then there is a link of forgot password,were one has puts its email id and then the default password will send to its id and it will also update it in table with update query,the problem is that when in future we give this project to client we have truncate whole data and give him empty database, at that time update query won't work at that moment i need to use insert query, so i need a trigger query which fires on insert as well as on update query also later on when data is filled, please help because i never used trigger and i don't know its syntax, please explain me by showing an example
You do not need a trigger. Just run an INSERT INTO...ON DUPLICATE KEY UPDATE query. It will insert a row, but if a row with the same primary key already exists, update the column in the existing row instead.
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html

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