Reputation: 183
I have got everything I need in terms of inputting my data into my data from a form but struggling to get it to timestamp or looking for the best methods.
submit form:
<form action="actions/newDocAdd.php" method="post">
<input type="text" name="doc_title" id="doc_title" required="required" placeholder="Document Title"/><br />
<input type="text" name="doc_content" id="doc_content" placeholder="Document Content"/><br/>
<br><br>
<input type="submit" value="Create Document" name="submit"/><br />
</form>
<?php
if(isset($_POST["submit"])){
$hostname='localhost';
$username='******';
$password='******';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=******",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "INSERT INTO doc_list (doc_title, doc_content) VALUES ('".$_POST["doc_title"]."','".$_POST["doc_content"]."')";
if ($dbh->query($sql)) {
header ('Location: ../docList.php');
}
else{
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
I have a field inside the DB which is set to DATETIME named 'doc_create' but just need some idea at which point it timestamp the entry and where?
Upvotes: 1
Views: 31
Reputation:
Try using NOW() when inserting the doc entry
<form action="actions/newDocAdd.php" method="post">
<input type="text" name="doc_title" id="doc_title" required="required" placeholder="Document Title"/><br />
<input type="text" name="doc_content" id="doc_content" placeholder="Document Content"/><br/>
<br><br>
<input type="submit" value="Create Document" name="submit"/><br />
</form>
<?php
if(isset($_POST["submit"])){
$hostname='localhost';
$username='******';
$password='******';
$title = $_POST["doc_title"];
$content = $_POST["doc_content"];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=******",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$stmt = $dbh->prepare("INSERT INTO doc_list (doc_title, doc_content,doc_create) VALUES (:title, :content, NOW())");
$stmt->bindParam(':title', $title);
$stmt->bindParam(':content', $content);
if ($stmt->execute()) {
header ('Location: ../docList.php');
}
else{
... // your else code
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
Upvotes: 1
Reputation: 883
the best point is default value for the field in table definition. you must define that field as follow:
timeStampField TIMESTAMP DEFAULT CURRENT_TIMESTAMP
also you can define field type as DATETIME
notice that this will work for mySql version MySQL 5.6.5 and higher.
Upvotes: 2