473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

on duplicate key update only null values

I am relatively new to php and mySQL. I have a database with a user form used to update and create records. I need to allow duplicate key entries to update but I do not want to overwrite existing values in the field. I only want to update the fields that are empty. Here is the code as I have it.
Expand|Select|Wrap|Line Numbers
  1.  
  2. $sql="INSERT INTO Results (a, b, c, d, e, f, g, h, i, j)
  3. VALUES ('$_POST[a]','$_POST[b]','$_POST[c]','$_POST[d]','$_POST[e]','$_POST[f]','$_POST[g]','$_POST[h]','$_POST[i]','$_POST[j]')
  4. ON DUPLICATE KEY UPDATE f = '$_POST[f]', g = '$_POST[g]', h = '$_POST[h]', i = '$_POST[i]', j = '$_POST[j]'";
  5.  
  6.  
Currently, it does update the field with new information, but it will overwrite existing data with an empty field if it is left empty on update. How can I get it to only update fields that are empty?
Jun 27 '11 #1

✓ answered by Rabbit

You can use the IF function.

Expand|Select|Wrap|Line Numbers
  1. UPDATE TableName SET FieldName = IF(Input = '', FieldName, Input)
Basically this sets FieldName to itself if the input value is a blank string. You said null in your title but leaving a field empty does not always mean it's a null. Most likely a blank string is being passed.

2 3720
Rabbit
12,516 Expert Mod 8TB
You can use the IF function.

Expand|Select|Wrap|Line Numbers
  1. UPDATE TableName SET FieldName = IF(Input = '', FieldName, Input)
Basically this sets FieldName to itself if the input value is a blank string. You said null in your title but leaving a field empty does not always mean it's a null. Most likely a blank string is being passed.
Jun 27 '11 #2
Thanks so much! Seems so simple now, I just COULD NOT get it. Thank you thank you!
Jun 28 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Joshua Ruppert | last post by:
A section of the documentation for the isSet() function states: Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant. Where would you encounter a NULL byte? Is a null...
26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
1
by: stewart palmer via AccessMonster.com | last post by:
What is wrong with the following: UPDATE enq SET enq.DUP = Left$(!,3)&Left$(!,3)&Right$(!,3)&Left$(!,2)&left$(!,5); According to ACCESS 97 Help, using & instead of + should overcome the problem...
6
by: Modest Marsupial | last post by:
What is the DAO method of allowing a recordset to have null values? Thanks, marie
12
by: Ivan Sammut | last post by:
Hi, I created an access database and I am using ado.net to access it. Now I am trying to create an update statment but I am having some error when so fields are empty. Here is my statment...
6
by: doncee | last post by:
I have a list box that is generated on a form by way of a Parameter Query. Problem is whenever I try to refer to the list box, i.e., to update the underlying table, I am getting a "null" value...
1
by: RDizzle | last post by:
okay. so all i am doing is changing a registration script that uses $_GET to a script that uses $_POST, but the validation script now returns NULL values for all posted vars. What's the deal? ...
5
by: hr833 | last post by:
I'm trying to check for null values so that the staff would know that the lookup table need to be updated. Hower for many of the ways i tried, error 424 object required always appear If...
10
by: Toby Gallier | last post by:
Hello! I have a form that is calculating averages as follows: " =(NZ()+Nz()+Nz())/3 " However I need to now adjust for null values , so for example if value2 is null I would then need to...
0
by: Manoj Deshwal | last post by:
I have a table named client_master in sqlserver and i have a windows application in c#.In c# application, there are three forms. One is to enter Client details. Second is to update the existing...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.