Guest User

Untitled

a guest
Jul 24th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <?php
  2.  
  3. // Include my functions
  4. require_once("functions.php");
  5.  
  6. // Connect to database
  7. $handle = connectToDatabase();
  8.  
  9. // Select the database I want to work with
  10. mysql_select_db("testdb");
  11.  
  12. // Switch the post that comes back
  13. switch ($_POST['action'])
  14. {
  15. // When action returns as "add" call function addUser()
  16. case "add":
  17. addUser();
  18. break;
  19.  
  20. // When action returns as "remove" call function removeUser()
  21. case "remove":
  22. removeUser();
  23. break;
  24. }
  25.  
  26. // Function to add users
  27. function addUser()
  28. {
  29. // Store the username and password back from the post
  30. $username = $_POST['username'];
  31. $password = $_POST['password'];
  32.  
  33. // Construct the SQL
  34. $sql = "INSERT INTO `users` (`username`, `password`) VALUES ('{$username}', '{$password}')";
  35.  
  36. // Execute the query
  37. executeQuery($sql);
  38. }
  39.  
  40. // Function to remove users
  41. function removeUser()
  42. {
  43. // Store the ID of the row you want to delete
  44. $row_id = $_POST['row_id'];
  45.  
  46. // Construct the SQL
  47. $sql = "DELETE FROM `users` WHERE 1 AND `id` = '{$row_id}'";
  48.  
  49. // Execute the query
  50. executeQuery($sql);
  51. }
  52.  
  53. // Function execute query
  54. function executeQuery($sql)
  55. {
  56. // Query the database
  57. $results = mysql_query($sql);
  58.  
  59. // Print out the affected rows
  60. printf("Query executed: %s rows affected.", mysql_affected_rows());
  61. }
  62.  
  63. // Function to return the users currently in the database
  64. function getUsers()
  65. {
  66. // Construct the SQL
  67. $sql = "SELECT * FROM `users` WHERE 1 LIMIT 20";
  68.  
  69. // Query the database
  70. $results = mysql_query($sql);
  71.  
  72. // Start constructing the select block
  73. $output = "<select name=\"row_id\">";
  74.  
  75. // While through your results
  76. while ($row = mysql_fetch_array($results))
  77. {
  78. $output .= "<option value=\"{$row['id']}\">{$row['username']}</option>\n";
  79. }
  80.  
  81. // Finish the select block
  82. $output .= "</select>";
  83.  
  84. // Return your <select> block
  85. return $output;
  86. }
  87.  
  88. ?>
  89. <!DOCTYPE HTML>
  90. <html>
  91. <head>
  92. <title>PHP and MySQL</title>
  93. </head>
  94. <body>
  95.  
  96. <form action="ello.php" method="post">
  97. <input type="text" name="username" value="Username" />
  98. <input type="password" name="password" value="password" />
  99. <input type="hidden" name="action" value="add" />
  100. <input type="submit" value="Add User" />
  101. </form>
  102.  
  103. <br />
  104.  
  105. <form action="ello.php" method="post">
  106. <?php echo getUsers(); ?>
  107. <input type="hidden" name="action" value="remove" />
  108. <input type="submit" value="Delete User" />
  109. </form>
  110.  
  111. </body>
  112. </html>
Add Comment
Please, Sign In to add comment