How can PHP be used to create a dynamic and user-friendly categorization system for organizing submitted links on a website?
To create a dynamic and user-friendly categorization system for organizing submitted links on a website using PHP, you can utilize a combination of HTML forms for user input, PHP for processing the form data, and a database to store and retrieve the categorized links.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "links_db";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Process form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$link = $_POST['link'];
$category = $_POST['category'];
// Insert the submitted link into the database with the selected category
$sql = "INSERT INTO links (link, category) VALUES ('$link', '$category')";
if ($conn->query($sql) === TRUE) {
echo "Link added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// Display form for users to submit links
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">
<input type="text" name="link" placeholder="Enter link URL">
<select name="category">
<option value="tech">Tech</option>
<option value="food">Food</option>
<option value="travel">Travel</option>
</select>
<input type="submit" value="Submit">
</form>';
// Close the database connection
$conn->close();
?>