Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $mysqli = new mysqli('localhost', 'root', '', 'crud_test') or die(mysqli_error($mysqli));
- session_start();
- $update = false;
- $id = 0;
- $location = '';
- $name = '';
- if(isset($_POST['save']))
- {
- $name = $_POST['name'];
- $location = $_POST['location'];
- $mysqli->query("INSERT INTO data(name, location) VALUES('$name', '$location' )") or die($mysqli->error);
- $_SESSION['message'] = "Data succesfully inserted.";
- $_SESSION['msg_type'] = "Success:";
- header("location: index.php");
- }
- if(isset($_POST['update']))
- {
- $id = $_POST['id'];
- $name = $_POST['name'];
- $location = $_POST['location'];
- $mysqli->query("UPDATE data SET name='$name', location='$location' WHERE id = $id") or die($mysqli->error);
- $_SESSION['message'] = "Data succesfully Updated.";
- $_SESSION['msg_type'] = "Success:";
- header("location: index.php");
- }
- if(isset($_GET['edit']))
- {
- $id = $_GET['edit'];
- $result = $mysqli->query("SELECT * FROM data WHERE id=$id") or die($mysqli->error);
- $row = $result->fetch_array();
- $name = $row['name'];
- $location = $row['location'];
- $update = true;
- }
- if(isset($_GET['delete']))
- {
- $id = $_GET['delete'];
- $mysqli->query("DELETE FROM data WHERE id=$id") or die($mysqli->error);
- $_SESSION['message'] = "Data succesfully Deleted.";
- $_SESSION['msg_type'] = "Information:";
- header("location: index.php");
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Crud test</title>
- </head>
- <body>
- <?php
- require_once "process.php";
- $mysqli = new mysqli('localhost', 'root', '', 'crud_test') or die(mysqli_error($mysqli));
- $result = $mysqli->query("SELECT * FROM data") or die($mysqli->error);
- if(isset($_SESSION['message']))
- {
- echo $_SESSION['msg_type'] . " " . $_SESSION['message'];
- unset($_SESSION['message']);
- }
- ?>
- <form action="process.php" method="POST">
- <input type="hidden" name="id" value="<?php echo $id; ?>">
- <input type="text" placeholder="name" value="<?php echo $name; ?>" name="name">
- <input type="text" placeholder="location" value="<?php echo $location; ?>" name="location">
- <?php
- if($update == true)
- {
- echo "<button type='submit' name='update'>Update</button>";
- }
- else
- {
- echo "<button type='submit' name='save'>Save</button>";
- }
- ?>
- </form>
- <table>
- <thead>
- <tr>
- <td>Name</td>
- <td>Location</td>
- <td>Action</td>
- </tr>
- </thead>
- <tbody>
- <?php
- while($row = $result->fetch_assoc())
- {
- echo "<tr>";
- echo "<td>{$row['name']}</td>";
- echo "<td>{$row['location']}</td>";
- echo "<td><a href='index.php?edit={$row['id']}'>Edit</a>
- <a href='process.php?delete={$row['id']}'>Delete</a></td>";
- echo "</tr>";
- }
- ?>
- </tbody>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement