Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Get names of database to establish connection
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "opslag";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- mysqli_set_charset($conn,"utf8");
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- ?>
- <html>
- <head>
- <meta charset="utf-8 ">
- <!-- here I call for my Style.css script. This script makes bulletin look good and imports a background-->
- <link rel="stylesheet" type="text/css" href="Style.css">
- </head>
- <!-- making two text-inputs (overskrift and tekst) and adding a submit button to save the data as POST so it is accessible-->
- <form method="POST">
- <input type="text" name="overskrift" oninput="this.value = this.value.toUpperCase()" /><br/>
- <input type="text" name="tekst"/><br/>
- <input type="submit" value="Create post"/>
- </form>
- <?php
- // Save the headline and text if they have been filled and the date it was posted. This data is inserted into mysql database "opslag", into the table "artikel"
- if(isset($_POST["overskrift"])){
- if (!empty($_POST["overskrift"] && !empty($_POST["tekst"]))){
- $overskrift = $_POST["overskrift"];
- $tekst = $_POST["tekst"];
- $dato=date("y-m-d \n H:i");
- $gemsql = "INSERT INTO artikel (overskrift, tekst, dato) VALUES ('$overskrift', '$tekst','$dato');";
- $conn->query($gemsql);
- }
- }
- // This if statement will check the id of the post that the administraitor wants to delete, and then removes it from mysql database
- if (!empty($_POST['id'])) {
- $id=$_POST['id'];
- $sletsql = "delete from artikel where id= $id";
- $conn->query($sletsql);
- }
- // Here every bulltin that has been created and saved into the database table "artikel" is being printed out to the website
- $sql = "SELECT * FROM artikel;";
- $result = $conn->query($sql);
- //TRUNCATE TABLE "tablename" (use this command in cmd to delete everything in a table, including the id)
- if ($result->num_rows > 0) {
- // output data of each row
- while($row = $result->fetch_assoc()) {
- ?>
- <div class="opslag">
- <form method="POST">
- <input type="hidden" name="id" value = "<?php echo $row['id']; ?>" />
- <input type="submit" value="Delete">
- </form>
- <?php
- $dato= $row["dato"];
- $overskrift = $row["overskrift"];
- $tekst = $row["tekst"];
- echo "<br>$dato</br> <br>$overskrift</br> <br>$tekst</br>";
- ?>
- </div>
- <?php
- }
- }
- $conn->close();
- ?>
- </html>
Add Comment
Please, Sign In to add comment