Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. <?php
  2.  
  3. $host = "localhost";
  4. $user = "root";
  5. $password ="";
  6. $database = "ntmadb";
  7.  
  8. $id = "";
  9. $firstname = "";
  10. $lastname = "";
  11. $username = "";
  12.  
  13. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  14.  
  15. // connect to mysql database
  16. try{
  17. $connect = mysqli_connect($host, $user, $password, $database);
  18. } catch (mysqli_sql_exception $ex) {
  19. echo 'Error';
  20. }
  21.  
  22. // get values from the form
  23. function getPosts()
  24. {
  25. $posts = array();
  26. $posts[0] = $_POST['id'];
  27. $posts[1] = $_POST['firstname'];
  28. $posts[2] = $_POST['lastname'];
  29. $posts[3] = $_POST['username'];
  30. return $posts;
  31. }
  32.  
  33. // Search
  34.  
  35. if(isset($_POST['search']))
  36. {
  37. $data = getPosts();
  38.  
  39. $search_Query = "SELECT * FROM members WHERE id = $data[0]";
  40.  
  41. $search_Result = mysqli_query($connect, $search_Query);
  42.  
  43. if($search_Result)
  44. {
  45. if(mysqli_num_rows($search_Result))
  46. {
  47. while($row = mysqli_fetch_array($search_Result))
  48. {
  49. $id = $row['id'];
  50. $firstname = $row['firstname'];
  51. $lastname = $row['lastname'];
  52. $username = $row['username'];
  53. }
  54. }else{
  55. echo 'No Data For This Id';
  56. }
  57. }else{
  58. echo 'Result Error';
  59. }
  60. }
  61.  
  62.  
  63. // Edit
  64. if(isset($_POST['update']))
  65. {
  66. $data = getPosts();
  67. $update_Query = "UPDATE `members` SET `firstname`='$data[1]',`lastname`='$data[2]',`username`='$data[3]' WHERE `id` = $data[0]";
  68. try{
  69. $update_Result = mysqli_query($connect, $update_Query);
  70.  
  71. if($update_Result)
  72. {
  73. if(mysqli_affected_rows($connect) > 0)
  74. {
  75. echo 'Data Updated';
  76. }else{
  77. echo 'Data Not Updated';
  78. }
  79. }
  80. } catch (Exception $ex) {
  81. echo 'Error Update '.$ex->getMessage();
  82. }
  83. }
  84.  
  85. <!--UPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOAD -->
  86. // Check if a file has been uploaded
  87. if(isset($_FILES['uploaded_file'])) {
  88. // Make sure the file was sent without errors
  89. if($_FILES['uploaded_file']['error'] == 0) {
  90. // Connect to the database
  91. $dbLink = new mysqli('localhost', 'root', '', 'ntmadb');
  92. if(mysqli_connect_errno()) {
  93. die("MySQL connection failed: ". mysqli_connect_error());
  94. }
  95.  
  96. // Gather all required data
  97.  
  98.  
  99. $data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
  100.  
  101.  
  102. // Create the SQL query
  103. $query = "
  104. INSERT INTO `members` (
  105. `data`
  106. )
  107. VALUES (
  108. '{$data}' NOW()
  109. )";
  110.  
  111. // Execute the query
  112. $result = $dbLink->query($query);
  113.  
  114. // Check if it was successfull
  115. if($result) {
  116. echo 'Success! Your file was successfully added!';
  117. }
  118. else {
  119. echo 'Error! Failed to insert the file'
  120. . "<pre>{$dbLink->error}</pre>";
  121. }
  122. }
  123. else {
  124. echo 'An error accured while the file was being uploaded. '
  125. . 'Error code: '. intval($_FILES['uploaded_file']['error']);
  126. }
  127.  
  128. // Close the mysql connection
  129. $dbLink->close();
  130. }
  131. else {
  132. echo 'Error! A file was not sent!';
  133. }
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement