I'm trying to make the meta title, meta description and meta keywords dynamic in my application, so that each page contains its own data.
First, I inserted all the data in the database in a table called metatags.
Then, I would like to display this data from header.php.
Here is my code:
<?php include "admin/functions.php"; ?>
<?php
  $url = basename($_SERVER['REQUEST_URI']);
  // get meta tag
  $metaqry = "SELECT * FROM metatags WHERE metatag_url = $url";
  $metares = mysqli_query($connection, $metaqry);
  $metadata = mysqli_fetch_assoc($metares);
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title><?php echo $metadata['metatag_title']; ?></title>
    <meta
      name="description"
      content="<?php echo $metadata['metatag_description']; ?>"
    />
    <meta
      name="keywords"
      content="<?php echo $metadata['metatag_keywords']; ?>"
    />
    <!-- <meta name="viewport" content="width=device-width, initial-scale=1" /> -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link
      rel="shortcut icon"
      type="image/x-icon"
      href="/projects/online-projects/clique-digitale-php/assets/img/favicon.ico"
    />
    <!-- Plugin css -->
    <!-- <link rel="stylesheet" href="assets/css/vendor/bootstrap.min.css"> -->
    <!-- Custom Style CSS -->
    <link rel="stylesheet" href="/projects/online-projects/clique-digitale-php/assets/css/style.css" />
  </head>
However, when I'm going to index.php, I'm getting this error:
Fatal error: Uncaught mysqli_sql_exception: Unknown column 'index.php' in 'where clause' in C:\xampp\htdocs\projects\online-projects\clique-digitale-php\includes\header.php:8 Stack trace: #0 C:\xampp\htdocs\projects\online-projects\clique-digitale-php\includes\header.php(8): mysqli_query(Object(mysqli), 'SELECT * FROM m...') #1 C:\xampp\htdocs\projects\online-projects\clique-digitale-php\index.php(2): include('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\projects\online-projects\clique-digitale-php\includes\header.php on line 8
Edit: I updated my code and used PDO:
<?php
  $url = basename($_SERVER['REQUEST_URI']);
  $metares = $connection->prepare("
    SELECT * FROM metatags
    INNER JOIN site_pages on site_pages.page_id = metatags.metatag_url
    WHERE site_pages.page_url = :url
  ");
  $metares->bindValue(':url', $url);
  $metares->execute([
    'url' => $url
  ]);
  $metarow = $metares->rowCount();
  $metadata = $metares->fetch(PDO::FETCH_ASSOC);
  $metatag_title = '';
  $metatag_description = '';
  $metatag_keywords = '';
  if($metarow > 0){
    $metatag_title = $metadata['metatag_title'];
    $metatag_description = $metadata['metatag_description'];
    $metatag_keywords = $metadata['metatag_keywords'];
    
  } else {
    // You can fetch by default index.php from database
    $metatag_title = 'Agence Digitale - Clique Digitale';
    $metatag_description = 'Donnez à votre business la valeur qu\'il mérite !';
    $metatag_keywords = 'Agence Digitale';
  }
?>
