2017-01-30 184 views
0

代碼:如何將當前日期插入數據庫?

<?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> 

如何插入當前日期到數據庫時提交表單數據,如果我刪除插入查詢當前的日期也將運行,但是當我插入當前日期就說明沒有如此。我們怎麼能做到這一點?

謝謝

+0

'current_date'的類型是什麼? 'datetime'或'timestamp'或'date'? –

+0

你可以在你的sql查詢中使用CURRENT_TIMESTAMP或NOW()。 – Shubhranshu

+0

current_date類型是日期 –

回答

1

使用NOW()或者CURRENT_TIMESTAMP()

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

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

當創建一個表,你可以使用CURRENT_DATE字段類型爲CURRENT_TIM ESTAMP所以當前的日期和時間自動插入到列中。

0

嘗試上面的代碼:

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

嘗試上面的代碼。 –

0

或者使用CURDATE()MYSQL的功能,它打印的日期相同的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())";