gpt4 book ai didi

javascript - 将日期插入 mysql 数据库 [我正在使用 php 和 xampp mysql 数据库]

转载 作者:行者123 更新时间:2023-11-30 22:05:01 27 4
gpt4 key购买 nike

<?php
session_start();
require 'dbconfig/config.php';
//phpinfo();
?>
<!DOCTYPE html>
<html>
<head>
<title>Time Tracker</title>
<link rel="stylesheet" href="style1.css">
<script type="text/javascript">
function clock() {
var mytime = new Date();
var seconds = mytime.getSeconds();
var minutes = mytime.getMinutes();
var hours = mytime.getHours();
var currentTime = hours + ":" + minutes + ":" + seconds;
document.getElementById("Timer").firstChild.nodeValue = currentTime;
}
</script>
</head>

<body>
<div id="main-wrapper">
<center><h2>Time Tracker</h2></center>
<center><h3>Welcome <?php echo $_SESSION['username']; ?></h3></center>

<form method="post">
<div class="Time Fields">


<tr>
<td>Date</td>

<?php
$username = $_SESSION['username'];
$date=date('Y:m:d');
$query = "select * from datetime where username='$username' and Date = '$date'";
$query_run1 = mysqli_query($con,$query);
$row = mysqli_fetch_array($query_run1,MYSQLI_ASSOC);

?>
<input type="text" name="Date" value="<?php echo $row ['Date']; ?> " size=10>
</tr>

<tr>
<td>Time In</td>

<?php
$username = $_SESSION['username'];
$date=date('Y:m:d');
$query = "select * from datetime where username='$username' and Date = '$date'";
$query_run1 = mysqli_query($con,$query);
$row = mysqli_fetch_array($query_run1,MYSQLI_ASSOC);

?>

<input type="int" name="TimeIn" value="<?php echo $row ['TimeIn']; ?> " size=20 readonly>
</tr>


<tr>
<td>Time Out</td>
<span id = "Timer">00:00:00</span>
<script type="text/javascript">
setInterval('clock()', 1000);
</script>

</tr>


<tr>
<td>Total Time</td>
<td><input type=text name=x3 size=20 value=""></td>
</tr>

<tr>

<td><button class="Submit" name="Submit" type="submit">Submit</button></tr>

</table>
</div>


</div>
<?php
if(isset($_POST['Submit']))
{
@$TimeOut=$_POST['Timer'];
$query_run3 = mysqli_query($con,"insert into DateTime (TimeOut) values('$TimeOut')");
}
?>
</body>

