dsy1971 2010-05-01 16:57
浏览 119
已采纳

表单向MySQL数据库提交不正确的信息

I've created a form that submits data to a MySQL database but the Date, Time, Year and Month fields constantly revert to the exact same date (1st January 1970) despite the fact that when I submit the information to the database the form displays the current date, time etc to me. I've already set it so that the time and date fields automatically display the current time and date. Could someone please help me with this.

Form:

    <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

For some reason the posts are being submitted without a timestamp and are reverting to a default timestamp.

  • 写回答

4条回答 默认 最新

  • douyihuaimao733955 2010-05-01 17:05
    关注

    You have three text boxes named date. Since $_POST['month'] and $_POST['year'] are not set, strtotime() cannot figure out the correct timestamp and the SQL query looks like this:

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

    Change the names of the input elements to what your PHP code is expecting:

    <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; ?>" />
    

    After you've done this, strtotime() will be able to parse the date and insert the correct value into the query.

    INSERT INTO php_blog (timestamp,title,entry) VALUES ('1272738360','Title Goes Here.','')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?