Post an HTML Input with datetime Value to MySQL Database
I am looking for a solution to capturing a users input from a HTML input set to a value of 'datetime-local' into a MySQL database with a table field set to 'datetime'. Whenever I submit the form an ID is generated and the date is saved, but the time is not. EX: 2018-11-20 00:00:00.
My HTML:
<form class="" action="/index.php" method="post">
<input type="datetime-local" name="myDate" id="myDate"><br /><br />
<button id="submit" value="submit">Submit</button><br /><br />
</form>
My PHP:
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$sql = "INSERT INTO test_one ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['myDate'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
I got the code from a tutorial on Youtube, and it is working so far. I just want to be able to get the time that they select as well.
UPDATE:
The time is being saved, but it appears to be in military time. EX: Input = 11/22/2018 5:00 PM ; Output = 2018-11-22 17:00:00
UPDATE AFTER FIRST COMMENT:
This directs me to a failed HTTP request -
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
$sql = "INSERT INTO test_one ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['$date'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
If it has anything to do with ' ' around $date I have tried it without and got the same error.
Thank you in advance!
php html mysql database
add a comment |
I am looking for a solution to capturing a users input from a HTML input set to a value of 'datetime-local' into a MySQL database with a table field set to 'datetime'. Whenever I submit the form an ID is generated and the date is saved, but the time is not. EX: 2018-11-20 00:00:00.
My HTML:
<form class="" action="/index.php" method="post">
<input type="datetime-local" name="myDate" id="myDate"><br /><br />
<button id="submit" value="submit">Submit</button><br /><br />
</form>
My PHP:
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$sql = "INSERT INTO test_one ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['myDate'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
I got the code from a tutorial on Youtube, and it is working so far. I just want to be able to get the time that they select as well.
UPDATE:
The time is being saved, but it appears to be in military time. EX: Input = 11/22/2018 5:00 PM ; Output = 2018-11-22 17:00:00
UPDATE AFTER FIRST COMMENT:
This directs me to a failed HTTP request -
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
$sql = "INSERT INTO test_one ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['$date'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
If it has anything to do with ' ' around $date I have tried it without and got the same error.
Thank you in advance!
php html mysql database
add a comment |
I am looking for a solution to capturing a users input from a HTML input set to a value of 'datetime-local' into a MySQL database with a table field set to 'datetime'. Whenever I submit the form an ID is generated and the date is saved, but the time is not. EX: 2018-11-20 00:00:00.
My HTML:
<form class="" action="/index.php" method="post">
<input type="datetime-local" name="myDate" id="myDate"><br /><br />
<button id="submit" value="submit">Submit</button><br /><br />
</form>
My PHP:
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$sql = "INSERT INTO test_one ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['myDate'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
I got the code from a tutorial on Youtube, and it is working so far. I just want to be able to get the time that they select as well.
UPDATE:
The time is being saved, but it appears to be in military time. EX: Input = 11/22/2018 5:00 PM ; Output = 2018-11-22 17:00:00
UPDATE AFTER FIRST COMMENT:
This directs me to a failed HTTP request -
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
$sql = "INSERT INTO test_one ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['$date'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
If it has anything to do with ' ' around $date I have tried it without and got the same error.
Thank you in advance!
php html mysql database
I am looking for a solution to capturing a users input from a HTML input set to a value of 'datetime-local' into a MySQL database with a table field set to 'datetime'. Whenever I submit the form an ID is generated and the date is saved, but the time is not. EX: 2018-11-20 00:00:00.
My HTML:
<form class="" action="/index.php" method="post">
<input type="datetime-local" name="myDate" id="myDate"><br /><br />
<button id="submit" value="submit">Submit</button><br /><br />
</form>
My PHP:
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$sql = "INSERT INTO test_one ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['myDate'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
I got the code from a tutorial on Youtube, and it is working so far. I just want to be able to get the time that they select as well.
UPDATE:
The time is being saved, but it appears to be in military time. EX: Input = 11/22/2018 5:00 PM ; Output = 2018-11-22 17:00:00
UPDATE AFTER FIRST COMMENT:
This directs me to a failed HTTP request -
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
$sql = "INSERT INTO test_one ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['$date'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
If it has anything to do with ' ' around $date I have tried it without and got the same error.
Thank you in advance!
php html mysql database
php html mysql database
edited Nov 20 '18 at 4:03
EataSandwhich
asked Nov 20 '18 at 3:44
EataSandwhichEataSandwhich
125
125
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The issue is that the datetime-local adds a T in the middle, i.e. 2018-11-19T10:52:23. You have to change the format to remove the T. Maybe try this:
$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
Unfortunately that did not work for me. Thank you though! I placed it above the $sql line then update the $_POST after the VALUE to capture $date, but after I submitted the form I got an HTTP error. @miken32
– EataSandwhich
Nov 20 '18 at 3:57
@EataSandwhich Did you use it as $_POST['date']? As that won't work it would need to be just $date. i.e. VALUES ($date)
– Dan W.
Nov 20 '18 at 3:59
Ill update my question to include the new code @Dan W.
– EataSandwhich
Nov 20 '18 at 4:00
@EataSandwhich The time 'military time' is how servers treat datetime, you have to convert it back after you pull it out of the database.
– Dan W.
Nov 20 '18 at 4:03
Oh.... ok. so I guess I am good with that then, right? If you can't tell I am an expert at this hahah @Dan W.
– EataSandwhich
Nov 20 '18 at 4:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385909%2fpost-an-html-input-with-datetime-value-to-mysql-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue is that the datetime-local adds a T in the middle, i.e. 2018-11-19T10:52:23. You have to change the format to remove the T. Maybe try this:
$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
Unfortunately that did not work for me. Thank you though! I placed it above the $sql line then update the $_POST after the VALUE to capture $date, but after I submitted the form I got an HTTP error. @miken32
– EataSandwhich
Nov 20 '18 at 3:57
@EataSandwhich Did you use it as $_POST['date']? As that won't work it would need to be just $date. i.e. VALUES ($date)
– Dan W.
Nov 20 '18 at 3:59
Ill update my question to include the new code @Dan W.
– EataSandwhich
Nov 20 '18 at 4:00
@EataSandwhich The time 'military time' is how servers treat datetime, you have to convert it back after you pull it out of the database.
– Dan W.
Nov 20 '18 at 4:03
Oh.... ok. so I guess I am good with that then, right? If you can't tell I am an expert at this hahah @Dan W.
– EataSandwhich
Nov 20 '18 at 4:05
add a comment |
The issue is that the datetime-local adds a T in the middle, i.e. 2018-11-19T10:52:23. You have to change the format to remove the T. Maybe try this:
$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
Unfortunately that did not work for me. Thank you though! I placed it above the $sql line then update the $_POST after the VALUE to capture $date, but after I submitted the form I got an HTTP error. @miken32
– EataSandwhich
Nov 20 '18 at 3:57
@EataSandwhich Did you use it as $_POST['date']? As that won't work it would need to be just $date. i.e. VALUES ($date)
– Dan W.
Nov 20 '18 at 3:59
Ill update my question to include the new code @Dan W.
– EataSandwhich
Nov 20 '18 at 4:00
@EataSandwhich The time 'military time' is how servers treat datetime, you have to convert it back after you pull it out of the database.
– Dan W.
Nov 20 '18 at 4:03
Oh.... ok. so I guess I am good with that then, right? If you can't tell I am an expert at this hahah @Dan W.
– EataSandwhich
Nov 20 '18 at 4:05
add a comment |
The issue is that the datetime-local adds a T in the middle, i.e. 2018-11-19T10:52:23. You have to change the format to remove the T. Maybe try this:
$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
The issue is that the datetime-local adds a T in the middle, i.e. 2018-11-19T10:52:23. You have to change the format to remove the T. Maybe try this:
$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
edited Nov 20 '18 at 3:56
miken32
23.6k84872
23.6k84872
answered Nov 20 '18 at 3:51
Dan W.Dan W.
706
706
Unfortunately that did not work for me. Thank you though! I placed it above the $sql line then update the $_POST after the VALUE to capture $date, but after I submitted the form I got an HTTP error. @miken32
– EataSandwhich
Nov 20 '18 at 3:57
@EataSandwhich Did you use it as $_POST['date']? As that won't work it would need to be just $date. i.e. VALUES ($date)
– Dan W.
Nov 20 '18 at 3:59
Ill update my question to include the new code @Dan W.
– EataSandwhich
Nov 20 '18 at 4:00
@EataSandwhich The time 'military time' is how servers treat datetime, you have to convert it back after you pull it out of the database.
– Dan W.
Nov 20 '18 at 4:03
Oh.... ok. so I guess I am good with that then, right? If you can't tell I am an expert at this hahah @Dan W.
– EataSandwhich
Nov 20 '18 at 4:05
add a comment |
Unfortunately that did not work for me. Thank you though! I placed it above the $sql line then update the $_POST after the VALUE to capture $date, but after I submitted the form I got an HTTP error. @miken32
– EataSandwhich
Nov 20 '18 at 3:57
@EataSandwhich Did you use it as $_POST['date']? As that won't work it would need to be just $date. i.e. VALUES ($date)
– Dan W.
Nov 20 '18 at 3:59
Ill update my question to include the new code @Dan W.
– EataSandwhich
Nov 20 '18 at 4:00
@EataSandwhich The time 'military time' is how servers treat datetime, you have to convert it back after you pull it out of the database.
– Dan W.
Nov 20 '18 at 4:03
Oh.... ok. so I guess I am good with that then, right? If you can't tell I am an expert at this hahah @Dan W.
– EataSandwhich
Nov 20 '18 at 4:05
Unfortunately that did not work for me. Thank you though! I placed it above the $sql line then update the $_POST after the VALUE to capture $date, but after I submitted the form I got an HTTP error. @miken32
– EataSandwhich
Nov 20 '18 at 3:57
Unfortunately that did not work for me. Thank you though! I placed it above the $sql line then update the $_POST after the VALUE to capture $date, but after I submitted the form I got an HTTP error. @miken32
– EataSandwhich
Nov 20 '18 at 3:57
@EataSandwhich Did you use it as $_POST['date']? As that won't work it would need to be just $date. i.e. VALUES ($date)
– Dan W.
Nov 20 '18 at 3:59
@EataSandwhich Did you use it as $_POST['date']? As that won't work it would need to be just $date. i.e. VALUES ($date)
– Dan W.
Nov 20 '18 at 3:59
Ill update my question to include the new code @Dan W.
– EataSandwhich
Nov 20 '18 at 4:00
Ill update my question to include the new code @Dan W.
– EataSandwhich
Nov 20 '18 at 4:00
@EataSandwhich The time 'military time' is how servers treat datetime, you have to convert it back after you pull it out of the database.
– Dan W.
Nov 20 '18 at 4:03
@EataSandwhich The time 'military time' is how servers treat datetime, you have to convert it back after you pull it out of the database.
– Dan W.
Nov 20 '18 at 4:03
Oh.... ok. so I guess I am good with that then, right? If you can't tell I am an expert at this hahah @Dan W.
– EataSandwhich
Nov 20 '18 at 4:05
Oh.... ok. so I guess I am good with that then, right? If you can't tell I am an expert at this hahah @Dan W.
– EataSandwhich
Nov 20 '18 at 4:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385909%2fpost-an-html-input-with-datetime-value-to-mysql-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown