Create new page using link php dynamically
Here is an example of how you can create a new page using a link in PHP dynamically:
<?php
// Check if the user has clicked the link
if (isset($_GET['create_page'])) {
// Get the page title and content from the user
$pageTitle = $_GET['page_title'];
$pageContent = $_GET['page_content'];
// Create a new page
$pageId = createPage($pageTitle, $pageContent);
// Redirect the user to the new page
header('Location: page.php?id='. $pageId);
exit;
}
// Function to create a new page
function createPage($pageTitle, $pageContent) {
// Connect to the database
$db = mysqli_connect('localhost', 'username', 'password', 'database');
// Check connection
if (!$db) {
die('Could not connect: '. mysqli_error($db));
}
// Create a new page
$query = "INSERT INTO pages (title, content) VALUES ('$pageTitle', '$pageContent')";
mysqli_query($db, $query);
// Get the ID of the new page
$pageId = mysqli_insert_id($db);
// Close the database connection
mysqli_close($db);
return $pageId;
}
// Display the link to create a new page
echo '<a rel="nofollow" target="_blank" href="?create_page=true">Create a new page</a>';
?>
This code checks if the user has clicked the link to create a new page by checking if the create_page
variable is set in the URL. If it is, it gets the page title and content from the user, creates a new page in the database, and redirects the user to the new page.
The createPage
function connects to the database, creates a new page, and gets the ID of the new page. It then closes the database connection and returns the ID of the new page.
Finally, the code displays a link to create a new page.
You can also use a more secure way to create a new page by using a form and validating the input data before creating the new page.
You can also use a framework like Laravel or CodeIgniter to create a new page, they have built-in functions to create and manage pages.
Please note that this is a basic example, you should consider security and validation when creating a new page.
Also, you should consider using prepared statements to prevent SQL injection.
You can also use a template engine like Twig or Smarty to render the page content.