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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.