MySQL ERROR: Column 'Time' cannot be null [duplicate]
Possible Duplicate:
MySQL ERROR: Column 'Time' cannot be null
I am getting the error: Column 'Time' cannot be null, this is my code:
$table = "highscores";
// Initialization
$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());
}
if($_GET['secret'] != "5920239") {
header("Location: /404.html");
exit();
}
// Localize the GET variables
$user = isset($_GET['username']) ? $_GET['username'] : "";
$time = isset($_GET['time']) ? $_GET['time'] : "";
$videos = isset($_G开发者_JAVA技巧ET['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/UPDATE
$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);
I only get the error when 'Username' is a duplicate, otherwise it will insert fine.
If you need any more information/need me to do anything then please post and I will respond asap, thanks in advance to everyone who helps!
DATE_ADD()
can't work on TIME
data type, the first argument can only be a DATE
. Also assigning '100'
to a TIME
column results in '00:01:00'
which I don't think is what you want. I believe for what you want to do you should be using a column with TIMESTAMP
type or just UNSIGNED INT
and replace your DATE_ADD() with a simple + operation.
Are you setting a value to $time
? You have code to initialize it to ""
if $_GET['time']
is not passed, and that could be causing your issue.
精彩评论