douzi8127 2015-06-17 13: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

  1. <!-- Process POST Data and dump into DB -->
  2. <?php
  3. $header1 = $_POST['header1'];
  4. $summary1 = $_POST['summary1'];
  5. $handler = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password');
  6. $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  7. $sql = "UPDATE myTable SET header='$header1', summary='$summary1' WHERE id=1";
  8. try {
  9. $stmt = $handler->prepare($sql);
  10. $stmt->execute();
  11. } catch(PDOException $e) {
  12. echo $e->getMessage();
  13. die();
  14. }
  15. $handler = null;
  16. ?>
  17. <!-- Get mysql data -->
  18. <?php
  19. $handler = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password');
  20. $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  21. $fetchData = $handler->prepare("SELECT * FROM newsletters WHERE id=1");
  22. $fetchData->execute();
  23. $data = $fetchData->fetchAll();
  24. ?>
  25. <!-- form -->

// Form HTML

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

展开全部

  • 写回答

1条回答 默认 最新

  • dongshan7708 2015-06-17 13: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.

    1. if (isset($_POST['header1'], $_POST['summary1'])) {
    2. $header1 = $_POST['header1'];
    3. $summary1 = $_POST['summary1'];
    4. $handler = new PDO('mysql:host=localhost;dbname=myDB', 'username', 'password');
    5. $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    6. $sql = "UPDATE myTable SET header= :header , summary= :summary WHERE id=1";
    7. try {
    8. $stmt = $handler->prepare($sql);
    9. $stmt->execute(array(':header' => $header1, ':summary' => $summary1));
    10. } catch(PDOException $e) {
    11. echo $e->getMessage();
    12. die();
    13. }
    14. $handler = null;
    15. }

    I've also shown how to use parameters in the prepared statement to prevent SQL injection.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部