7

I'm using MySQL 5.1 hosted at my ISP. This is my query

mysql_query("
IF EXISTS(SELECT * FROM licensing_active WHERE title_1='$title_1') THEN
    BEGIN
        UPDATE licensing_active SET time='$time' WHERE title_1='$title_1')
    END ELSE BEGIN
        INSERT INTO licensing_active(title_1) VALUES('$title_1')
    END   
") or die(mysql_error());  

The error is

... check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS(SELECT * FROM licensing_active WHERE title_1='Title1') THEN ' at line 1

My actual task involves

WHERE title_1='$title_1' AND title_2='$title_2' AND version='$version' ...ETC...

but I have reduced it down to make things simpler for my problem solving

In my searches on this, I keep seeing references to 'ON DUPLICATE KEY UPDATE', but don't know what to do with that.

6 Answers 6

24

Here is a simple and easy solution, try it.

$result = mysql_query("SELECT * FROM licensing_active WHERE title_1 ='$title_1' ");

if( mysql_num_rows($result) > 0) {
    mysql_query("UPDATE licensing_active SET time = '$time' WHERE title_1 = '$title_1' ");
}
else
{
    mysql_query("INSERT INTO licensing_active (title_1) VALUES ('$title_1') ");
}

Note: Though this question is from 2012, keep in mind that mysql_* functions are no longer available since PHP 7.

Sign up to request clarification or add additional context in comments.

1 Comment

That works for me, thanks. And seems straightforward to understand and remember. It might not be super-efficient, but this isn't yet a high traffic application so I'm sure this method will be fine for a year or more. Thank you.
13

This should do the trick for you:

insert into 
    licensing_active (title_1, time) 
    VALUES('$title_1', '$time') 
    on duplicate key 
        update set time='$time'

This is assuming that title_1 is a unique column (enforced by the database) in your table.

The way that insert... on duplicate works is it tries to insert a new row first, but if the insert is rejected because a key stops it, it will allow you to update certain fields instead.

5 Comments

Hi Fluffeh. title_1 is not unique. The only unique column I have is an int named id. It is set as 'auto_increment', 'unique' and 'primary'. I don't access it directly. Am still struggling with the same error.
Couldn't quite get there, so I did the logic in PHP - as described by Dark Wish. Thanks though.
works for me when I remove set at the end. i.e. INSERT INTO <table> (col1, col2) VALUES('f1','f2') ON DUPLICATE KEY UPDATE col2='fn'
Just remember to use WHERE clause!!! From here: w3schools.com/sql/sql_update.asp The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
@BamsBamx: This is all one INSERT statement.
1

The syntax of your query is wrong. Checkout http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

Use the on duplicate key syntax to achieve the result you want. See http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

Comments

0

Another solution

$insertQuery = "INSERT INTO licensing_active (title_1) VALUES ('$title_1')"; 
if(!$link->query($insertQuery)){ // Insert fails, so update
    $updateQuery = "UPDATE licensing_active SET time='$time' WHERE title_1='$title_1'";
    $link->query($updateQuery);
}

Comments

0

Here is the example I tried and its works fine:

INSERT INTO user(id, name, address) VALUES(2, "Fadl", "essttt") ON DUPLICATE KEY UPDATE name = "kahn ajab", address = "Address is test"

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.
-1

I am amazed to see so many useless codes and answers... Just replace INSERT with REPLACE.

¯\(ツ)

1 Comment

The answer could have been provided without attitude, which is not required or helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.