Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7855479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:24:00+00:00 2026-06-02T20:24:00+00:00

I have a PHP script which runs a MySQL query. $query = INSERT INTO

  • 0

I have a PHP script which runs a MySQL query.

$query = "INSERT INTO table (col1, col2)
          VALUES('$val1', '$val2')
          ON DUPLICATE KEY UPDATE col2= IF(IS NOT NULL '$val1', 'test', 'col2)";

Here is what I am trying to do:
Col1 is the primary key. If there is a duplicate, it checks to see if the insert value for col2 is null. If not, it will update with the value, otherwise the value will stay the same.

This insert fails. When I try to run it manually in sqlyog (inserting actual values in place of variables), I get the following error:
Error Code: 1064

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 ‘IS NOT NULL…..’

I have checked the MySQL reference manual for the IS NOT NULL comparison operator (http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#operator_is-not-null), and also for the INSERT … ON DUPLICATE KEY UPDATE Syntax (http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) and believe I am using both correctly, but obviously that is not the case.

What am I doing wrong?

For reference, I am using MySQL 5 and the script is running on a RHEL5 server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:24:04+00:00Added an answer on June 2, 2026 at 8:24 pm

    The syntax IS NOT NULL column is wrong. Correct: column IS NOT NULL. I’m not sure what this 'test' is about. You say that you want to either update the column or keep it as it is.

    $query = "INSERT INTO table (col1, col2)
              VALUES('$val1', '$val2')
              ON DUPLICATE KEY UPDATE col2 = IF('$val2' IS NOT NULL, '$val2', col2)";
    

    which can also be written as:

    $query = "INSERT INTO table (col1, col2)
              VALUES('$val1', '$val2')
              ON DUPLICATE KEY UPDATE 
                col2 = COALESCE( VALUES(col2), col2)";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a php script which runs fine when executed by SSHing into my
I have a (PHP) script which runs on a new row in MySQL. However,
Here I have a simple php script which displays some values from a database
I have a scheduled task on my server which runs a PHP script every
I have small PHP script which has $query = SELECT MAX(id) FROM `dbs`; //query
I have the following PHP script which runs nicely and inserts all the data
I have tried a session.php script which runs at the head of each page
I have a script in kohana which runs fine on php 5.2.11. I took
I have this PHP script which i'm grabbing images from a directory and displaying
I have a php script which saves the original image, then resizes it -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.