dongle7882 2015-11-18 17:27
浏览 87
已采纳

如何在PHP中提交表单时添加日期和时间? [关闭]

I am making a comment system in php which can store comment ,email and name of the user but I also want to add 2 fields more on my database called time - date that store the date and the time when user submitted the form automatically! I have searched a lot but I couldn't find any useful article about this. If you can know how to do that please help me I would appreciate that!

Here's my code:

<form action='comment.php' method='POST'>
    <span>
        <input type='text' name='name' placeholder='Your Name'/>
        <input type='email' name='email' placeholder='Email Address'/>
    </span>
        <textarea name='' name='comment' placeholder='Comment'></textarea>
        <input name='submit' type='submit' value='Submit'></input>
        <p><?php if(isset($errors['name1'])) echo $errors['name1']; ?></p>
        <p><?php if(isset($errors['name2'])) echo $errors['name1']; ?></p>
        <p><?php if(isset($errors['email1'])) echo $errors['name1']; ?></p>
        <p><?php if(isset($errors['email2'])) echo $errors['name1']; ?></p>
        <p><?php if(isset($errors['comment1'])) echo $errors['name1']; ?></p>
        <p><?php if(isset($errors['comment2'])) echo $errors['name1']; ?></p>
</form>
<?php 
$con = mysqli_connect("localhost","root","","ecommerce");
if (isset($_POST['submit'])){
    $name = (isset($_POST['name']));
    $email = (isset($_POST['email']));
    $comment = (isset($_POST['comment']));

    if (empty($name)){
        $errors['name1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:30px;float:right;'>Enter your name</p>";
    }else{
        $name_length = strlen($name);
        if ($name_length > 2 ){
            if (!empty($email)){
                    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    $errors['email1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Enter your email</p>"; 
                }else{
                    if (empty($comment)){
                        $errors['comment1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Write your comment</p>"; 
                    }else{
                        $comment_length = strlen($comment);
                        if ($comment_length < 5){
                            $errors['comment2'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Your comment must be more than 5 words</p>"; 
                        }
                    }
                }
            }else{
                $errors['email2'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Enter your email</p>"; 
            }
        }else{
            $errors['name2'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:30px;float:right;'>Your name must be more than 2 words/p>";
        }
    }

    //check errors
        if(count($errors) == 0)
        {
            $insert_comment = "insert into comments (email,name,comment) values ('$email','$name','$comment')";
            $insert_comment = mysqli_query($con, $insert_comment);
        }
}
?>
  • 写回答

4条回答 默认 最新

  • dpn517111 2015-11-18 17:35
    关注

    Create two fields in your database:

    • modified_date date
    • modified_time time

    Now change your insert query to

    insert into comments (email,name,comment, modified_date, modified_time) values ('$email','$name','$comment', CURDATE(), CURTIME())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?