$vqr = $var2 placing value of one field in another

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

$vqr = $var2 placing value of one field in another

Post by jarow »

Hi

All my data is in one table of my database and I would like to know how to write a statment that would do the following:

When a value is entered in one field of a database that value is automatically entered into another field of the same table.

I am new to MySql and php and would appreciate a little help with this.

Many thanks

Jim
hurkle
Forum Newbie
Posts: 15
Joined: Tue Feb 11, 2003 1:21 pm
Location: Minnesota, USA

Post by hurkle »

hmm.. if you were using sql server, I'd say use a 'trigger', but I don't think mySql has that type of thing.

If you want to do this as you store the entry into the first field, you could do something like this..
(this assumes the value you want to put into the two fields is in a variable named $str_value, the table is name tbl_users, fld1 is fld_username, fld2 is fld_displayname)

Code: Select all

$str_sql = "INSERT INTO tbl_users FIELDS(fld_username, fld_displayname) VALUES ('" . $str_value . "','" . $str_value . "')";
That should place the same value into both fields.
If you want something that automatically places the value of fld1 into fld2 each time you run the query, try this..

Code: Select all

$str_sql = "UPDATE tbl_users SET fld_displayname = fld_username WHERE fld_displayname IS NULL";
That should copy the value from fld1 to fld2 if fld2 (fld_displayname) is null

I hope this helps get you started, have fun.
Post Reply