I have a problem here at ()

Joined
Dec 30, 2024
Messages
1
Reaction score
0
Help needed! I have made a comment where I need help

Code:
<!--Revising forum/index.php-->
<?php
!session_start();

$emma = "emma";

// Kontrollera om användaren är inloggad
if (isset($_SESSION["user_id"])) { // WO
    echo $sql;
    exit;
}
?>

<html id="wholly">
<head id="swollyness">
    <title>Forum Board</title>
    <style id='newsner' id="krejs">
        .iBo {
            background-color: #d4edda--; /* Grön bakgrund */
            padding: 10px;
            margin: 5px 0;
            border-radius: 5px;
            border: 1px solid #a9dfbf;
            font-family: inherit--;
        }
    </style>
    <link rel="stylesheet" href="css/styles.css"/>
</head>
<body>
    <h1>Forum Board</h1>
    <?php if (): ?> <!--Here I have a problem-->
        <span style="background: yellow !important;">Under konstruktion</span>
        <h2>Skicka ett meddelande!:</h2>
        <form method="get" action="index.php">
            <textarea name="iBo" rows="4" cols="50"></textarea><br>
            <input type="submit" value="Skicka!">
        </form>
    <?php endif; ?>
    <h2>Meddelanden</h2>
    <ul>
<a href='register.php'>Registrera konto</a> || <?= htmlspecialchars($loggedin); ?>
    <?=
    include 'db-connect.php';
    include 'functions.php';

    $sql = "SELECT id, iBo, created_At FROM forum_posts ORDER BY created_At DESC";
    $result = $conn->query($sql);

    if ($result && $result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "<li class=\"iBo\">" . htmlspecialchars(format_id($row["id"])) . ": " . htmlspecialchars($row["created_At"]) . " - " . htmlspecialchars($row["iBo"]) . "</li>";
        }
    } else {
        echo "<li>Ingenting här.</li>";
    }
    $conn->close();
    ?>
    </ul>
</body>
</html>
 
Joined
Jul 4, 2023
Messages
548
Reaction score
71
No offense, but the code is a bit chaotic. Try writing it like this, for example.

config.php
PHP:
<?php
  return [ 'is_under_construction' => true ];
?>

under_construction.php
PHP:
<!doctype html>
<html lang="sv">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Under konstruktion</title>

    <link rel="stylesheet" href="css/styles.css"/>
    <style>
      .under-construction {
        background-color: lightyellow;
        padding: 1rem;
        color: darkred;
        font-size: 1.5rem;
      }
      h3 {
        margin: 1rem 0 .5rem 0;
      }
      .iBo {
        background-color: #d4edda;
        padding: .5rem;
        margin: .5rem 0;
        border-radius: .25rem;
        border: 2px solid #a3d8a0;
        font-family: inherit;
        font-size: 1rem;
        resize: vertical;
      }
      textarea {
        display: block;
      }
      button {
        background-color: #d4edda;
        border-radius: .25rem;
        border: 2px solid #a3d8a0;
      }
    </style>
  </head>
  <body>
    <h1 class="under-construction">
      Sidan under uppbyggnad - Page under construction
    </h1>
    <h3>Skicka ett meddelande:</h3>
    <form action="post_message.php" method="post">
      <textarea name="iBo" class="iBo" rows="4" cols="50"></textarea>
      <button type="submit">Skicka</button>
    </form>    
  </body>
</html>

index.php
PHP:
<?php
  session_start();
 
  $config = require_once('config.php');

  if ($config['is_under_construction']) {
    header('HTTP/1.1 503 Service Unavailable');
    include 'under_construction.php';
    exit;
  }
?>
<!doctype html>
<html lang="sv">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your website name</title>

    <link rel="stylesheet" href="css/styles.css">
    <style>
      html,
      body {
        margin: 0;
        padding: .5rem;
      }
      header ul {
        display: flex;
        list-style: none;
        justify-content: space-between;
        margin: 0;
        padding: .5rem 1rem;
      }
      header .logo { /* for demonstration purposes */
        background-image: url('https://png.pngtree.com/png-clipart/20190604/original/pngtree-creative-company-logo-png-image_1197025.jpg');
        width: 50px;
        height: 50px;
        background-repeat: no-repeat;
        background-size: cover;
      }
      header .logo a {
        display: block;
        width: 100%;
        height: 100%;     
      }
      main ul {
        list-style: none;
        padding: 0;
      }
      main li.iBo {
        background-color: #d4edda;
        padding: 10px;
        margin: 5px 0;
        border-radius: 5px;
        border: 1px solid #a9dfbf;
        font-family: inherit;
      }
    </style>
  </head>
  <body>

    <header>
      <ul>
        <li class="logo">
          <a href="index.php"></a>
        </li>
        <li>
          <?php if (! empty($_SESSION["user_logged_in"])): ?>
            Välkommen: <a href="user_account.php"><?php echo $_SESSION["user_logged_name"]; ?></a>
          <?php else: ?>
            <a href="register.php">Registrera konto</a> ||
            <a href="login.php">Logga in</a>
          <?php endif; ?>
        </li>
      </ul>
    </header>
 
    <main>
      <h1>Forum Board</h1>
      <h2>Meddelanden</h2>
   
      <ul>
        <?php
          include_once('db-connect.php');
          include_once('functions.php');

          $sql = "SELECT id, iBo, created_At FROM forum_posts ORDER BY created_At DESC";
       
          if (($result = $conn->query($sql)) && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
             echo '<li class="iBo">'
                  . htmlspecialchars(format_id($row["id"])) . ': '
                  . htmlspecialchars($row["created_At"]) . ' - '
                  . htmlspecialchars($row["iBo"])
                  . '</li>';
            }
          } else {
            echo '<li>Det finns inga inlägg än.</li>';
          }
 
          $conn->close();
        ?>
      </ul>
    </main>

  </body>
