Jump to content

Insert to next field if current is not empty


Darnel

Recommended Posts

Hi, I have this code but would like to modify it to be able to insert a new data in the next field if the current one is not empty.

 

global $db;
 $sql= " UPDATE ".Project
  ." SET "
  ." name_1   =   '".$user_id."' "
  ." WHERE project_id = '".$project_id." '";

 $db->query($sql);

 

So for example if field "name_1" is occupied I would like to add the same $user_id to "name_2".

 

I've been trying to use

CASE WHEN name_1 = '0'

but can't seem to get the syntax right and keep getting Mysql errors.

 

Any help appreciated, thanks

This is going to sound all negative, but you should not have columns in a database with names like - name_1, name_2, ... that store same meaning data. Doing so makes writing any query and code that manipulates that data, difficult. Which you have found to be the case with your UPDATE query.

 

Same meaning data should be store as one row per data item. You can then easily write a query to find, store, or change just that one data item or any or all the related data items.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.