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 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题