php - 如何从 MySQL 数据库中获取数据并将其插入到文本中

标签 php mysql

我一直在开发我的网站 - 这是一家出售不同商品的商店(这也是我的第一个网站)。我刚刚完成网站的 HTML/CSS 部分,但我想让它更动态、更容易编辑,而不必一次更改多个页面。我认为我可以做到这一点并且还可以更轻松地实现网站搜索的一种方法是将商品的名称和价格保存在数据库中,然后让 PHP 代码检索名称和价格并将其插入商品图像下方。不幸的是,我完全不知道该怎么做。我试过寻找其他有类似问题的人,但他们似乎都不是我要找的人。我将在下面放置我的 PHP 代码,然后放置一些 html 代码来显示我想放置信息的位置。

<?php
$servername = "localhost";
$username = "root";
$password = "testpw";
$dbname = "testdb";

$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) {
}
$sql = "SELECT id, name, price FROM items";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo " " .$row["id"]. " " .$row["name"]. " " .$row["price"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();

?>

这就是我用来获取每个项目信息的 PHP 代码。但问题是它获取每个项目的信息并立即显示。我希望代码检查项目的 ID,然后检索有关它的统计信息并显示它们。这意味着如果我想重新排列商店中的元素,我只需要更改 ID——这让我更容易操纵商店和其中的元素。以下是项目的 HTML 代码。

<div id="images">
<figure>
    <img class="gunimage" id="(THIS IS THE ID USED TO GET THE INFO)" src="idk.png" width="225" height="190" alt="">
    <figcaption><h3>Name: (INSERT NAME HERE USING PHP) </h3></figcaption>
    <figcaption><h5>Price: (INSERT OTHER PRICE HERE)/month</h5></figcaption>
    <figcaption><div id="tfbutton5"><form action="buy.html"><input   type="submit" value="Rent" class="tfbutton5"></form></figcaption>
</div>
</figure>
</div>

我标记了我希望信息到达的位置,并且我想要 ID 的东西,以便 PHP 代码知道它是什么项目。我知道这两个代码是分开工作的,因为我已经对它们进行了多次测试。抱歉,如果我的问题真的很长,我想尽可能具体,以便清楚我的问题是什么。如果我的代码需要大量更改才能适用于我所描述的情况,请原谅,因为我只能弄清楚如何制作列出每个项目信息的东西。任何帮助,将不胜感激。谢谢。

最佳答案

如果你想做更多的事情,你可以放入一个名为
的文件 'connection.php' 或更具描述性的内容

  try { 
  $servername = 'localhost';
 $username = 'root';
 $password = 'testpw';
 $dbname = 'testdb';
 $db = new mysqli($servername, $username, $password, $dbname);
 } catch(Exception $e) {
   echo $e->error_list;
   exit;
 }
function execute_query($con, $query, $variables) {
$stmt = $con->prepare($query);
$stmt->execute($variables);
return $stmt;
} 

如果您有多个函数,您可以将这些顶部部分拆分为 connection.php,而将函数拆分为 function.php 文件。

include ('connection.php');
$id_number = $_GET['id'];
# !! NOTICE if grabbing | my understanding another page | 
// filter if grabbing from GLOBAL Variable $_GET 
$id = filter_var($id_number, FILTER_SANITIZE_NUMBER_INT); //take out 
if not grabbing from outside source.
$con = $db;
try {
   $query = 'SELECT t.id as id , t.name as name, t.price as price
   FROM items as t
   WHERE t.id = :id';
$variables[':id'] = $id;
  $items = execute_query($con, $query, $variables);
}catch(mysqli_error_list $e) {
  echo $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
 </head>
<body>
<div id="images"><!-- i am guessing you want it within this div-->
<?php if(isset($item)) { ?>
 <?php foreach ( $items as $item) { ?> 
 <figure>
      <img class="gunimage" id="<?php echo $item['id']; ?>"
 src="idk.png" width="225" height="190" alt="<?php echo $item['name']; 
?>">
  <figcaption><h3>Name: <?php echo $item['name']; ?> </h3>
 </figcaption>
  <figcaption><h5>Price: <?php echo $item['price']; ?> </h5>
 </figcaption>
  <figcaption>
  <!-- instead of a id tfbutton5 needs to be a class otherwise your
  going to have an issue here when it loops, you will have an error-->
  <div class="tfbutton5"><form action="buy.html"><input
    type="submit" value="Rent" class="tfbutton5"></form>
  <!--also, not sure why you have an Id and a class  named exactly 
 identical but it is bad practice. sorry do not do it. -->
   </figcaption>
     </div>
     </figure>
  <?php } ?>
 <?php } ?>
 </div>
</body>
</html> 

如果您想要图像源,则需要将其输入数据库并将项目表连接到图像表,该表具有 say..rows id、image 和 item-id。然后使用项目 ID 作为项目和图像之间的点

关于php - 如何从 MySQL 数据库中获取数据并将其插入到文本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38515034/

相关文章:

php - "sudo -u www-data git pull"凭证保存?

php - 在 PHP 中向 MYSQL 数据库添加记录不起作用

mysql - 在 foreach Node.js Promise 中查询

php - 为什么我的 php 代码不能执行任何操作?

php - 包含文件和 https 停止我的 css 工作

php - 从 mysql 列表创建列

php - 不理解这种 isset 行为

mysql向一列插入多列数据

mysql - 使用复杂谓词优化 MySQL 查询

mysql - 从一个输出中的两个不同表查询每个用户的调用统计信息