Guest User

Untitled

a guest
Nov 8th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. <?php
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $dbname = "phppdo";
  6.  
  7. try {
  8. $connection = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  9. // set the PDO error mode to exception
  10. $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11. }
  12. catch(PDOException $e)
  13. {
  14. echo $sql . "<br>" . $e->getMessage();
  15. }
  16. ?>
  17. <!DOCTYPE HTML>
  18. <html lang="en-US">
  19. <head>
  20. <meta charset="UTF-8">
  21. <title></title>
  22. <style type="text/css">
  23. input{
  24. display: block;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <?php
  30. if(isset($_POST['submit']))
  31. {
  32. $name= $_POST['name'];
  33. $date= $_POST['date'];
  34. $email = $_POST['email'];
  35. $role = $_POST['role'];
  36. $sql = "INSERT INTO info(name,date,email,role) VALUES(:name, :date, :email, :role)";
  37. $stmt = $connection->prepare($sql);
  38. $stmt->bindparam(':name', $name);
  39. $stmt->bindparam(':date', $date);
  40. $stmt->bindparam(':email', $email);
  41. $stmt->bindparam(':role', $role);
  42. $stmt->execute();
  43. echo "data inserted successfully";
  44. }
  45.  
  46. $sql = "SELECT * FROM info ORDER BY id DESC";
  47. $stmt = $connection->prepare($sql);
  48. $stmt->execute();
  49.  
  50. if(isset($_GET['delete_id'])){
  51. $id = $_GET['delete_id'];
  52. $sql = "DELETE FROM info WHERE id = :id";
  53. $stmt = $connection->prepare($sql);
  54. $stmt->bindparam(':id', $id);
  55. $stmt->execute();
  56. header("Location: single.php");
  57. }
  58.  
  59. if(isset($_GET['edit_id'])){
  60. $id = $_GET['edit_id'];
  61. $sql = "SELECT * FROM info WHERE id = :id";
  62. $stmt = $connection->prepare($sql);
  63. $stmt->bindparam(':id', $id);
  64. $stmt->execute();
  65. $edit_row = $stmt->FETCH(PDO::FETCH_ASSOC);
  66. }
  67.  
  68. if(isset($_POST['update'])){
  69. $id = $_GET['edit_id'];
  70. $name= $_POST['name'];
  71. $date= $_POST['date'];
  72. $email = $_POST['email'];
  73. $role = $_POST['role'];
  74. $sql = "UPDATE info SET name = :name, date = :date, email = :email, role = :role WHERE id = :id";
  75. $stmt = $connection->prepare($sql);
  76. $stmt->bindparam(':name', $name);
  77. $stmt->bindparam(':date', $date);
  78. $stmt->bindparam(':email', $email);
  79. $stmt->bindparam(':role', $role);
  80. $stmt->bindparam(':id', $id);
  81. $stmt->execute();
  82. header("Location: single.php");
  83. }
  84. ?>
  85. <table border="1">
  86. <tr>
  87. <th>Id</th>
  88. <th>Name</th>
  89. <th>Date</th>
  90. <th>Email</th>
  91. <th>Role</th>
  92. <th>Update</th>
  93. <th>Delete</th>
  94. </tr>
  95. <?php
  96. if($stmt->rowCount() > 0)
  97. {
  98. while($row = $stmt->FETCH(PDO::FETCH_ASSOC))
  99. {
  100. ?>
  101. <tr>
  102. <td><?php echo $row['id']; ?></td>
  103. <td><?php echo $row['name']; ?></td>
  104. <td><?php echo $row['date']; ?></td>
  105. <td><?php echo $row['email']; ?></td>
  106. <td><?php echo $row['role']; ?></td>
  107. <td><a onclick="return confirm('Are you sure?')" href="single.php?edit_id=<?php echo $row['id']; ?>">Edit</a></td>
  108. <td><a onclick="return confirm('Are you sure?')" href="single.php?delete_id=<?php echo $row['id']; ?>">Delete</a></td>
  109. </tr>
  110. <?php
  111. }
  112. }
  113. ?>
  114. </table>
  115. <form action="" method="post">
  116. <label for="">Name: </label>
  117. <input type="text" name="name" placeholder="Your name" value="<?php if(isset($_GET['edit_id'])){echo $edit_row['name'];}; ?>"/>
  118. <label for="">Date: </label>
  119. <input type="date" name="date" placeholder="1979-12-31" value="<?php if(isset($_GET['edit_id'])){echo $edit_row['date'];}; ?>"/>
  120. <label for="">Email: </label>
  121. <input type="mail" name="email" placeholder="Enter your mail" value="<?php if(isset($_GET['edit_id'])){echo $edit_row['email'];}; ?>"/>
  122. <label for="">Role: </label>
  123. <input type="text" name="role" placeholder="Admin" value="<?php if(isset($_GET['edit_id'])){echo $edit_row['role'];}; ?>"/>
  124. <?php
  125. if(isset($_GET['edit_id'])){
  126. ?>
  127. <input type="submit" name="update" value="Update"/>
  128. <?php
  129. }else{
  130. ?>
  131. <input type="submit" name="submit" value="Submit"/>
  132. <?php
  133. }
  134. ?>
  135. </form>
  136. </body>
  137. </html>
  138. <?php
  139. //$connection = null;
  140. ?>
Add Comment
Please, Sign In to add comment