0

I am not able to insert automatic date and time in database from frontend form page.Please help me.

<b><input type="datetime" name="DateTime" value="NOW()"/></b>
<br/> <b>Name:</b> 
<input type="text" name="name" />
</b>
<br/> <b>Subject:</b> 
<input type="text" name="subject" />
</b>
<br/> <b>E-mail:</b> 
<input type="text" name="email" />
</b>
<br/> <b>Contact Number:</b> 
<input type="text" name="contact_number" />
<br/>
</p>
<p><b>Please describe your health related problem:</b>
    <br/>
    <textarea name="comment" rows="3" cols="40"></textarea>
    <br/> <b>Select case paper/reports-1 to upload (if any):</b>
    <br/>
    <input type="file" name="report_one" id=" report_one "> <b>Select case 
    paper/reports-2 to upload (if any):</b>
    <br/>
    <input type="file" name="report_two" id=" report_two ">
    <br/>
</p>
<p>
    <input type="submit" name="submit" class="rainbow_border" value="Send 
    it!">
</p>
</form>

Back End is(back_end.php):

$DateTime=$_POST['DateTime'];
$name=$_POST['name'];
$subject=$_POST['subject'];
$email=$_POST['email'];
$contact_number=$_POST['contact_number'];
$comment=$_POST['comment'];
$insert_query="INSERT INTO casepaper (id, DateTime, name, subject, email, 
contact_number, comment, case_paper_path_one, case_paper_path_two) 
VALUES('$id', '$DateTime', '$name', '$subject', '$email', '$contact_number', 
'$comment', '$case_paper_path_one', '$case_paper_path_two')";
$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$statement=$connection->prepare($insert_query);
$statement->bindValue(':id', $id, PDO::PARAM_STR);
$statement->bindValue(':DateTime', $DateTime, PDO::PARM_STR);
$statement->bindValue(':name', $name, PDO::PARAM_STR);
$statement->bindValue(':subject', $subject, PDO::PARAM_STR);
$statement->bindValue(':email', $email, PDO::PARAM_STR);
$statement->bindValue(':contact_number', $contact_number, PDO::PARAM_STR);
$statement->bindValue(':comment', $comment, PDO::PARAM_STR);
$statement->bindValue(':case_paper_path_one', $case_paper_path_one, 
PDO::PARAM_STR);
$statement->bindValue(':case_paper_path_two', $case_paper_path_two, 
PDO::PARAM_STR);
$statement->execute();
}else{
    echo "Sorry, there was a problem uploading your file.";
}
//--reports end--//
ob_start();
header('location:thanks.html');
?>

I'm little bit confused what type,name & value attribute should be there in html form and process back end accordingly and insert successfully in to database.

Thanks in advance.

2 Answers2

0

You can use

<input type="datetime-local" name="dateTime">

but type="datetime-local" is not supported in Firefox, or Internet Explorer 12 and earlier versions.

So , better we use plugins visit https://eonasdan.github.io/bootstrap-datetimepicker/

As your comment you can do these

  1. in the mysql table change the default value type to TIMESTAMP and change default to CURRENT_TIMESTAMP
  2. In your PDO query change as this example

    $stmt = $pdoDb->prepare('INSERT INTO tablename (id, time_created) VALUES (:id,  NOW())');
    
Sanjit Bhardwaj
  • 893
  • 7
  • 13
  • Thanks, but I don't want to pick up date and time during the submission of the form rather I want it automatically entered in to database without interfere of user. Just like comment & blog posting. – rajkumar vishwakarma Feb 10 '18 at 08:18
  • Ok, please look at this code . I want to insert like this... $DateTime= “NOW()”; $insert_query="INSERT INTO casepaper (id, DateTime) VALUES('$id', '$DateTime')"; $connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $statement=$connection->prepare($insert_query); $statement->bindValue(':id', $id, PDO::PARAM_STR); $statement->bindValue(':DateTime', $DateTime, PDO::PARM_STR); $statement->execute(); – rajkumar vishwakarma Feb 10 '18 at 11:22
  • try https://stackoverflow.com/questions/1575601/datetime-now-php-mysql-pdo-variant – Sanjit Bhardwaj Feb 10 '18 at 11:27
0
$date_string="07/01/1997 10:20 AM";
$formatted_date=date_create_from_format("m/d/Y h:i a",$date);
$date=date_format($date,"Y-m-d H:i");

SQL database stores datetime in this format "Y-m-d H:i" . So you need to change your datetime to this format before executing SQL.

use this function date_create_from_format("m/d/Y h:i a",$date); to create a date object from your datetime string. Change "m/d/Y h:i a" format according to your string.

date_format($date,"Y-m-d H:i"); format it in SQL accepted way.

srx
  • 335
  • 1
  • 4
  • 17
  • Thanks, but I don't want date and time should be entered by users.Current date & time must be entered automatically in to database when user submits the filled form.That is the only my problem.Please ignore the input type attribute of date & time in front end form but please provide me solution in back-end part. – rajkumar vishwakarma Feb 10 '18 at 08:16
  • You're welcome. I would advise you to store date and time in a single column rather than having seperate for each. – srx Feb 11 '18 at 11:06