Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- $mysqli = new mysqli('localhost', 'testblog', 'password', 'testblog');
- try {
- if ($mysqli->connect_error) {
- echo "<h5 class='red-text center'>Can't connect to the database!</h5><br>";
- throw new Exception($conn->connect_error);
- }
- } catch (Exception $e){
- echo $e->getMessage(), "\n";
- }
- function makeNewPost() : bool
- {
- $stmt = $mysqli->prepare("INSERT INTO posts VALUES (null, ?, ?, ?, NOW());");
- $stmt->bind_param('sss', $_POST['title'], $_POST['text'], $_POST['image']);
- if ($stmt->execute()) {
- return true;
- } else {
- return false;
- }
- }
- function correctCredentials() : bool
- {
- if (isset($_POST['user']) && isset($_POST['pass'])) {
- if ($_POST['user'] == 'username' && $_POST['pass'] == 'password') {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- ?>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Dashboard</title>
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
- <link rel="stylesheet" href="css/main.css">
- </head>
- <body>
- <div class="ui container">
- <?php
- if (isset($_SESSION['isLoggedIn'])) {
- if (isset($_POST['title']) && isset($_POST['text']) && isset($_POST['image'])) {
- makeNewPost();
- if (!makeNewPost()) {
- echo '<h5 class=\'red-text center\'>Error creating post!</h5>';
- } else {
- header('Refresh:0; url=index.php');
- }
- } else {
- ?>
- <br>
- <div class="container">
- <div class="card-panel">
- <h4>New Post</h4>
- <div class="row">
- <form method="POST" action="index.php" class="col s12">
- <div class="row">
- <div class="input-field col s6">
- <i class="fa fa-book prefix"></i>
- <input id="Title" name="title" type="text" class="validate">
- <label for="Title">Title</label>
- </div>
- <div class="input-field col s12">
- <i class="fa fa-file-text prefix"></i>
- <input id="Text" name="text" type="text">
- <label for="Text">Text</label>
- </div>
- <div class="input-field col s12">
- <i class="fa fa-file-image-o prefix"></i>
- <input id="Image" name="image" type="text">
- <label for="Image">Image</label>
- </div>
- <div class="input-field col s12">
- <i class="fa fa-lock prefix"></i>
- <input id="password" name="pass" type="password" class="validate">
- <label for="password">Confirm Password</label>
- </div>
- </div>
- <button class="btn waves-effect waves-light right" type="submit" name="action">Submit
- <i class="fa fa-arrow-circle-right"></i>
- </button>
- </form>
- </div>
- </div>
- </div>
- <?php
- }
- } else {
- if (correctCredentials()) {
- $_SESSION['isLoggedIn'] = true;
- header('Refresh:0');
- } else {
- ?>
- <div class="container"><div class="container"><div class="container"><div class="container">
- <br><br><br><br><br><br>
- <div class="card-panel">
- <h4>Login</h4>
- <div class="row">
- <form class="col s12" method="POST" action="dashboard.php">
- <div class="row">
- <div class="input-field col s10">
- <i class="fa fa-user-circle prefix"></i>
- <input type="text" name="user"></input>
- <label for="user">User Name</label>
- </div>
- </div>
- <div class="row">
- <div class="input-field col s10">
- <i class="fa fa-lock prefix prefix"></i>
- <input type="password" name="pass"></input><br/>
- <label for="pass">Password</label>
- </div>
- </div>
- <input type="submit" name="submit" value="Go"></input>
- </form>
- </div>
- </div>
- </div></div></div></div>
- <?php
- }
- }
- /*
- if(isset($_POST['title']) && isset($_POST['text']) && isset($_POST['image'])){
- $mysqli = new MySQLi('localhost', 'root', null, 'test');
- $stmt = $mysqli->prepare("INSERT INTO alphy VALUES (null, ?, ?, ?, NOW());");
- $stmt->bind_param('sss', $_POST['title'], $_POST['text'], $_POST['image']);
- if ($stmt->execute()) {
- echo "successfull";
- } else {
- echo "error";
- }
- }
- */
- $mysqli->close();
- ?>
- </div>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement