php 表单未将日期发送到数据库 SQL
php form not sending date through to database SQL
我已经使用 HTML & PHP 设置了一个表单。我将从表单提交的所有信息发送到 SQL 数据库。当我提交表单时,我收到以下错误代码:
ERROR: Could not execute INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`, `tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('Nathan Langer','07:30','16:00','18:00','19:00','06:30','15:00','03:00','16:00','05:00','16:00','00:00','00:00','00:00','00:00'). Column count doesn't match value count at row 1
如您所见,日期没有被推迟(应该在 Nathan Langer 和 07:30 之间。
我的提交代码如下:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "username", "pass", "dbname");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$Name = mysqli_real_escape_string($link, $_POST['Name']);
$insertdate = date('Y-m-d', strtotime($_POST['weekending']));
$monin = mysqli_real_escape_string($link, $_POST['monin']);
$monout = mysqli_real_escape_string($link, $_POST['monout']);
$tuein = mysqli_real_escape_string($link, $_POST['tuein']);
$tueout = mysqli_real_escape_string($link, $_POST['tueout']);
$wedin = mysqli_real_escape_string($link, $_POST['wedin']);
$wedout = mysqli_real_escape_string($link, $_POST['wedout']);
$thursin = mysqli_real_escape_string($link, $_POST['thursin']);
$thursout = mysqli_real_escape_string($link, $_POST['thursout']);
$friin = mysqli_real_escape_string($link, $_POST['friin']);
$friout = mysqli_real_escape_string($link, $_POST['friout']);
$satin = mysqli_real_escape_string($link, $_POST['satin']);
$satout = mysqli_real_escape_string($link, $_POST['satout']);
$sunin = mysqli_real_escape_string($link, $_POST['sunin']);
$sunout = mysqli_real_escape_string($link, $_POST['sunout']);
// attempt insert query execution
$sql = "INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`, `tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('$Name','$monin','$monout','$tuein','$tueout','$wedin','$wedout','$thursin','$thursout','$friin','$friout','$satin','$satout','$sunin','$sunout')";
if(mysqli_query($link, $sql)){
header('location:timesheets.php');
} else{
echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
这是因为我在提交表格中的日期做错了吗?表单中的日期字段如下:
<div class="col-md-6 col-sm-6" >
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-calendar"></i>
</span>
<input type="date" id="weekending" class="form-control " name="weekending" placeholder="Week Ending">
</div>
</div>
请有人帮我找出问题所在以及为什么它不会 post 通过数据库?
从
更改
$sql = "INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`,`tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('$Name','$monin','$monout','$tuein','$tueout','$wedin','$wedout','$thursin','$thursout','$friin','$friout','$satin','$satout','$sunin','$sunout')";
到
$sql = "INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`,`tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('$Name','$insertdate','$monin','$monout','$tuein','$tueout','$wedin','$wedout','$thursin','$thursout','$friin','$friout','$satin','$satout','$sunin','$sunout')";
您传递的列比没有传递的多。值,
因此您需要通过添加一个列名称或删除一个值来更正查询。
INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`, `tuein`,
`tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`,
`satin`, `satout`, `sunin`, `sunout`) VALUES ('Nathan
Langer','07:30','16:00','18:00','19:00','06:30','15:00','03:00','16:00','05:00',
'16:00','00:00','00:00','00:00','00:00', '00:00')
上面的代码可以工作,因为我在其中添加了一个值。
我已经使用 HTML & PHP 设置了一个表单。我将从表单提交的所有信息发送到 SQL 数据库。当我提交表单时,我收到以下错误代码:
ERROR: Could not execute INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`, `tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('Nathan Langer','07:30','16:00','18:00','19:00','06:30','15:00','03:00','16:00','05:00','16:00','00:00','00:00','00:00','00:00'). Column count doesn't match value count at row 1
如您所见,日期没有被推迟(应该在 Nathan Langer 和 07:30 之间。
我的提交代码如下:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "username", "pass", "dbname");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$Name = mysqli_real_escape_string($link, $_POST['Name']);
$insertdate = date('Y-m-d', strtotime($_POST['weekending']));
$monin = mysqli_real_escape_string($link, $_POST['monin']);
$monout = mysqli_real_escape_string($link, $_POST['monout']);
$tuein = mysqli_real_escape_string($link, $_POST['tuein']);
$tueout = mysqli_real_escape_string($link, $_POST['tueout']);
$wedin = mysqli_real_escape_string($link, $_POST['wedin']);
$wedout = mysqli_real_escape_string($link, $_POST['wedout']);
$thursin = mysqli_real_escape_string($link, $_POST['thursin']);
$thursout = mysqli_real_escape_string($link, $_POST['thursout']);
$friin = mysqli_real_escape_string($link, $_POST['friin']);
$friout = mysqli_real_escape_string($link, $_POST['friout']);
$satin = mysqli_real_escape_string($link, $_POST['satin']);
$satout = mysqli_real_escape_string($link, $_POST['satout']);
$sunin = mysqli_real_escape_string($link, $_POST['sunin']);
$sunout = mysqli_real_escape_string($link, $_POST['sunout']);
// attempt insert query execution
$sql = "INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`, `tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('$Name','$monin','$monout','$tuein','$tueout','$wedin','$wedout','$thursin','$thursout','$friin','$friout','$satin','$satout','$sunin','$sunout')";
if(mysqli_query($link, $sql)){
header('location:timesheets.php');
} else{
echo "ERROR: Could not execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
这是因为我在提交表格中的日期做错了吗?表单中的日期字段如下:
<div class="col-md-6 col-sm-6" >
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-calendar"></i>
</span>
<input type="date" id="weekending" class="form-control " name="weekending" placeholder="Week Ending">
</div>
</div>
请有人帮我找出问题所在以及为什么它不会 post 通过数据库?
从
更改$sql = "INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`,`tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('$Name','$monin','$monout','$tuein','$tueout','$wedin','$wedout','$thursin','$thursout','$friin','$friout','$satin','$satout','$sunin','$sunout')";
到
$sql = "INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`,`tuein`, `tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`, `satin`, `satout`, `sunin`, `sunout`) VALUES ('$Name','$insertdate','$monin','$monout','$tuein','$tueout','$wedin','$wedout','$thursin','$thursout','$friin','$friout','$satin','$satout','$sunin','$sunout')";
您传递的列比没有传递的多。值,
因此您需要通过添加一个列名称或删除一个值来更正查询。
INSERT INTO `timesheets` (`Name`, `weekending`, `monin`, `monout`, `tuein`,
`tueout`, `wedin`, `wedout`, `thursin`, `thursout`, `friin`, `friout`,
`satin`, `satout`, `sunin`, `sunout`) VALUES ('Nathan
Langer','07:30','16:00','18:00','19:00','06:30','15:00','03:00','16:00','05:00',
'16:00','00:00','00:00','00:00','00:00', '00:00')
上面的代码可以工作,因为我在其中添加了一个值。