</html>`<?php
session_start();
require 'dbconfig/config.php';
//phpinfo();
?>
<!DOCTYPE html>
<html>
<head>
<title>Time Tracker</title>
<link rel="stylesheet" href="style1.css">
<script type="text/javascript">
function clock() {
var mytime = new Date();
var seconds = mytime.getSeconds();
var minutes = mytime.getMinutes();
var hours = mytime.getHours();
var currentTime = hours + ":" + minutes + ":" + seconds;
document.getElementById("Timer").firstChild.nodeValue = currentTime;
}
</script>
</head>

<body>
<div id="main-wrapper">
<center><h2>Time Tracker</h2></center>
<center><h3>Welcome <?php echo $_SESSION['username']; ?></h3></center>

<form method="post">
<div class="Time Fields">


<tr>
<td>Date</td>

<?php
$username = $_SESSION['username'];
$date=date('Y:m:d');
$query = "select * from datetime where username='$username' and Date = '$date'";
$query_run1 = mysqli_query($con,$query);
$row = mysqli_fetch_array($query_run1,MYSQLI_ASSOC);

?>
<input type="text" name="Date" value="<?php echo $row ['Date']; ?> " size=10>
</tr>

<tr>
<td>Time In</td>

<?php
$username = $_SESSION['username'];
$date=date('Y:m:d');
$query = "select * from datetime where username='$username' and Date = '$date'";
$query_run1 = mysqli_query($con,$query);
$row = mysqli_fetch_array($query_run1,MYSQLI_ASSOC);

?>

<input type="int" name="TimeIn" value="<?php echo $row ['TimeIn']; ?> " size=20 readonly>
</tr>


<tr>
<td>Time Out</td>
<span id = "Timer">00:00:00</span>
<script type="text/javascript">
setInterval('clock()', 1000);
</script>

</tr>


<tr>
<td>Total Time</td>
<td><input type=text name=x3 size=20 value=""></td>
</tr>

<tr>

<td><button class="Submit" name="Submit" type="submit">Submit</button></tr>

</table>
</div>


</div>
<?php
if(isset($_POST['Submit']))
{
@$TimeOut=$_POST['Timer'];
$query_run3 = mysqli_query($con,"insert into DateTime (TimeOut) values('$TimeOut')");
}
?>
</body>

</html>`<?php
session_start();
require 'dbconfig/config.php';
//phpinfo();
?>
<!DOCTYPE html>
<html>
<head>
<title>Time Tracker</title>
<link rel="stylesheet" href="style1.css">
<script type="text/javascript">
function clock() {
var mytime = new Date();
var seconds = mytime.getSeconds();
var minutes = mytime.getMinutes();
var hours = mytime.getHours();
var currentTime = hours + ":" + minutes + ":" + seconds;
document.getElementById("Timer").firstChild.nodeValue = currentTime;
}
</script>
</head>

<body>
<div id="main-wrapper">
<center><h2>Time Tracker</h2></center>
<center><h3>Welcome <?php echo $_SESSION['username']; ?></h3></center>

<form method="post">
<div class="Time Fields">


<tr>
<td>Date</td>

<?php
$username = $_SESSION['username'];
$date=date('Y:m:d');
$query = "select * from datetime where username='$username' and Date = '$date'";
$query_run1 = mysqli_query($con,$query);
$row = mysqli_fetch_array($query_run1,MYSQLI_ASSOC);

?>
<input type="text" name="Date" value="<?php echo $row ['Date']; ?> " size=10>
</tr>

<tr>
<td>Time In</td>

<?php
$username = $_SESSION['username'];
$date=date('Y:m:d');
$query = "select * from datetime where username='$username' and Date = '$date'";
$query_run1 = mysqli_query($con,$query);
$row = mysqli_fetch_array($query_run1,MYSQLI_ASSOC);

?>

<input type="int" name="TimeIn" value="<?php echo $row ['TimeIn']; ?> " size=20 readonly>
</tr>


<tr>
<td>Time Out</td>
<span id = "Timer">00:00:00</span>
<script type="text/javascript">
setInterval('clock()', 1000);
</script>

</tr>


<tr>
<td>Total Time</td>
<td><input type=text name=x3 size=20 value=""></td>
</tr>

<tr>

<td><button class="Submit" name="Submit" type="submit">Submit</button></tr>

</table>
</div>


</div>
<?php
if(isset($_POST['Submit']))
{
@$TimeOut=$_POST['Timer'];
$query_run3 = mysqli_query($con,"insert into DateTime (TimeOut) values('$TimeOut')");
}
?>
</body>

