简体   繁体   English

在html输入字段(PDO)中显示MySQL表数据

[英]Display MySQL table data in html input field (PDO)

I want this form to load any data found in the "header" and "summary" columns in my database into the input fields for ease of editing. 我希望此表单将在数据库的“标题”和“摘要”列中找到的所有数据加载到输入字段中,以便于编辑。 Then, after the user submits the form, the contents are dumped back into the DB and the new "values" are shown in the form. 然后,在用户提交表单之后,将内容转储回数据库,并在表单中显示新的“值”。 My issue is that the "dumping" part works fine, however when I refresh the page or navigate back to the form, no data is displayed and the data in the table has been replaced by white space...How do I fix this? 我的问题是“转储”部分工作正常,但是当我刷新页面或导航回表单时,没有显示任何数据,并且表中的数据已被空格替换...如何解决此问题?

// Form PHP //表格PHP

<!-- Process POST Data and dump into DB -->
<?php 

    $header1 = $_POST['header1'];
    $summary1 = $_POST['summary1'];

    $handler = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password');
    $handler->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_EXCEPTION);

    $sql = "UPDATE myTable SET header='$header1', summary='$summary1' WHERE id=1";

    try {

        $stmt = $handler->prepare($sql);

        $stmt->execute();

    } catch(PDOException $e) {
        echo $e->getMessage();
        die();
    }

    $handler = null;
?>
<!-- Get mysql data -->
<?php
    $handler = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password');
    $handler->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_EXCEPTION);

    $fetchData = $handler->prepare("SELECT * FROM newsletters WHERE id=1");
    $fetchData->execute();

    $data = $fetchData->fetchAll();
?>

<!-- form -->

// Form HTML //表单HTML

<form action="index.php" method="POST">
<input type="text" name="header1" value="<?php foreach ($data as $Data){echo $Data['header'];} ?>">
<textarea name="summary1" rows="5" value="<?php foreach ($data as $Data){echo $Data['summary'];} ?>"></textarea>
<input type="submit" name="submit1" value="Submit">
</form>

Assuming you're using the same script to display the form and process the submission, you need to check whether the form was submitted before updating the DB. 假设您使用相同的脚本来显示表单并处理提交,则需要在更新数据库之前检查表单是否已提交。

if (isset($_POST['header1'], $_POST['summary1'])) {
    $header1 = $_POST['header1'];
    $summary1 = $_POST['summary1'];

    $handler = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password');
    $handler->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_EXCEPTION);

    $sql = "UPDATE myTable SET header= :header , summary= :summary WHERE id=1";

    try {

        $stmt = $handler->prepare($sql);
        $stmt->execute(array(':header' => $header1, ':summary' => $summary1));

    } catch(PDOException $e) {
        echo $e->getMessage();
        die();
    }

    $handler = null;
}

I've also shown how to use parameters in the prepared statement to prevent SQL injection. 我还展示了如何在准备好的语句中使用参数来防止SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM