如何将当前日期插入数据库?

时间:2017-01-30 06:01:28

标签: javascript php jquery html5 css3

代码:

<?php
session_start();
if(isset($_POST['enq']))
{

  extract($_POST);

 $query = mysqli_query($link, "SELECT * FROM enquires2 WHERE email = '".$email. "'");
 if(mysqli_num_rows($query) > 0)
  {
      echo '<script>alert("EmailId Already Exist Please Login with diffrent Id");</script>';
  }
 else
 {
 $enqq =  "insert into enquires2(name,email,phone,message,current_date)values('$name','$email','$phone','$message','$current_date')"; 
 $res_enqq = mysqli_query($link,$enqq);
 if($res_enqq)
  {
    if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])
    {  
      echo '<script>alert("Your Enquiry Has Been Send. We May Contact You Soon ");</script>';

    }
    else
    {  
      echo '<script>alert("Validation Code Does Not Match Please Try Again");</script>';    
    } ?>

形式:

<form method="post" action = 'index.php' onsubmit="return validateForm();" name="form">
      <div class="form-group has-feedback">
        <input type="text" class="form-control" id="name" placeholder="Full Name" name="name" style="width: 74%; background: black;">
      </div>

      <div class="form-group has-feedback">
        <input type="email" class="form-control" id="email" placeholder="Email Id" name="email" style="width: 74%; background: black;">
      </div>
      <div class="form-group has-feedback">
        <input type="text" class="form-control" id="phone" placeholder="Phone" name="phone" style="width: 74%; background: black;">
      </div>

      <div class="form-group has-feedback">
        <textarea rows="4" cols="39" name="message" placeholder="Message" style="background: black;"></textarea>
      </div>

      <div class="form-group has-feedback">
        <input name="captcha" type="text" placeholder="Put Text Here" style="margin-left: -100px; background: black;">
        <img src="captcha.php" style="margin-top: -7px;"/><br>
      </div>

      <input type="hidden" name="current_date" id="current_date"/>
      <script type="text/javascript">
      function getDate()
      {
          var today = new Date();
          var dd = today.getDate();
          var mm = today.getMonth()+1; //January is 0!
          var yyyy = today.getFullYear();
          if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm}
          today = yyyy+"-"+mm+"-"+dd;

          document.getElementById("current_date").value = today;
      }
      getDate();
      </script>

      <div class="form-group has-feedback">
        <input type="submit" name="enq" class="btn btn-success" value="Submit" style="margin-left: 215px; border-radius: 0px; margin-top: -80px;">
      </div>
</form>

当提交表单数据时如何将当前日期插入数据库如果我从插入查询中删除当前日期它将运行但是当我插入当前日期时它没有显示任何内容。我们怎么能这样做?

谢谢

3 个答案:

答案 0 :(得分:1)

使用 NOW() CURRENT_TIMESTAMP()

  $enqq =  "insert into enquires2(name,email,phone,message,current_date)values('$name','$email','$phone','$message',CURRENT_TIMESTAMP())";

position:fixed;

创建表格时,您可以将 current_date 字段类型用作 CURRENT_TIMESTAMP ,以便将当前日期和时间自动插入到列中。

答案 1 :(得分:0)

尝试以上代码:

$enqq =  "insert into enquires2(name,email,phone,message,current_date)
          values('$name','$email','$phone','$message','".date('Y-m-d')."')";

答案 2 :(得分:0)

或者使用MYSQL的 CURDATE()功能,它打印的日期与php的功能相同,更容易:

http://www.w3schools.com/sql/func_curdate.asp

示例:

ennq = "INSERT INTO enquires2 (name, email, phone, message, current_date) VALUES ('".$name."', '".$email."', '".$phone."', '".$message."', CURDATE())";