PDO将输入值附加到现有值

时间:2018-06-08 02:03:52

标签: php mysql pdo

我在将新发布的数据附加到现有数据时遇到问题。

现在,当点击提交按钮时,它会覆盖'notes'的现有值,仅包含$ header和新的$ _POST [“notes”]值。

我希望它能做的是: $ header + $ _POST [“notes”] + $ notes(现有数据)

这是我目前的代码:

<?php
    // Database connection file
    include_once("../config.php");

    // Get id from url
    $id = $_GET['id'];

    // Selecting data associated with this id
    $sql = "SELECT * FROM testtable WHERE id=:id";
    $query = $dbConn->prepare($sql);
    $query->execute(array(':id' => $id));

    while($row = $query->fetch(PDO::FETCH_ASSOC)){
        $notes = $row['notes'];
    }
?>

<?php
        // Create Header
        $header = "----- Updated on " . date("Y-m-d") . " at " . date("h:i:sa") . " ----- <br>";
        // Current note data
        $currentnotes = $notes;
        // Append Header + Post + Current Data
        $appendednotes = $header;
        $appendednotes .= $_POST["notes"];
        $appendednotes .= "<br>";
        $appendednotes .= $currentnotes;

    if(isset($_POST['update'])){
        $id = $_POST['id'];
        $notes=$appendednotes;

        // Update the table
        $sql = "UPDATE testtable SET
            notes=:notes
        WHERE id=:id";

        $query = $dbConn->prepare($sql);
    
        $query->bindparam(':id', $id);
        $query->bindparam(':notes', $notes);

        $query->execute();

        // Redirect to the display page
        header("Location: index.php");
        
    }
?>

这是找到我想要做的最接近的例子,但我不确定如何调整它: php post to variable and then append to txt

1 个答案:

答案 0 :(得分:0)

感谢Paul,我将我的代码更新到了这个并且它有效!

// Update the table
$sql = "UPDATE survey SET
    notes=concat(:notes, notes)
WHERE id=:id";