【问题标题】:How to UPDATE a column without affecting the others?如何在不影响其他列的情况下更新列?
【发布时间】:2018-11-21 16:48:06
【问题描述】:

我有这段代码来更新我的数据库中的一条记录,通常我只会更新一列,例如:在数组上我列出了 6 列,我只想更新列keywords,其他列应该不影响我。

使用此代码,我可以更新我的记录,但不能更新特定列,因为如果我在 input name="keywords" 上写了一些东西,而在其他输入上什么也不写,这些输入中的列值将被我的数据库上的无值替换.

我的问题是:

  1. 如何在不影响其他列的情况下更新列?

    if(isset($_POST["updateBTN"])){    
      $insert_data = array(
    
        ':title'            => $_POST['title'],
        ':keywords'         => $_POST['keywords'],
        ':img'              => $_POST['img'],
        ':widht'            => $_POST['widht'],
        ':status'           => $_POST['status'],
        ':name'             => $_POST['name'],
        ':height'           => $_POST['height']
    
      );
    
    $query = "UPDATE table SET keywords = :keywords, img = :img, widht = :widht, status = :status, name = :name, height = :height WHERE title = :title";
    $statement = $conn->prepare($query);
    $statement->execute($insert_data);
    
    }
    

html:

<form  method="post">
<div>
    <input type="text" name="title"> 
    <span data-placeholder="Title"></span>          
</div>
<div>
    <input type="text" name="keywords"> 
    <span data-placeholder="keywords"></span>          
</div>
.
.
.
<button type="submit" name="updateBTN">Send</button>
</form>

【问题讨论】:

    标签: php sql forms post sql-update


    【解决方案1】:

    您可以更改 SQL 查询以仅更新您拥有数据的列,或使用从数据库中检索到的值填充 $insert_data 中的任何未定义值。第一个可能是一个更好的主意,但第二个更容易实现。

    我还没有写过一点 PHP,但可能是这样的:

        if(isset($_POST["updateBTN"])) {
          $query = "SELECT title, keywords, img, widht, staus, name, height FROM table WHERE title = :title";
          $stmt = $conn->prepare($query);
          $stmt->execute(array(":title" => $_POST['title']));
          $row = $stmt->fetch(PDO::FETCH_ASSOC);
    
          $insert_data = array(
            ':title'            => $_POST['title'] ? $_POST['title'] : $row['title'],
            ':keywords'         => $_POST['keywords'] ? $_POST['keywords'] : $row['keywords'],
            ...
          );
    
          $query = "UPDATE table SET keywords = :keywords, img = :img, widht = :widht, status = :status, name = :name, height = :height WHERE title = :title";
          $statement = $conn->prepare($query);
          $statement->execute($insert_data);
        }
    

    【讨论】:

    • 我收到一个错误Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2031 in ... PDO-&gt;query('SELECT title, k...')。人们不以这种方式更新记录?我觉得这是一个简单的问题。
    • 确保仔细检查我的代码;就像我说的,我很长时间没有编写 PHP 代码了(所以再给你一个回应;我不以这种方式更新记录。我通常使用 ORM,或者带有 ORM 的框架,例如 Django,Ruby on Rails、Phoenix 等)。
    • 我更新了答案;让我知道它是否有效,这样我就可以晚上睡觉了:)。
    • 遗憾的是:/ 我收到了这个错误:Notice: Trying to get property 'num_rows' of non-object 和未定义变量 $row 到数组中。我几乎要放弃了。我想我会在数据库上使用纯 SQL 来更新我的记录。无论如何,谢谢。
    • 又改了;也许您可以尝试调试错误,因为您有系统设置?
    【解决方案2】:

    假设您不想将任何列设置为NULL 值,您可以使用COALESCE()

    UPDATE table
        SET keywords = COALESCE(:keywords, keywords),
            img = COALESCE(:img, img),
            widht = COALESCE(:widht, widht),
            status = COALESCE(:status, status),
            name = COALESCE(:name, name),
            height = COALESCE(:height height)
    WHERE title = :title;
    

    【讨论】:

    • 列没有获得 NULL 值,我只想更新一列,当我仅在单个输入上设置值时,其他列将被替换为无值。我尝试了您的代码,但结果相同。
    • @515948453225 。 . . “无”不是数据库概念。你用的是什么数据库? COALESCE() 应该可以工作,假设缺失值作为 NULL 传入。
    • 我正在使用 phpmyadmin。使用none value,我的意思是列上的值保持空白,而不是NULL,只是没有数据的空白。
    【解决方案3】:

    看看这是否有效

    UPDATE table
    SET keywords = :keywords,
        img = CASE WHEN img IS NOT NULL THEN img ELSE :img END,
        widht = CASE WHEN widht IS NOT NULL THEN widt ELSE :widht END,
        status = CASE WHEN status IS NOT NULL THEN status ELSE :status END,
        name = CASE WHEN name IS NOT NULL name ELSE :name END,
        height = CASE WHEN height IS NOT NULL THEN height ELSE :height END
     WHERE title = :title;
    

    【讨论】:

    • 不,它甚至没有更新我的记录:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 2021-12-12
    • 2019-08-25
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多