Guest User

Untitled

a guest
Feb 16th, 2016
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5.  
  6.  
  7. $servername = "localhost";
  8. $username = "root";
  9. $password = "root";
  10. $database = "lamp2proj1";
  11. $connection = new mysqli($servername, $username, $password, $database);
  12. if ($connection->connect_error)
  13. {
  14. die("Connection failed: " . $connection->connect_error);
  15. }
  16.  
  17. if(isset($_POST['action']) && !empty($_POST['action'])) {
  18. $action = $_POST['action'];
  19.  
  20. switch($action) {
  21. case 'delete':
  22. deletePost($connection, $_POST['data']);
  23. break;
  24. case 'new':
  25. newPost();
  26. break;
  27. }
  28. }
  29.  
  30. function newPost()
  31. {
  32. $sql = "INSERT INTO task (description, priority) VALUES ('".$_POST['description']."', ".$_POST['priority'].")";
  33.  
  34. if ($connection->query($sql) === TRUE) {
  35. echo "New record created successfully";
  36. header('Location:index.php');
  37. } else {
  38. echo "Error: " . $sql . "<br>" . $connection->error;
  39. }
  40.  
  41. $connection->close();
  42. }
  43.  
  44. function deletePost($connection, $ids) {
  45. $decodeJSON = json_decode($ids);
  46. // var_dump($decodeJSON);
  47. foreach($decodeJSON as $id) {
  48. $sql = "DELETE FROM task WHERE id = $id";
  49. if ($connection->query($sql) === TRUE) {
  50. echo "deleted";
  51. } else {
  52. echo "Error: " . $sql . "<br>" . $connection->error;
  53. }
  54. }
  55.  
  56. $connection->close();
  57. }
  58.  
  59. ?>
Add Comment
Please, Sign In to add comment