mysql 时间不能为空,MySQL错误:“时间”列不能为空

当尝试使用ON DUPLICATE KEY UPDATE更新包含'Time'列的MySQL记录时,遇到了'Time'列不能为null的错误。问题在于插入或更新时没有正确处理'Time'列。解决方案是在UPDATE部分使用DATE_ADD和IFNULL函数确保即使'Time'为空,也能正确添加时间间隔。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I get the error: Column 'Time' cannot be null when using the query below, it works fine the first time when there is no duplicate but then when trying to update again I get the error: Column 'Time' cannot be null

mysql_query("

INSERT INTO

$table(Username, Time, Videos, Credits)

VALUES

('$user', '$time', '$videos', '$credits')

ON DUPLICATE KEY UPDATE

Time=Time+INTERVAL $time SECOND

Videos=Videos+'$videos',

Credits=Credits+'$credits'

",

$conn

);

Hope you can spot my error as I am new to this, thanks!

Here is some more of my code:

$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

mysql_select_db(DB_NAME, $conn);

// Error checking

if(!$conn) {

die('Could not connect ' . mysql_error());

}

// Localize the GET variables

$user = isset($_GET['username']) ? $_GET['username'] : "";

$time = isset($_GET['time']) ? $_GET['time'] : "";

$videos = isset($_GET['videos']) ? $_GET['videos'] : "";

$credits = isset($_GET['credits']) ? $_GET['credits'] : "";

// Protect against sql injections

$user = mysql_real_escape_string($user);

$time = mysql_real_escape_string($time);

$videos = mysql_real_escape_string($videos);

$credits = mysql_real_escape_string($credits);

$secret = mysql_real_escape_string($secret);

// Insert

$retval = mysql_query("

INSERT INTO

$table(Username, Time, Videos, Credits)

VALUES

('$user', '$time', '$videos', '$credits')

ON DUPLICATE KEY UPDATE

Time = DATE_ADD(IFNULL(Time,now()),INTERVAL '$time' SECOND),

Videos = Videos+'$videos',

Credits = Credits+'$credits'

",

$conn

);

// End Query

if($retval) {

echo "Success! Updated $user with Time: $time - Videos: $videos - Credits: $credits";

} else {

echo "ERROR:
" . mysql_error();

}

mysql_close($conn);

解决方案

It should be:

mysql_query("

INSERT INTO

$table(Username, `Time`, Videos, Credits)

VALUES

('$user', '$time', '$videos', '$credits')

ON DUPLICATE KEY UPDATE

Time = DATE_ADD(IFNULL(`Time`,now()),INTERVAL '$time' SECOND)

,Videos = Videos+'$videos'

,Credits = Credits+'$credits'

",

$conn

);

Don't forget to put single quotes around all injected variables, otherwise mysql_real_escape_string will not protect you.

### MySQL `workingtime` 不能NULL 的解决方案 当尝试向表中插入数据时,如果遇到错误提示 `column 'workingtime' cannot be null`,这表明该被定义为不允许存储NULL值。为了处理这种情况,可以考虑几种方法。 #### 方法一:修改表结构允许为 可以通过更改表结构来使 `workingtime` 接受NULL值: ```sql ALTER TABLE table_name MODIFY COLUMN workingtime DATETIME NULL; ``` 此命令会将指定的数据类型设置为可选输入状态[^1]。 #### 方法二:提供默认值 另一种方式是在创建表或者之后通过SQL语句给定一个缺省值,这样即使应用程序未提供具体数值也不会违反约束条件: ```sql ALTER TABLE table_name ALTER COLUMN workingtime SET DEFAULT CURRENT_TIMESTAMP; ``` 上述代码片段设置了当前时间戳作为新记录中的默认工作时间[^2]。 #### 方法三:确保应用层传递有效参数 检查并修正前端或中间件逻辑,保证每次调用INSERT操作前都已准备好有效的`workingtime`字段内容。对于日期/时间类型的属性尤其重要,因为它们通常依赖于特定格式化字符串表示法。 #### 方法四:使用触发器自动填充 还可以利用MySQL内置功能——触发器,在执行插入动作之前动态计算所需的时间戳,并将其赋值给目标: ```sql CREATE TRIGGER before_insert_table BEFORE INSERT ON table_name FOR EACH ROW BEGIN IF NEW.workingtime IS NULL THEN SET NEW.workingtime = NOW(); END IF; END$$ DELIMITER ; ``` 这段脚本会在任何新的行加入到表格之前运行,从而避免了因缺少必要的非字段而导致失败的情况发生[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值