我使用UPDATE查询来更改数据库。我对一行中的特定单元格所做的更新会插入到数据库中。然而,发生了什么是在该单元格输入as 0000-00-00之后出现的任何空白日期空间。
例如,当我更新最终审核日期时,这就是我应该得到的
Before Update
Date Received Final Review Date Date Delivered Date Accepted
2015-03-03
After Update
Date Received Final Review Date Date Delivered Date Accepted
2015-03-03 2015-08-05
Instead I get this:
Date Received Final Review Date Date Delivered Date Accepted
2015-03-03 2015-08-05 0000-00-00 0000-00-00
我已尝试对此进行故障排除,但我对mysqli / php相当新,所以我知道我可能错过了一些简单的事情。任何帮助解决这个问题都将受到赞赏。
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE Projects SET Project='$_POST[project]', Client='$_POST[client]', LastName='$_POST[lastname]', DateReceived='$_POST[datereceived]', FinalReviewDate='$_POST[finalreviewdate]', DateDelivered='$_POST[datedelivered]', DateAccepted='$_POST[dateaccepted]' WHERE Project='$_POST[hidden]'";
mysqli_query($conn, $UpdateQuery);
};
$sql = "SELECT * FROM Projects";
$result = $conn->query($sql);
echo "<table>
<tr>
<th>Project</th>
<th>Client</th>
<th>Last Name</th>
<th>Date Received</th>
<th>Final Review Date</th>
<th>Date Delivered</th>
<th>Date Accepted</th>
</tr>";
while($record = mysqli_fetch_array($result))
{
if ($result->num_rows > 0){
echo "<form action='mynewform.php' method='post'>";
echo "<tr>";
echo "<td>" . "<input type='text' name='project' value='" . $record['Project'] . "' /></td>";
echo "<td>" . "<input type='text' name='client' value='" . $record['Client'] . "'/></td>";
echo "<td>" . "<input type='text' name='lastname' value='" . $record['LastName'] . "' /></td>";
echo "<td>" . "<input type='text' name='datereceived' value='" . $record['DateReceived'] . "' /></td>";
echo "<td>" . "<input type='text' name='finalreviewdate' value='" . $record['FinalReviewDate'] . "' /></td>";
echo "<td>" . "<input type='text' name='datedelivered' value='" . $record['DateDelivered'] . "' /></td>";
echo "<td>" . "<input type='text' name='dateaccepted' value='" . $record['DateAccepted'] . "' /></td>";
echo "<td>" . "<input type='hidden' name='hidden' value='" . $record['Project'] . "' /></td>";
echo "<td>" . "<input type='submit' name='update' value='update' /></td>";
echo "<td>" . "<input type='submit' name='delete' value='delete' /></td>";
echo "</tr>";
echo "</form>";
}
}
echo "</table>";
?>
<?php
$conn->close();
?>
</body>
</html>
答案 0 :(得分:13)
<强>问题:强>
我对一行中的特定单元格所做的更新会插入到数据库中。然而,发生了什么是在该单元格输入为0000-00-00之后的任何空白日期空间。
<强>解决方案:强>
根据您的评论,
数据库已将这些列设置为默认值
NULL
。唯一没有的是DateReceived
列。
我假设已经设置了Project
,Client
,LastName
和DateReceived
。因此,如果用户没有指定要更新的任何数据,解决方案是插入NULL
值,如下所示:
// your code
if (isset($_POST['update'])){
$project = trim($_POST['project']);
$client = trim($_POST['client']);
$lastname = trim($_POST['lastname']);
$datereceived = $_POST['datereceived'];
$UpdateQuery = "UPDATE Projects SET Project='{$project}', Client='{$client}', LastName='{$lastname}', DateReceived='{$datereceived}'";
if(empty($_POST['finalreviewdate'])){
$UpdateQuery .= ", FinalReviewDate = NULL";
}else{
$UpdateQuery .= ", FinalReviewDate = '{$_POST['finalreviewdate']}'";
}
if(empty($_POST['datedelivered'])){
$UpdateQuery .= ", DateDelivered = NULL";
}else{
$UpdateQuery .= ", DateDelivered = '{$_POST['datedelivered']}'";
}
if(empty($_POST['dateaccepted'])){
$UpdateQuery .= ", DateAccepted = NULL";
}else{
$UpdateQuery .= ", DateAccepted = '{$_POST['dateaccepted']}'";
}
$UpdateQuery .= " WHERE Project='{$_POST['hidden']}'";
mysqli_query($conn, $UpdateQuery);
};
// your code
这里是完整的代码:(tested
)
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['update'])){
$project = trim($_POST['project']);
$client = trim($_POST['client']);
$lastname = trim($_POST['lastname']);
$datereceived = $_POST['datereceived'];
$UpdateQuery = "UPDATE Projects SET Project='{$project}', Client='{$client}', LastName='{$lastname}', DateReceived='{$datereceived}'";
if(empty($_POST['finalreviewdate'])){
$UpdateQuery .= ", FinalReviewDate = NULL";
}else{
$UpdateQuery .= ", FinalReviewDate = '{$_POST['finalreviewdate']}'";
}
if(empty($_POST['datedelivered'])){
$UpdateQuery .= ", DateDelivered = NULL";
}else{
$UpdateQuery .= ", DateDelivered = '{$_POST['datedelivered']}'";
}
if(empty($_POST['dateaccepted'])){
$UpdateQuery .= ", DateAccepted = NULL";
}else{
$UpdateQuery .= ", DateAccepted = '{$_POST['dateaccepted']}'";
}
$UpdateQuery .= " WHERE Project='{$_POST['hidden']}'";
mysqli_query($conn, $UpdateQuery);
};
$sql = "SELECT * FROM Projects";
$result = $conn->query($sql);
echo "<table>
<tr>
<th>Project</th>
<th>Client</th>
<th>Last Name</th>
<th>Date Received</th>
<th>Final Review Date</th>
<th>Date Delivered</th>
<th>Date Accepted</th>
</tr>";
while($record = mysqli_fetch_array($result))
{
if ($result->num_rows > 0){
echo "<form action='process.php' method='post'>";
echo "<tr>";
echo "<td>" . "<input type='text' name='project' value='" . $record['Project'] . "' /></td>";
echo "<td>" . "<input type='text' name='client' value='" . $record['Client'] . "'/></td>";
echo "<td>" . "<input type='text' name='lastname' value='" . $record['LastName'] . "' /></td>";
echo "<td>" . "<input type='text' name='datereceived' value='" . $record['DateReceived'] . "' /></td>";
echo "<td>" . "<input type='text' name='finalreviewdate' value='" . $record['FinalReviewDate'] . "' /></td>";
echo "<td>" . "<input type='text' name='datedelivered' value='" . $record['DateDelivered'] . "' /></td>";
echo "<td>" . "<input type='text' name='dateaccepted' value='" . $record['DateAccepted'] . "' /></td>";
echo "<td>" . "<input type='hidden' name='hidden' value='" . $record['Project'] . "' /></td>";
echo "<td>" . "<input type='submit' name='update' value='update' /></td>";
echo "<td>" . "<input type='submit' name='delete' value='delete' /></td>";
echo "</tr>";
echo "</form>";
}
}
echo "</table>";
?>
<?php
$conn->close();
?>
</body>
</html>
答案 1 :(得分:5)
这可能会发生,因为在创建表时,您将列类型指定为 DATE ,并将值作为空字符串插入没有值的列。因此,在更新值时,其他列的值为0000-00-00。 DATE列不能将空字符串作为默认值。您可以根据数据类型here检查默认值。
您可以做的最好的事情是更改列以将默认值更改为 NULL ,并且在第一次向表中插入值时,为没有的列提供值 NULL 任何价值。因此,在更新时,这些列不会将值作为0000-00-00,它们的值将为NULL。
例如,考虑表名为测试,列名为 date1 , date2 , date3 和 date4 强>
CREATE TABLE test (date1 DATE DEFAULT NULL, date2 DATE DEFAULT NULL, date3 DATE DEFAULT NULL, date4 DATE DEFAULT NULL)
您还可以使用以下查询修改现有列
ALTER TABLE test MODIFY COLUMN date1 DATE DEFAULT NULL
并且当您第一次只有第一列的值插入值时,请像这样插入
INSERT INTO test VALUES ('2015-12-07', NULL, NULL, NULL)
所以稍后当你更新任何其他列时,只会更新该值,而其他列将不会取值。它们的值为 NULL 。
我测试了它,它对我有用。
答案 2 :(得分:5)
更改保留日期的字段的数据类型。然后,当您在更新时离开空字段时,如果您的数据库字段也为空,它将分配给'0000-00-00',否则它将保持不变。 现在更改数据库数据类型 - &gt;顶部的“结构”选项卡 - &gt;找到我们需要更改数据类型的行,然后单击该字段的更改操作。 - &gt;在下拉列表中选择类型数据。
答案 3 :(得分:2)
这是代码中的一小段代码,但是负责输入数据的部分。如果日期字段为空,则会获得指定值'' ( empty string )
,这有望阻止它们使用0000-00-00
进行更新。
if( isset( $_POST['update'] ) ){
/* For convenience, shorthand object notation for $_POST */
$pd=(object)$_POST;
/* Assign each parameter as a variable - using false or null as appropriate. There is some rudimentary filtering at least */
$project = isset( $pd->project ) && !empty( $pd->project ) ? strip_tags( filter_input( INPUT_POST, 'project', FILTER_SANITIZE_STRING ) ) : false;
$client = isset( $pd->client ) && !empty( $pd->client ) ? strip_tags( filter_input( INPUT_POST, 'client', FILTER_SANITIZE_STRING ) ) : false;
$lastname = isset( $pd->lastname ) && !empty( $pd->lastname ) ? strip_tags( filter_input( INPUT_POST, 'lastname', FILTER_SANITIZE_STRING ) ) : false;
$datereceived = isset( $pd->datereceived ) && !empty( $pd->datereceived ) ? strip_tags( filter_input( INPUT_POST, 'datereceived', FILTER_SANITIZE_STRING ) ) : false;
$finalreviewdate = isset( $pd->finalreviewdate ) && !empty( $pd->finalreviewdate ) ? strip_tags( filter_input( INPUT_POST, 'finalreviewdate', FILTER_SANITIZE_STRING ) ) : '';
$datedelivered = isset( $pd->datedelivered ) && !empty( $pd->datedelivered ) ? strip_tags( filter_input( INPUT_POST, 'datedelivered', FILTER_SANITIZE_STRING ) ) : '';
$dateaccepted = isset( $pd->dateaccepted ) && !empty( $pd->dateaccepted ) ? strip_tags( filter_input( INPUT_POST, 'dateaccepted', FILTER_SANITIZE_STRING ) ) : '';
$hidden = isset( $pd->hidden ) && !empty( $pd->hidden ) ? strip_tags( filter_input( INPUT_POST, 'hidden', FILTER_SANITIZE_STRING ) ) : false;
/* The sql MUST have these to be processed */
if( $project && $client && $hidden && $lastname && $datereceived ){
/* The various date fields that were being updated with 0000-00-00 have a default value of empty string in the above vars */
/* use the defined variables */
$sql = "update `projects` set
`project`='{$project}', `client`='{$client}', `lastname`='{$lastname}', `datereceived`='{$datereceived}',
`finalreviewdate`='{$finalreviewdate}', `datedelivered`='{$datedelivered}', `dateaccepted`='{$dateaccepted}'
where `project`='{$hidden}';";
/* run the query */
mysqli_query( $conn, $sql );
}
};
答案 4 :(得分:1)
如果您没有将任何数据输入数据时间字段,则默认为0000-00-00
如果要更改默认值,可以在表格设置中使用。
或者您可以通过执行标准验证检查确保数据始终进入数据库
要回答您的问题,请检查表格和字段,检查它们的类型。我认为那些空白的不是日期时间,因为默认情况下设置为0000-00-00。
只需检查并确保它们都在同一设置上。