开发者

How can I make a field default a value with every new record?

开发者 https://www.devze.com 2023-04-05 03:58 出处:网络
I have an HTML form to insert ($_POST) a new record to MySQL. I have a field that always needs the value \"FC\".开发者_运维百科Do I make this happen in phpMyAdmin or do I insert \"FC\" at the time wh

I have an HTML form to insert ($_POST) a new record to MySQL.

I have a field that always needs the value "FC".开发者_运维百科 Do I make this happen in phpMyAdmin or do I insert "FC" at the time when I insert the record via HTML form?


Just perform this query:

ALTER TABLE `tablename` ALTER `fieldname` SET DEFAULT 'FC';

Supposing that you have tablename table and fieldname


MySQL has the option to specify a default value for columns. Edit the column and set default to As defined: and put 'FC' in the field.

For consistency you might also want to insert 'FC' in the form since the default value will not be used if something else is provided.

Or if you really want to limit the value only to 'FC' then why not hardcode it into your query?

0

精彩评论

暂无评论...
验证码 换一张
取 消