2010-05-01 4 views
0

데이터를 MySQL 데이터베이스에 제출하는 양식을 만들었지 만 날짜, 시간, 연도 및 월 필드는 언제 같은 날짜 (1970 년 1 월 1 일)로 되 돌린다. 양식에 현재 날짜, 시간 등이 표시되는 정보를 데이터베이스에 제출합니다. 시간과 날짜 필드에 자동으로 현재 시간과 날짜가 표시되도록 이미 설정했습니다. 누군가 나를 도와주세요.양식을 MySQL 데이터베이스에 제출하는 중

형태 : 게시물이 타임 스탬프없이 제출되고, 디폴트의 타임 스탬프로 되돌아 몇 가지 이유로

<html> 

<head> 
<title>Ultan's Blog | New Post</title> 
<link rel="stylesheet" href="css/newposts.css" type="text/css" /> 
</head> 

<body> 
<div class="new-form"> 
<div class="header"> 
<a href="edit.php"><img src="images/edit-home-button.png"></a> 
</div> 
<div class="form-bg"> 
<?php 
if (isset($_POST['submit'])) { 

    $month = htmlspecialchars(strip_tags($_POST['month'])); 
    $date = htmlspecialchars(strip_tags($_POST['date'])); 
    $year = htmlspecialchars(strip_tags($_POST['year'])); 
    $time = htmlspecialchars(strip_tags($_POST['time'])); 
    $title = htmlspecialchars(strip_tags($_POST['title'])); 
    $entry = $_POST['entry']; 

     $timestamp = strtotime($month . " " . $date . " " . $year . " " . $time); 

    $entry = nl2br($entry); 

    if (!get_magic_quotes_gpc()) { 
     $title = addslashes($title); 
     $entry = addslashes($entry); 
    } 

    mysql_connect ('localhost', 'root', 'root') ; 
    mysql_select_db ('tmlblog'); 

$sql = "INSERT INTO php_blog (timestamp,title,entry) VALUES ('$timestamp','$title','$entry')"; 

    $result = mysql_query($sql) or print("Can't insert into table php_blog.<br />" . $sql . "<br />" . mysql_error()); 

    if ($result != false) { 
     print "<p class=\"success\">Your entry has successfully been entered into the blog. </p>"; 
    } 

    mysql_close(); 
} 
?> 

<?php 
$current_month = date("F"); 
$current_date = date("d"); 
$current_year = date("Y"); 
$current_time = date("H:i"); 
?> 

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 

<input type="text" name="month" value="<?php echo $current_month; ?>" /> 
<input type="text" name="date" value="<?php echo $current_date; ?>" /> 
<input type="text" name="year" value="<?php echo $current_year; ?>" /> 


<input type="text" name="time" id="time" size="5"value="<?php echo $current_time; ?>" /> 

<input class="field2" type="text" id="title" value="Title Goes Here." name="title" size="40" /> 

<textarea class="textarea" cols="80" rows="20" name="entry" id="entry" class="field2"></textarea> 

<input class="field" type="submit" name="submit" id="submit" value="Submit"> 

</form> 
</div> 
</div> 
</div> 
<div class="bottom"></div> 
<!-- //End Wrapper!--> 
</body> 

</html> 

</html> 

alt text http://i42.tinypic.com/2ns4755.jpg

.

답변

0

date이라는 세 개의 텍스트 상자가 있습니다. $_POST['month']$_POST['year']이 설정되어 있지 않기 때문에, strtotime() 올바른 타임 스탬프를 알아낼 수 없습니다 및 SQL 쿼리는 다음과 같습니다 : PHP 코드가 기대하는 것과 입력 요소의

INSERT INTO php_blog (timestamp,title,entry) VALUES ('','Title Goes Here.','') 

변경 이름 :

<input type="text" name="month" value="<?php echo $current_month; ?>" /> 
<input type="text" name="date" value="<?php echo $current_date; ?>" /> 
<input type="text" name="year" value="<?php echo $current_year; ?>" /> 

이 작업을 완료하면 strtotime()은 날짜를 구문 분석하고 올바른 값을 쿼리에 삽입 할 수 있습니다.

INSERT INTO php_blog (timestamp,title,entry) VALUES ('1272738360','Title Goes Here.','') 
+0

이 질문을 업데이트했습니다. 새로운 정보를 확인하십시오 ... 제발 도와주세요. –

+0

그러나 양식을 수정하지 않았습니다. 그것을 고친 후 내 컴퓨터에서 코드를 실행했고 올바른 타임 스탬프가 생성되었습니다. – Mike

+0

여기에 코드를 수정하지 않았지만 타임 스탬프가 '0'인 방법을 보여주는 이미지를 추가했습니다. 내 컴퓨터에 코드를 수정했습니다. 양식을 업데이트했지만 정확한 타임 스탬프로 내 블로그에 제출하지 않았습니다. –

0

strtotime처럼 보이는 형식은 사용자가 먹는 형식을 이해하지 못합니다.
대신 mktime을 사용하십시오.

항상 코드를 디버깅해야합니다. 즉, 잘못된 내용이 있는지 확인하기 위해 쿼리를 인쇄해야합니다.

또는 MySQL을 지원하는 형식 colymn를 사용하는 것을 고려 date 또는 datetime

당신은 혼동 될 수

2010-05-01 00:00:00 문자열이 단지 숫자 ANS MySQL의 타임 스탬프 형식 열 인 unix timestamp

+0

지금 것을 해봤지만 현재 날짜와 시간을 표시하지만 2010 년 7 월 23 일에 제출합니다. –

+0

@ThatMacLad 데이터베이스에 어떤 유형의 타임 스탬프 필드가 있습니까? 'int'가 아닌 경우 유닉스 타임 스탬프를 저장해서는 안됩니다 –

+0

이 질문을 업데이트했습니다. 새로운 정보를 확인하십시오. 제발 도와주세요. –

1

양식과 같은 소리는 데이터베이스에 매우 적은 양의 "틱"을 제공합니다. with name = "date"및 id = "date"가 세 번 있음을 주목하십시오. 이것은 당신의 문제 일 가능성이 큽니다. 해당 이름과 ID를 변경하고, 월, 연도, 날짜를 사용하십시오 ("포스트 백"에 따라).

+0

이 질문을 업데이트했습니다. 새로운 정보를 확인하십시오 ... 제발 도와주세요. –

0

strtotime은 당신이 당신의 날짜를 제출하는 형식을 인식 실패가 될 수

고전 YYYY/MM/DD 형식을 시도해보십시오. 여전히 지금

$timestamp = strtotime("$year/$month/$date $time"); 
+0

이 질문을 업데이트했습니다. 새로운 정보를 확인하십시오 ... 제발 도와주세요. –

+0

@ThatMacLad 예, 1970 년 1 월 1 일은 타임 스탬프 '0'입니다. 내 제안이나 다른 제안 중 하나를 시도해 보셨습니까? –

+0

나는 이전의 모든 제안을 시도했다. 귀하의 제안은 2010 년 7 월 23 일까지 기본 타임 스탬프를 설정하는 것으로 보입니다. –

관련 문제