</html>`<?php
session_start();
require 'dbconfig/config.php';
//phpinfo();
?>
<!DOCTYPE html>
<html>
<head>
<title>Time Tracker</title>
<link rel="stylesheet" href="style1.css">
<script type="text/javascript">
function clock() {
var mytime = new Date();
var seconds = mytime.getSeconds();
var minutes = mytime.getMinutes();
var hours = mytime.getHours();
var currentTime = hours + ":" + minutes + ":" + seconds;
document.getElementById("Timer").firstChild.nodeValue = currentTime;
}
</script>
</head>

<body>
<div id="main-wrapper">
<center><h2>Time Tracker</h2></center>
<center><h3>Welcome <?php echo $_SESSION['username']; ?></h3></center>

<form method="post">
<div class="Time Fields">


<tr>
<td>Date</td>

<?php
$username = $_SESSION['username'];
$date=date('Y:m:d');
$query = "select * from datetime where username='$username' and Date = '$date'";
$query_run1 = mysqli_query($con,$query);
$row = mysqli_fetch_array($query_run1,MYSQLI_ASSOC);

?>
<input type="text" name="Date" value="<?php echo $row ['Date']; ?> " size=10>
</tr>

<tr>
<td>Time In</td>

<?php
$username = $_SESSION['username'];
$date=date('Y:m:d');
$query = "select * from datetime where username='$username' and Date = '$date'";
$query_run1 = mysqli_query($con,$query);
$row = mysqli_fetch_array($query_run1,MYSQLI_ASSOC);

?>

<input type="int" name="TimeIn" value="<?php echo $row ['TimeIn']; ?> " size=20 readonly>
</tr>


<tr>
<td>Time Out</td>
<span id = "Timer">00:00:00</span>
<script type="text/javascript">
setInterval('clock()', 1000);
</script>

</tr>


<tr>
<td>Total Time</td>
<td><input type=text name=x3 size=20 value=""></td>
</tr>

<tr>

<td><button class="Submit" name="Submit" type="submit">Submit</button></tr>

</table>
</div>


</div>
<?php
if(isset($_POST['Submit']))
{
@$TimeOut=$_POST['Timer'];
$query_run3 = mysqli_query($con,"insert into DateTime (TimeOut) values('$TimeOut')");
}
?>
</body>

</html>

点击提交后,运行“$query_run3”时,数据库中的时间没有更新。它显示为 00:00:00。此处使用的 javascript 提供了一个运行时钟,我希望在单击提交时提交并在数据库字段中更新。如果这不可能:- 我的确切要求是:- 我需要创建一个输入类型字段,我可以在其中输入 24 小时格式的时间(html 输入字段),以便当我单击提交时它会在数据库中更新.

提前致谢。

最佳答案

看来您需要一些 HTML 修复:

  • 添加action表单的属性:

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">


  • 自 Javascript 函数 clock()专门针对带有 id="Timer" 的 HTML 元素,您可以使用它来处理属于 <form> 的输入字段正在提交。记得添加 name属性到这个字段。因此,修复实际上非常简单,如下所示。我不确定您对 <span> 有多满意更改为 <input>虽然字段:

    <input id="Timer" name="Timer" disabled="disabled" value="00:00:00">

  • 在 Javascript 中,更改:

    document.getElementById("Timer").firstChild.nodeValue = currentTime;

    document.getElementById("Timer").value = currentTime;


如果你想保留<span>并在表单提交中使用它的值(value),这里有一个关于如何去做的基本想法:

  • 你需要一个隐藏的 <input> field 。例如:<input type="hidden" name="TimerCopy" id="TimerCopy" value="">

  • 提交表单时,阻止默认操作(暂时)并复制 <span id="Timer"> 的值至 <input id="TimerCopy">使用 Javascript/jQuery

  • 使用 Javascript/jQuery 以编程方式提交表单

  • 在 PHP 中接收 TimerCopy字段为:

    $TimeOut = $_POST['TimerCopy'];

其余的应该照常进行。


编辑:一个最小的工作代码:

    <input id="Timer" name="Timer" disabled="disabled" value="00:00:00">

<script>
function clock() {
var mytime = new Date();
var seconds = mytime.getSeconds();
var minutes = mytime.getMinutes();
var hours = mytime.getHours();
var currentTime = hours + ":" + minutes + ":" + seconds;
document.getElementById("Timer").value = currentTime;
}
setInterval('clock()', 1000);
</script>


注意事项:

关于javascript - 将日期插入 mysql 数据库 [我正在使用 php 和 xampp mysql 数据库],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42098526/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com