So I've recently been trying to make a website. The website I'm making has multiple pages that are basically the same except different lay-outs of items. I want to reduce the painstaking time it takes to change all of these pages at the same time by having the name and price of the item taken from a database where info about all of the items is stored (including a special ID for each item). The basic idea is that I only had to specify the ID of an item and the database would get the information by itself. I already have the HTML and PHP basic coding done but I don't know how to make the PHP code into a function and then make it runnable inside the command. I'll include the PHP code below:
<?php
$servername = "localhost";
$username = "root";
$password = "testpw";
$dbname = "testdb";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) {
}
$sql = "SELECT name, price FROM items WHERE id='(Get ID from HTML code here)'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo " " .$row["name"]. " " .$row["price"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I already know this code writes out the specifics for the ID I put where (Get ID from HTML code here) is, I just need to know how to import the ID in the HTML into here, and then be able to put the function into the code.
Here's the HTML:
<div id="images">
<figure>
<img class="image" id=(Item ID here) src="idk.png" width="225" height="190" alt="">
<figcaption><h3>Name: (PHP Function Here) </h3></figcaption>
<figcaption><h5>Price: (PHP Function Here)/month</h5></figcaption>
<figcaption><div id="tfbutton5"><form action="buy.html"><input type="submit" value="Rent" class="tfbutton5"></form></figcaption>
</div>
</figure>
</div>
Any solution would be appreciated as this is a big road block currently in my website construction. Even if it isn't the answer I was looking for. Thank you.