使用php从mysql中调出内容

时间:2013-06-06 20:45:41

标签: php mysql

我正在尝试为网站创建一个简单的管理页面。从下面的表格中的内容,将填充在html doc

中的p标签内

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

我是sql的新手,任何帮助都将不胜感激。感谢

4 个答案:

答案 0 :(得分:0)

下面的代码应该为您检索数据。您需要将index.html文件更改为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>

这只会检索一行,所以如果你有多行(即多个页面),你需要添加一些代码来告诉mysql要检索哪一行。

答案 1 :(得分:0)

在你开始工作之后,你应该立即看看前面提到的mysql_real_escape_string

答案 2 :(得分:-1)

试试这个:
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);

答案 3 :(得分:-1)

当你在PHP中进行一些MySQL查询时,你必须将DB的table- \ rownames设置为这些符号。并且“”中的数组必须打包成{} 试试这个

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