当前位置:  开发笔记 > 编程语言 > 正文

sql UPDATES更新更改列之后的行中的每一列

如何解决《sqlUPDATES更新更改列之后的行中的每一列》经验,为你挑选了3个好方法。

我正在使用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相当新,所以我知道我可能会错过一些简单的东西.任何帮助解决这个问题将不胜感激.

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 "";

while($record = mysqli_fetch_array($result))
{
if ($result->num_rows > 0){

echo "";
echo ""; 
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
}
echo "
Project Client Last Name Date Received Final Review Date Date Delivered Date Accepted
" . "" . "" . "" . "" . "" . "" . "" . "" . "" . "
"; ?> close(); ?>

Rajdeep Paul.. 13

问题:

我对一行中的特定单元格所做的更新会插入到数据库中.然而,正在发生的是在该单元格输入as 0000-00-00之后出现的任何空白日期空间.

解:

从你的评论,

数据库已将这些列设置为默认值NULL.唯一没有的是DateReceived列.

我假设Project,Client,LastNameDateReceived已设置.因此,解决方案是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)

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 "";

while($record = mysqli_fetch_array($result))
{
if ($result->num_rows > 0){

echo "";
echo ""; 
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
}
echo "
Project Client Last Name Date Received Final Review Date Date Delivered Date Accepted
" . "" . "" . "" . "" . "" . "" . "" . "" . "" . "
"; ?> close(); ?>


小智.. 5

这可能会发生,因为在创建表时,您将列类型指定为DATE,并将值作为空字符串插入没有值的列.因此,在更新值时,其他列的值为0000-00-00.DATE列不能将空字符串作为默认值.您可以在此处根据数据类型检查默认值.

您可以做的最好的事情是更改列以将默认值设置为NULL,并且在第一次向表中插入值时,对于没有任何值的列,将值设置为NULL.因此在更新时,这些列不会将值作为0000-00-00,它们的值将为NULL.

例如,考虑表名是test,列名是date1,date2,date3date4

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)

因此,稍后当您更新任何其他列时,只会更新该值,而其他列将不会获取值作为0000-00-00.它们的值为NULL.

我测试了这个,它对我有用.



1> Rajdeep Paul..:

问题:

我对一行中的特定单元格所做的更新会插入到数据库中.然而,正在发生的是在该单元格输入as 0000-00-00之后出现的任何空白日期空间.

解:

从你的评论,

数据库已将这些列设置为默认值NULL.唯一没有的是DateReceived列.

我假设Project,Client,LastNameDateReceived已设置.因此,解决方案是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)

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 "";

while($record = mysqli_fetch_array($result))
{
if ($result->num_rows > 0){

echo "";
echo ""; 
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";
}
}
echo "
Project Client Last Name Date Received Final Review Date Date Delivered Date Accepted
" . "" . "" . "" . "" . "" . "" . "" . "" . "" . "
"; ?> close(); ?>



2> 小智..:

这可能会发生,因为在创建表时,您将列类型指定为DATE,并将值作为空字符串插入没有值的列.因此,在更新值时,其他列的值为0000-00-00.DATE列不能将空字符串作为默认值.您可以在此处根据数据类型检查默认值.

您可以做的最好的事情是更改列以将默认值设置为NULL,并且在第一次向表中插入值时,对于没有任何值的列,将值设置为NULL.因此在更新时,这些列不会将值作为0000-00-00,它们的值将为NULL.

例如,考虑表名是test,列名是date1,date2,date3date4

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)

因此,稍后当您更新任何其他列时,只会更新该值,而其他列将不会获取值作为0000-00-00.它们的值为NULL.

我测试了这个,它对我有用.



3> 小智..:

更改保留日期的字段的数据类型.然后,当您在更新时离开空字段时,如果您的数据库字段也为空,它将分配给'0000-00-00',否则它将保持不变.现在更改数据库数据类型 - > oto"结构"选项卡在顶部 - >找到我们需要更改数据类型的行,然后单击该字段的更改操作. - >在下拉列表中选择类型数据.

推荐阅读
家具销售_903
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有