Jump to content

Datetime messing up


Ninjakreborn

Recommended Posts

The datetime is messing up. On entry it works fine, on edit it messes up.

My field is this:

news_date datetime No None

 

The code I am using for entry is the same for both add/edit. For adding it works fine, for editing it messes up the date and returns it to all 0's (month, year, day, and time).

	$news_id          = $_POST['news_id'];
	$news_date        = strtotime($_POST['news_date']);
	$news_date        = date('Y-m-d', $news_date);
	$news_description = $_POST['news_description'];
	echo $news_date;
	$error = '';

	if ($news_id == '') {
		$error .= 'News ID required.<br />';
	}
	if ($news_date == '') {
		$error .= 'News Date is required.<br />';
	}
	if ($news_description == '') {
		$error .= 'News Description is required.<br />';
	}

	if ($error == '') {
		$message = '';
		if ($news_id == 0) {
			$sql = "INSERT INTO recent_news (news_date, news_description) VALUES ('" . mysql_real_escape_string($news_date) . "','" . mysql_real_escape_string($news_description) . "')";
			if (mysql_query($sql)) {
				$message .= 'News item added successfully.<br />';
			}else {
				$message .= 'News item was NOT added. There was an internal error.<br />';
			}
		}else {
			$sql = "UPDATE recent_news SET news_date = '" . mysql_real_escape_string($news_date) . "' AND news_description = '" . mysql_real_escape_string($news_description) . "' WHERE news_id = '" . mysql_real_escape_string($news_id) . "'";
			if (mysql_query($sql)) {
				$message .= 'News item was edited successfully.<br />';
			}else {
				$message .= 'News item was NOT edited successfully.  There was an internal error.<br />';
			}
		}

When I echo the date, it returns the exact same whether adding/editing so I know it's getting entered into the database the exact same.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/245421-datetime-messing-up/
Share on other sites

AND is a logical operator. You are trying to SET news_date to 'a date' AND'ed with the value in the news_description column. That would result in a TRUE value, which is an invalid DATE, so your news_date column is set to an all zero date value.

Link to comment
https://forums.phpfreaks.com/topic/245421-datetime-messing-up/#findComment-1260502
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.