MySQL ERROR: Column 'Time' cannot be null

前端 未结 2 1984
悲哀的现实
悲哀的现实 2021-01-29 02:38

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 th

相关标签:
2条回答
  • 2021-01-29 02:53

    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.

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

    0 讨论(0)
  • 2021-01-29 02:57

    If there's no duplicate, then this query will do an insert, and the Time value will be null, as no value was ever set. Null + anything is null, hence the error.

    Try ... Time = COALESCE(Time, 0) + INTERVAL $time SECOND or similar to get aroun dit.

    0 讨论(0)
提交回复
热议问题