dougan0529 2013-06-06 20:45
浏览 23
已采纳

使用php从mysql中调出内容[关闭]

I'm attempting to create a simple admin page for a website. Where the content from the form below, will populate within the p tags in the html doc

<html>
    <body>

    <form action="write.php" method="post">
    Site Description: <input type="text" name="Description">
    <input type="submit">
    </form>

    </body>
    </html> 

write.php

    <?php
$con=mysqli_connect("BlahBlah","website","password","db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO Site (Description)
VALUES
('$_POST[Description]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?> 

index.html

<html><head></head><body>
<div id="description><h1>Title</h1>

<p class="body">INFORMATION FROM FORM DISPLAYED HERE</p>

</body></html>

I'm new to sql, any help will be appreciated. Thanks

  • 写回答

4条回答 默认 最新

  • dragonsun00000 2013-06-06 20:57
    关注

    The code below should retrieve the data for you. You will need to change your index.html file to index.php

    <?php
    $con=mysqli_connect("BlahBlah","website","password","db");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    
    $sql="SELECT `Description` FROM SITE";
    $myContent = mysql_query($sql, $con) or die(mysql_error());
    $row_myContent = mysql_fetch_assoc($myContent);
    
    mysqli_close($con);
    ?>
    <html><head></head><body>
    <div id="description">
    <h1>Title</h1>
    <p class="body"><?php echo $row_myContent['Description']; ?></p>
    </div>
    </body></html>
    

    This will only retrieve a single row, so if you have multiple rows (ie multiple pages) you will need to add some more code to tell mysql which row to retrieve.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法