MySQL错误:“时间"列不能为空 [英] MySQL ERROR: Column 'Time' cannot be null

查看:244
本文介绍了MySQL错误:“时间"列不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误:使用下面的查询时,时间"列不能为空,没有重复项时第一次运行良好,但随后尝试再次更新时,我得到了错误:时间"列不能为空空

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!

以下是我的一些代码:

$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 "<b>ERROR:</b><br>" . mysql_error();
}

mysql_close($conn);

推荐答案

应为:

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
);

别忘了在所有注入的变量周围加上单引号,否则mysql_real_escape_string不会保护您.

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

请参阅:
http://dev.mysql .com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

这篇关于MySQL错误:“时间"列不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