donmqryh49993 2015-08-31 00:38
浏览 61
已采纳

日期列不会在提交给DB的表单上显示正确的日期戳

For some reason or another the date and time in my date column shows 0000-00-00 00:00:00 when I submit my form to my DB. Only way I can get the date right is by updating it manually. It just started doing it and I'm not sure what I changed. I'm having a real hard time troubleshooting this.

Edit form:

<?php
if(!defined('IN_ADMIN')) exit;
?>

<div class="page">
    <h1 class="edit"><?=ucfirst($mode)?> post</h1>
    <span class="error-text"><?=$response_text?></span>
    <form action="admin.php?mode=<?=$mode?>&id=<?=$post['post_id']?>" method="post">
    <p>
        <label for="title">Post title:</label><br />
        <input type="text" size="80" id="title" name="data[post_title]" value="<?=htmlspecialchars(stripslashes($post['post_title']))?>" />
    </p>
    <p>
        <label for="title">Upload Image:</label><br />
        <input type="text" size="80" id="title" name="data[image]" value="<?=htmlspecialchars(stripslashes($post['image']))?>" />
    </p>
        <p>
        <label for="title">Image Alt:</label><br />
        <input type="text" size="80" id="title" name="data[image_alt]" value="<?=htmlspecialchars(stripslashes($post['image_alt']))?>" />
    </p>
        <p>
        <label for="title">Post Category:</label><br />
        <input type="text" size="80" id="title" name="data[post_category]" value="<?=htmlspecialchars(stripslashes($post['post_category']))?>" />
    </p>
    <p>
        <label for="content">Post Insert:</label><br />
        <textarea cols="77" rows="10" id="insert" name="data[post_insert]"><?=htmlspecialchars(stripslashes($post['post_insert']))?></textarea><br />
        <span class="form-text">Brief little tid-bit about the article for home page</span>
    </p>

    <p>
        <label for="content">Post content:</label><br />
        <script>edToolbar('mytxtarea'); </script>
        <textarea cols="77" rows="10" id="mytxtarea" class="ed" name="data[post_content]"><?=htmlspecialchars(stripslashes($post['post_content']))?></textarea><br />
        <span class="form-text">To format just use raw HTML.. &lt;strong&gt;, &lt;span&gt;, etc</span>
    </p>


    <p>
        <label for="status">Post status:</label><br />
        <select id="status" name="data[published]">
            <?=generate_option_list(array('0' => 'Unpublished', '1' => 'Published'), $post['published'])?>
        </select>
    </p>

    <p>
        <input class="button" type="submit" name="miniblog_PostBack" value="<?=ucfirst($mode)?>" />
    </p>



</div>

That's the form that I use, and here is the index.php that the form is in:

case 'add':

if(isset($_POST['miniblog_PostBack']))
{
        $data = $_POST['data'];

        $data['post_slug'] = mb_slug($_POST['data']['post_title']);
        $data['date']      = time();

        $sql ='';
        $i = 1;
        foreach($data as $field => $value)
        {
            if($value == '')
            {
                $failed = true;
                break;
            }
            $fields .= "`" . mysql_real_escape_string($field) . "`";
            $values .= "'" . mysql_real_escape_string($value) . "'";

            $values .= ($i == sizeof($data)) ? '' : ', ';
            $fields .= ($i == sizeof($data)) ? '' : ', ';

            $i++;
        }

        $post = $_POST['data'];

        if($failed)
        {
            $response_text = 'Error: You must fill out all fields';
        }
        else
        {
            $result = mysql_query("INSERT INTO `db` ({$fields}) VALUES({$values})");
            $response_text = ($result) ? 'Post added' : 'Post could not be added';
        }

}

include('edit.php');

break;
  • 写回答

2条回答 默认 最新

  • dousu1916 2015-08-31 00:53
    关注
    $data['date'] = time();
    

    Should be:

    $data['date'] = date("Y-m-d H:i:s");
    

    The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

    The date() function formats a local date and time (by default the current one), and returns the formatted date string, in this case, in the format MYSQL needs it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办