I already have a function where you can insert data to the database which then prints out all the "item_name"s on the index with their respective "item_name" as url (example.com/example_name)
To dumb the code down lets say item_name is the only row I have, and I want each item to have it's own page that says "My item: example_name". Here is the code I'm currently using for printing the list of items:
<?php
$sql = "SELECT * FROM items ORDER BY item_name DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '
<p>
<a href="/' . $row["item_name"] . '"> ' . $row["item_name"] . ' </a>
</p> ';
}
} else {
echo "0 results";
}
$conn->close();
?>
What is the simplest way to go about doint this? Security is no big issue here. Sorry if duplicate question, I have searched for over 2 hours now without finding anything directly applying to my issue. I am pretty new to programming in general so please don't go very technical on me. Thank you.
If what you want is to create a page while reading the contents of your DB, you'd have to first read the name from the DB (you already did that), and then create/write the file in each iteration (within the loop). That'd create a file per name.
Here you have examples on how to create/write a file easily using php. Of course, there are many ways of doing this.
Be sure that the user with which you're running the script has enough permissions to write on the server. Hope it helps.
I would approach this by creating a generic template page in php. Lay the template out so it has all the sections you need and have a variable for each section to hold the data. Example:
<?php
extract($_POST); // turn your post vars into variables
echo "<h1>$product_name</h1";
echo "<h2>Description</h2>";
echo $description;
etc.
You can give this page any name. let's use product.php. Now, in the code where you get the results from the query, make the links point to product.php. Every time the link is called, it will create an instance of that page with the variables. Your link should look like this.
while($row = $result->fetch_assoc()) {
echo '
<p>
<form action="product.php">
<input type="text" name="productname" value="'.$row['product_name'].'" />
<input type="submit" value="link" class="button" />
</form>
</p> ';
}
Add any of the other values from $row. When the user clicks the link they will go to the product.php page where the values will be displayed. This is not a full working example, but should get you thinking in the right direction. Also, this code is subject to sql injection, so you need to tighten it up.
When you create your query