</html>

BTW, here is an example of what the code for the file post_message.php looks like.
PHP:
<?php
// Include the database connection file
include_once('db-connect.php');

// Check if the form is submitted via POST and if the message field is not empty
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['iBo'])) {
    // Safely retrieve the form input using htmlspecialchars to prevent XSS attacks
    $iBo = htmlspecialchars($_POST['iBo']);
   
    // SQL query to insert the message into the database, with the current timestamp
    $sql = "INSERT INTO forum_posts (iBo, created_At) VALUES (?, NOW())";

    // Prepare the SQL query to prevent SQL injection
    if ($stmt = $conn->prepare($sql)) {
        // Bind the parameter to the SQL query (in this case, the message)
        $stmt->bind_param("s", $iBo); // "s" stands for string

        // Execute the SQL query
        if ($stmt->execute()) {
            // If the message is successfully inserted, display a success message
            echo "Meddelandet har skickats!"; // Message has been sent!
        } else {
            // If there was an error while saving, display an error message
            echo "Fel vid sparande av meddelandet: " . $stmt->error; // Error while saving the message
        }

        // Close the statement after execution
        $stmt->close();
    } else {
        // If there was an error preparing the SQL query, display an error message
        echo "Fel vid förberedelse av frågan: " . $conn->error; // Error preparing the query
    }
} else {
    // If the message field is empty, ask the user to enter a message
    echo "Vänligen skriv ett meddelande."; // Please write a message
}

// Close the database connection after the operation is complete
$conn->close();
?>
 
Last edited:
Joined
Jul 4, 2023
Messages
548
Reaction score
71
One more thought regarding index.php.
PHP:
<?php
  session_start();
 
  $config = require_once('config.php');

  if ($config['is_under_construction']) {
    header('HTTP/1.1 503 Service Unavailable');
    include 'under_construction.php';
    exit;
  }
?>
<!doctype html>
<html lang="sv">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your website name</title>

    <link rel="stylesheet" href="css/styles.css">
    <style>
      html,
      body {
        margin: 0;
        padding: .5rem;
      }
      header ul {
        display: flex;
        list-style: none;
        justify-content: space-between;
        margin: 0;
        padding: .5rem 1rem;
      }
      header .logo { /* for demonstration purposes */
        background-image: url('https://png.pngtree.com/png-clipart/20190604/original/pngtree-creative-company-logo-png-image_1197025.jpg');
        width: 50px;
        height: 50px;
        background-repeat: no-repeat;
        background-size: cover;
      }
      header .logo a {
        display: block;
        width: 100%;
        height: 100%;     
      }
      main ul {
        list-style: none;
        padding: 0;
      }
      main li.iBo {
        background-color: #d4edda;
        padding: 10px;
        margin: 5px 0;
        border-radius: 5px;
        border: 1px solid #a9dfbf;
        font-family: inherit;
      }
    </style>
  </head>
  <body>

    <header>
      <ul>
        <li class="logo">
          <a href="index.php"></a>
        </li>
        <li>
          <?php if (! empty($_SESSION["user_logged_in"])): ?>
            Välkommen: <a href="user_account.php"><?php echo $_SESSION["user_logged_name"]; ?></a>
          <?php else: ?>
            <a href="register.php">Registrera konto</a> ||
            <a href="login.php">Logga in</a>
          <?php endif; ?>
        </li>
      </ul>
    </header>
 
    <main>
      <h1>Forum Board</h1>
      <h2>Meddelanden</h2>
  
      <ul>
        <?php
          include_once('db-connect.php');
          include_once('functions.php');

          $sql = "SELECT id, iBo, created_At FROM forum_posts ORDER BY created_At DESC";
      
          if (($result = $conn->query($sql)) && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
             echo '<li class="iBo">'
                  . htmlspecialchars(format_id($row["id"])) . ': '
                  . htmlspecialchars($row["created_At"]) . ' - '
                  . htmlspecialchars($row["iBo"])
                  . '</li>';
            }
          } else {
            echo '<li>Det finns inga inlägg än.</li>';
            if (! empty($_SESSION["user_logged_in"])) {
              echo '<li><a href="add_new_post.php">+ lägg till nytt inlägg</a></li>;
            }
          }
 
          $conn->close();
        ?>
      </ul>
    </main>

  </body>
</html>
 
Joined
Jul 4, 2023
Messages
548
Reaction score
71
Oops, my mistake, I forgot to close the text string ( '...' ) here.
PHP:
echo '<li>Det finns inga inlägg än.</li>';
if (! empty($_SESSION["user_logged_in"])) {
  echo '<li><a href="add_new_post.php">+ lägg till nytt inlägg</a></li>'; // <==
}
 
Joined
Sep 4, 2022
Messages
140
Reaction score
16
hello !

to make yourself going through easy coding habits,
when you use external files with code only :


PHP:
<?php
// first lines of the file :

include_once 'external code.php';
include_once 'all resources external for the current page to execute.php' ;

// all code resources can be added here.
// by using include / require / include_once / require_once functions

// --------------------------------
// Php loads all files required when a script is launch,
// when in one sub scope (if(){....; include 'code.php' ; #faulty use })
// the amount of memory needed will be same.

// to make you coding easier , all resources will be load at top of your code page.

?>

the only external resources requiring a load in if statement are 'xml / html pattern files'.
when It's about Php script, put 'include' at top of your page.

conditional includes can be all at beginning of your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,120
Messages
2,570,710
Members
47,282
Latest member
citowad9

Latest Threads

Top