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 8474765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:42:43+00:00 2026-06-10T17:42:43+00:00

I’ve got a PHP function which receives some parameters. Each parameter corresponds to a

  • 0

I’ve got a PHP function which receives some parameters. Each parameter corresponds to a VALUE of one SQL line.
What I would like to do, is to update the values according to the parameters ; but with having some parameters not mandatory. So if a parameter is set to ‘keep’, the VALUE has to keep its current value. This is where I’m stuck.

Here is the function :

function upt($id, $p1, $p2)
{
    if ($p1 === 'keep')
        $p1 = 'columnName1';
    if ($p2 === 'keep')
        $p2 = 'columnName2';
    $REQ = $DB->prepare('UPDATE ms_cart_products SET columnName1=:P1, columnName2=:P2 WHERE ID=:ID');
    $REQ->execute(array(
        ':P1' => $p1,
        ':P2' => $p2,
        ':ID' => $id));
    $REQ->closeCursor();
}

Obviously, this does not work (even if I had the secret hope that making $p1 = ‘columnName1’ would work as if I made COL1=COL1 directly in the SQL console).
The VALUE is literally set to ‘columnName’.

Any ideas ?

Thank you,
Florian

  • 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-10T17:42:44+00:00Added an answer on June 10, 2026 at 5:42 pm

    Let’s assume that you use either PostgreSQL or MySQL (I don’t mention other RDBMSes here, please tell us if you use something else). Then you can write an UPDATE like

    ...
    SET columnName1 = COALESCE(NULLIF(:your_value, 'keep'), columnName1)
    ...
    

    NULLIF returns its first value when the too values provided are different, otherwise it returns NULL. COALESCE then returns the first non-NULL value. This way, if you pass ‘keep’ to this expression, the inner NULLIF part evaluates to NULL, therefore the COALESCE returns the value kept in columnName1. Otherwise the NULLIF returns the passed value and COALESCE evaluates to that.

    See the docs:

    http://www.postgresql.org/docs/9.1/static/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL

    or

    http://dev.mysql.com/doc/refman/5.5/en/control-flow-functions.html#function_nullif

    http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#function_coalesce

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

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.