Reputation: 95
My table "list" contains one field: "exam_date" of type "datetime". Dates in that table are formatted by the following structure: "2014-08-31 21:45:47" (yyyy-mm-dd hh:ii:ss). In my input form date is assigned the following way:
<input id="exam_date" name="exam_date" type="datetime" placeholder="<?php echo date('d-m-y H:i:s'); ?>">
In order to see the value assigne i added to my code:
$myDate = trim($_POST['exam_date']);
echo trim($_POST['exam_date']);
On the display i see the date inserted by the following structure:31-08-14 21:45:47. My code to insert the new value to the table is:
<?php
date_default_timezone_set('xxx');
$dbcon = @mysqli_connect ('xxx', 'xxx', 'xxx', 'test') OR die ('Could not connect to MySQL: ' . mysqli_connect_error () );
mysqli_set_charset($dbcon, 'utf8');
if (isset($_POST['exam_date']))
{
$myDate = trim($_POST['exam_date']);
echo $myDate ;
$q = "INSERT INTO list (exam_date) VALUES ($myDate)";
$result = @mysqli_query ($dbcon, $q);
if($result)
{
echo "record added";
}
else
{
echo "no record added !";
}
}
?>
<form action="add_date.php" method="post">
<input id="exam_date" name="exam_date" type="datetime" placeholder="<?php echo date('y-m-d H:i:s'); ?>">
<input id="submit" type="submit" name="submit" value="Register">
</form>
When I run it i get: "no record added" and when I check the table I see that no record was added indeed. I belive that the reason for not able to add a record is the difference in date structures: that of the table and that of the input. Can I change the date structure in the table to "dd-mm-yy hh:ii:ss"? Is there a way to convert the input structure from "dd-mm-yy hh-ii-ss" to "yyyy-mm-dd hh:ii:ss"? Thanks !
Upvotes: 2
Views: 861
Reputation: 95
Thank you all who tried to help me. I found this code on "youtube":
<!doctype html>
<html lang=en>
<head>
<title>Insert Date</title>
<meta charset=utf-8>
</head>
<body>
<?php
$raw = "2014 08 18 17 45 00";
$xplod = explode(' ', $raw);
print_r($xplod);
$string=$xplod[0].'-'.$xplod[1].'-'.$xplod[2].' '.$xplod[3].':'.$xplod[4].':'.$xplod[5];
echo "<br/>$string";
$date = date("y-m-d H:i:s", strtotime($string));
echo "<br/> $date";
$dbcon = @mysqli_connect ('xxx', 'xxx', 'xxx', 'test')
OR die ('Could not connect to MySQL: ' . mysqli_connect_error () );
mysqli_set_charset($dbcon, 'utf8');
$q = "INSERT INTO list VALUES ('$date')";
$result = @mysqli_query ($dbcon, $q);
if ($result)
{
echo "record added";
}
else
{
echo "record not added";
}
?>
</body>
</html>
Which is satisfactory for a beginner such as I am.
Upvotes: 0
Reputation: 313
This code is much safer as it uses prepared statements http://php.net/manual/en/mysqli.prepare.php.
Never trust user inputted variables!
Notice the name=""
on the submit button, we are checking if the form has been submitted
Always close your forms inputs
properly />
Timezones.. https://www.youtube.com/watch?v=-5wpm-gesOY
If you want to INSERT
a user inputted datetime
try this:
add_date.php:
<?php
date_default_timezone_set('xxx');
$link = mysqli_connect("localhost", "my_user", "my_password", "db_name");
mysqli_set_charset($link, 'utf8');
if (isset($_POST['register'])) {
$myDate = $_POST['exam_date'];
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($stmt = mysqli_prepare($link, "INSERT INTO list (exam_date) VALUES (?)")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $myDate);
/* execute query */
if(mysqli_stmt_execute($stmt)) {
echo "record added";
}
else {
echo "no record added !";
}
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
}
The Form:
<form action="add_date.php" method="post">
<input id="exam_date" name="exam_date" type="datetime" value="<?php echo date('y-m-d H:i:s'); ?>" />
<input id="submit" type="submit" name="register" value="Register" />
</form>
Upvotes: 1
Reputation: 2509
please tell me why you are using
date_default_timezone_set()
if you dont need that than just simply run this query
if ($_SERVER['REQUEST_METHOD']=="POST")
{
$query="INSERT INTO id (date) Values (now())";
$result= mysqli_query($con, $query)or die(mysqli_error($con));
if($result)
{
echo 'success';
}
else {
echo 'failed';
}
Upvotes: 0