douzi8127 2015-06-17 21:07
浏览 78
已采纳

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

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

<!-- 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

<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>
  • 写回答

1条回答 默认 最新

  • dongshan7708 2015-06-17 21:14
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划