Reputation:
I've searched through the posts for an answer and the closest answer came from the topic "trying-to-post-date-via-php-with-mysql-need-help"
I've got a form with 2 input fields and .The user selects the date from an html5 popup calendar picker, types in the time, and submits the form to a mysql db. I'm having trouble getting the date and time fields to validate/upload to the db.
In my MySQL db I have a table called appointments with a column called curdate that has a type of DATE; another column is called curtime with a type of TIME.
My PHP file has the PHP validation and SQL actions at the top of the file and the sticky html form at the bottom. The form uses to send the form
Here is the code:
$td = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $td );
if(isset($_POST['submitted'])) {
require_once('mysqli_connect.php');
$errors=array();
if(empty($_POST['curdate'])) {
$errors[]='Your date did not match';
} else {
$td=mysqli_real_escape_string($dbc,trim($_POST['curdate']));
}
if(empty($_POST['curtime'])) {
$errors[]='Your time is empty';
} else {
$tt=$_POST['curtime'];
}
if(empty($errors)) {
$q="INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason) VALUES ('$fn','$ln','$e','$td','$tt','$phys','$reason')";
$r=@mysqli_query($dbc,$q);
if($r) {
echo '<h1>Thanks</h1>
<p>You are now registered</p><p><br/></p>';
} else {
echo '<h1>System Error</h1>
<p class="error">You could not be registered</p>';
echo '<p>'.mysqli_error($dbc).'<br/><br/>Query:'.$q.'</p>';
}
mysqli_close();
include('includes/footer.html');
exit();
} else {
echo '<h1>Error</h1>
<p class="error">The following errors occurred:<br/>';
foreach ($errors as $msg) {
echo "-$msg<br/>\n";
}
echo '</p><p> Please try again</p><p><br/></p>';
}
mysqli_close($dbc);
}
Upvotes: 0
Views: 14197
Reputation: 35
You might have a problem, if you in mysql made that row in table dateandtime type, if that's the case you won't be able to store it like that. If you want it to be like that just use text or varchar ....
DATE - Stores a date value in the form YYYY-MM-DD. For example 2008-10-23. DATETIME - Stores a date and time value of the form YYYY-MM-DD HH:MM:SS. For example 2008-10-23 10:37:22. The supported range of dates and times is 1000-01-01 00:00:00 all the way through to 9999-12-31 23:59:59 TIMESTAMP - Similar to DATETIME with some differences depending on the version of MySQL and the mode in which the server is running.
Upvotes: 0
Reputation: 1528
Mysql Or Mysqli Supports date format as 'yyyy-mm-dd' so u need to convert date and time in proper format before inserting in DB
If you want to insert current date and time when the user submits the form then Use following Query
INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason) VALUES ('$fn','$ln','$e',CURDATE(),CURTIME(),'$phys','$reason')
But if you want to insert date from form field then do following
$td=$_POST['curdate']; $tt=$_POST['curtime']; $td=date('Y-m-d',strtotime($td)); //Converts date to 'yyyy-mm-dd' acceptable to mysql $tt=date('H:i:s',strtotime($tt)); //converts time to HH:mm:ss //Now apply Your Query as INSERT INTO users (first_name,last_name,email,curdate,curtime,physician,reason) VALUES ('$fn','$ln','$e','$td','$tt','$phys','$reason')
Upvotes: 2