NOT NULL默认值1插入0和1

时间:2015-04-20 16:23:40

标签: php mysql

在MySQL中,我向表中添加了一个新列...

ALTER TABLE `articles` ADD `visible` NOT NULL DEFAULT (1)

我使用默认值1表示新文章可见,如果文章被禁用,它将更新为0表示无法查看。

现在在新文章中插入而不是MySQL插入默认值1,而是在某些行和1个其他行上插入0。

我想知道导致此问题的原因以及此问题的可能解决方案,而不必手动将这些行更新为1。

例如,插入是......

INSERT INTO articles (filename, owner, name, descr, image1, image2, category, added, info_hash, size, numfiles, save_as, news, external, nfo, lang, anon,tube, last_action) VALUES (".sqlesc($fname).", '".$CURUSER['id']."', ".sqlesc($name).", ".sqlesc($descr).", '".$inames[0]."', '".$inames[1]."', '".$catid."', '" . get_date_time() . "', '".$infohash."', '".$articlesize."', '".$filecount."', ".sqlesc($fname).", '".$news."', '".$external."', '".$nfo."', '".$langid."','$anon', ".sqlesc($tube).",'".get_date_time()."')");

然后我尝试将其更新为

INSERT INTO articles (filename, owner, name, descr, image1, image2, category, added, info_hash, size, numfiles, save_as, news, external, nfo, lang, visible, anon,tube, last_action) VALUES (".sqlesc($fname).", '".$CURUSER['id']."', ".sqlesc($name).", ".sqlesc($descr).", '".$inames[0]."', '".$inames[1]."', '".$catid."', '" . get_date_time() . "', '".$infohash."', '".$articlesize."', '".$filecount."', ".sqlesc($fname).", '".$news."', '".$external."', '".$nfo."', '".$langid."','$visible', '$anon', ".sqlesc($tube).",'".get_date_time()."')");

并且

$visible = 1;

2 个答案:

答案 0 :(得分:2)

您忘记添加数据类型。

您可能需要以下内容:

ALTER TABLE `articles` ADD `visible` TINYINT NOT NULL DEFAULT 1

如果这不能解决问题,那么您可以在代码中的某处设置默认值。

答案 1 :(得分:1)

您需要定义要插入特定类型的列。通用ALTER语句应遵循此格式。

ALTER TABLE table_name ADD column_name column-definition;

我假设您只想拥有1或0,所以以下内容应该有效:

ALTER TABLE `articles` ADD `visible` TINYINT NOT NULL DEFAULT 1
相关问题