dongxiong2000 2017-11-17 20:27
浏览 104

为什么Internet Explorer和Edge将新的Date()数据输出到数据库中的方式与其他浏览器不同?

I found a way to add a date as a time stamp when a user submits a message with my web application. Every thing works perfectly but when I use new Date(); to have its value store in a MySQL database, Internet Explorer and Edge just inputs the data like this ‎12‎:‎14‎ ‎PM but it suppose to look something like this 12:33 PM.

In other words different browsers input it perfectly in the database so I know my code isn't wrong but Edge and Explorer don't store the data properly so what's going on here? And how can I get Edge and Explorer to store the time data like how the other browsers do perfectly? And this is a screenshot of the database.

Screenshot

The method I'm using to store data into the database works perfectly with all different kinds of data like strings, numeric etc.. and all different browsers but edge and explorer can't store JavaScript new Date() value properly.

Here is a method on how I'm getting a time stamp and generating values to be use in the server side code.

message.js

//Get message date
function message_date() {

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd = '0'+dd
} 

if(mm<10) {
    mm = '0'+mm
} 

today = mm + '/' + dd + '/' + yyyy;
var today;  
return today;
}  

var message_date_stamp = message_date();  // this will grab you the return value from message_date();

//Get user send message time
var timez = new Date();
timez = timez.toLocaleString('en-US', { hour: 'numeric',minute:'numeric', hour12: true });
var get_time = timez;

$(document).ready(function(){  
$("#send").click(function(){
var conversation_id = $("#conversation_id").val();
var user_id = $("#user_id").val();
var member_name = $("#member_name").val();
var message = $("#message").val();
var status= $("#status").val();
var sender_id= $("#sender_id").val();
var receiver_id= $("#receiver_id").val();
var photo_link = $("#photo_link").val();
var hyper_link = $("#hyper_link").val();
var date = message_date_stamp;
var time = get_time;

// Returns successful data submission message when the entered information is stored in database.
var dataString = 'conversation_id='+ conversation_id + '&user_id=' + user_id + '&member_name='+ member_name + '&message='+ message + '&status='+ status + '&sender_id='+ sender_id + '&receiver_id='+ receiver_id + '&photo_link='+ photo_link + '&hyper_link='+ hyper_link + '&time='+ time + '&date='+ date;

    //AJAX code to submit form.
    $.ajax({
    type: "POST",
    url: "send_message_data.php",
    data: dataString
    });
    });
    });

This is how I'm storing the data into the database in PHP just know that the value of new Date() is generated from a previous page and in PHP it's represented by PHP's variable call $time.

send_message_data.php

<?php
//Credentials
$db_servername = "localhost";
$db_username = "1234u";
$db_password = "1234";
$db_name = "1234db";

$connect = mysqli_connect($db_servername, $db_username, $db_password, $db_name);

//Form values
$conversation_id = $_POST['conversation_id'];
$user_id = $_POST['user_id'];
$member_name= $_POST['member_name'];
$message= $_POST['message'];
$status= $_POST['status'];
$sender_id= $_POST['sender_id'];
$receiver_id= $_POST['receiver_id'];

$date= $_POST['date'];

//FOR STACK OVER FLOW USERS $time is how I'm storing this into the data base  
$time= $_POST['time'];


$photo_link= $_POST['photo_link'];
$hyper_link= $_POST['hyper_link'];

//SECURITY

//Basic character escaping.
$conversation_id = htmlspecialchars($conversation_id);
$member_name = htmlspecialchars($member_name);
$message = htmlspecialchars($message);
$photo_link = htmlspecialchars($photo_link);
$hyper_link = htmlspecialchars($hyper_link);

//OTHER FEATURES 




//Prevent MYSQLI injection.
$conversation_id = mysqli_real_escape_string($connect, $conversation_id);
$member_name = mysqli_real_escape_string($connect, $member_name);
$message = mysqli_real_escape_string($connect, $message);
$photo_link = mysqli_real_escape_string($connect, $photo_link );
$hyper_link = mysqli_real_escape_string($connect, $hyper_link);

//Removes tags combat the following injections HTML, JAVA-SCRIPT, PHP.
$conversation_id = filter_var($conversation_id, FILTER_SANITIZE_STRING);
$member_name = filter_var($member_name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
$photo_link = filter_var($photo_link, FILTER_SANITIZE_STRING);
$hyper_link = filter_var($hyper_link, FILTER_SANITIZE_STRING);


$query = "INSERT INTO messages_x1(conversation_id,user_id,member_name,message,status,sender_id,receiver_id,photo_link,hyper_link,date,time) values ('$conversation_id','$user_id','$member_name','$message','$status','$sender_id','$receiver_id','$photo_link','$hyper_link','$date','$time')";

$run = $connect->query($query);

if($run) {
echo "Form Submitted succesfully";
}

?>

And one last thing I read that it has nothing to do with the code but I read some where that mentions Explorer and Edge have problems on interacting with JavaScript's new Date() poorly in certain situations. I just want a solution to bypass Explorer and Edge limitations.

  • 写回答

1条回答 默认 最新

  • dongpu3347 2017-11-18 00:33
    关注

    No offense to the people that said it was my code that had errors. But I always knew it wasn't my code that was causing this. I was right I found out that different browsers will tend to structure your code differently in your database. So for any future readers wondering why their code is treated and inserted differently in the data base then you know why. Here is proof starting from internet explore to chrome and to safari Iphone.

    Screen shot

    The good news is that internet explore version of the time is outputting the time in a human readable way but sadly it's structure of the time differs in the data base.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100