View difference between Paste ID: T66VqRcq and q1AnkRmG
SHOW: | | - or go back to the newest paste.
1
<?php
2
3
//Get names of database to establish connection
4
$servername = "localhost";
5
$username = "root";
6
$password = "";
7
$dbname = "opslag";
8
9
// Create connection
10
$conn = new mysqli($servername, $username, $password, $dbname);
11
mysqli_set_charset($conn,"utf8");
12
if ($conn->connect_error) {
13
    die("Connection failed: " . $conn->connect_error);
14
}
15
?>
16
17
18
<html>
19
  <head>
20
    <meta charset="utf-8  ">
21
22
    <!-- here I call for my Style.css script. This script makes bulletin look good and imports a background-->
23
      <link rel="stylesheet" type="text/css" href="Style.css">
24
  </head>
25
26
  <!-- making two text-inputs (overskrift and tekst) and adding a submit button to save the data as POST so it is accessible-->
27
      <form method="POST">
28
      <input type="text" name="overskrift" oninput="this.value = this.value.toUpperCase()" /><br/>
29
      <input type="text" name="tekst"/><br/>
30
      <input type="submit" value="Create post"/>
31
      </form>
32
33
<?php
34
35
// 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"
36
if(isset($_POST["overskrift"])){
37
    if (!empty($_POST["overskrift"] && !empty($_POST["tekst"]))){
38
      $overskrift = $_POST["overskrift"];
39
      $tekst = $_POST["tekst"];
40
      $dato=date("y-m-d \n H:i");
41
      $gemsql = "INSERT INTO artikel (overskrift, tekst, dato) VALUES ('$overskrift', '$tekst','$dato');";
42
      $conn->query($gemsql);
43
    }
44
}
45
46
// This if statement will check the id of the post that the administraitor wants to delete, and then removes it from mysql database
47
if (!empty($_POST['id'])) {
48
   $id=$_POST['id'];
49
   $sletsql = "delete from artikel where id= $id";
50
   $conn->query($sletsql);
51
 }
52
53
// Here every bulltin that has been created and saved into the database table "artikel" is being printed out to the website
54
$sql = "SELECT * FROM artikel;";
55
$result = $conn->query($sql);
56
57
//TRUNCATE TABLE "tablename" (use this command in cmd to delete everything in a table, including the id)
58
if ($result->num_rows > 0) {
59
    // output data of each row
60
    while($row = $result->fetch_assoc()) {
61
    ?>
62
    <div class="opslag">
63
      <form method="POST">
64
        <input type="hidden" name="id" value = "<?php echo $row['id']; ?>" />
65
        <input type="submit" value="Delete">
66
      </form>
67
    <?php
68
69
      $dato= $row["dato"];
70
      $overskrift = $row["overskrift"];
71
      $tekst = $row["tekst"];
72
      echo "<br>$dato</br> <br>$overskrift</br> <br>$tekst</br>";
73
74
    ?>
75
  </div>
76
77
    <?php
78
}
79
}
80
$conn->close();
81
?>
82
</html>