Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "phppdo";
- try {
- $connection = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
- // set the PDO error mode to exception
- $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $e)
- {
- echo $sql . "<br>" . $e->getMessage();
- }
- ?>
- <!DOCTYPE HTML>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title></title>
- <style type="text/css">
- input{
- display: block;
- }
- </style>
- </head>
- <body>
- <?php
- if(isset($_POST['submit']))
- {
- $name= $_POST['name'];
- $date= $_POST['date'];
- $email = $_POST['email'];
- $role = $_POST['role'];
- $sql = "INSERT INTO info(name,date,email,role) VALUES(:name, :date, :email, :role)";
- $stmt = $connection->prepare($sql);
- $stmt->bindparam(':name', $name);
- $stmt->bindparam(':date', $date);
- $stmt->bindparam(':email', $email);
- $stmt->bindparam(':role', $role);
- $stmt->execute();
- echo "data inserted successfully";
- }
- $sql = "SELECT * FROM info ORDER BY id DESC";
- $stmt = $connection->prepare($sql);
- $stmt->execute();
- if(isset($_GET['delete_id'])){
- $id = $_GET['delete_id'];
- $sql = "DELETE FROM info WHERE id = :id";
- $stmt = $connection->prepare($sql);
- $stmt->bindparam(':id', $id);
- $stmt->execute();
- header("Location: single.php");
- }
- if(isset($_GET['edit_id'])){
- $id = $_GET['edit_id'];
- $sql = "SELECT * FROM info WHERE id = :id";
- $stmt = $connection->prepare($sql);
- $stmt->bindparam(':id', $id);
- $stmt->execute();
- $edit_row = $stmt->FETCH(PDO::FETCH_ASSOC);
- }
- if(isset($_POST['update'])){
- $id = $_GET['edit_id'];
- $name= $_POST['name'];
- $date= $_POST['date'];
- $email = $_POST['email'];
- $role = $_POST['role'];
- $sql = "UPDATE info SET name = :name, date = :date, email = :email, role = :role WHERE id = :id";
- $stmt = $connection->prepare($sql);
- $stmt->bindparam(':name', $name);
- $stmt->bindparam(':date', $date);
- $stmt->bindparam(':email', $email);
- $stmt->bindparam(':role', $role);
- $stmt->bindparam(':id', $id);
- $stmt->execute();
- header("Location: single.php");
- }
- ?>
- <table border="1">
- <tr>
- <th>Id</th>
- <th>Name</th>
- <th>Date</th>
- <th>Email</th>
- <th>Role</th>
- <th>Update</th>
- <th>Delete</th>
- </tr>
- <?php
- if($stmt->rowCount() > 0)
- {
- while($row = $stmt->FETCH(PDO::FETCH_ASSOC))
- {
- ?>
- <tr>
- <td><?php echo $row['id']; ?></td>
- <td><?php echo $row['name']; ?></td>
- <td><?php echo $row['date']; ?></td>
- <td><?php echo $row['email']; ?></td>
- <td><?php echo $row['role']; ?></td>
- <td><a onclick="return confirm('Are you sure?')" href="single.php?edit_id=<?php echo $row['id']; ?>">Edit</a></td>
- <td><a onclick="return confirm('Are you sure?')" href="single.php?delete_id=<?php echo $row['id']; ?>">Delete</a></td>
- </tr>
- <?php
- }
- }
- ?>
- </table>
- <form action="" method="post">
- <label for="">Name: </label>
- <input type="text" name="name" placeholder="Your name" value="<?php if(isset($_GET['edit_id'])){echo $edit_row['name'];}; ?>"/>
- <label for="">Date: </label>
- <input type="date" name="date" placeholder="1979-12-31" value="<?php if(isset($_GET['edit_id'])){echo $edit_row['date'];}; ?>"/>
- <label for="">Email: </label>
- <input type="mail" name="email" placeholder="Enter your mail" value="<?php if(isset($_GET['edit_id'])){echo $edit_row['email'];}; ?>"/>
- <label for="">Role: </label>
- <input type="text" name="role" placeholder="Admin" value="<?php if(isset($_GET['edit_id'])){echo $edit_row['role'];}; ?>"/>
- <?php
- if(isset($_GET['edit_id'])){
- ?>
- <input type="submit" name="update" value="Update"/>
- <?php
- }else{
- ?>
- <input type="submit" name="submit" value="Submit"/>
- <?php
- }
- ?>
- </form>
- </body>
- </html>
- <?php
- //$connection = null;
- ?>
Add Comment
Please, Sign In to add comment