Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Include my functions
- require_once("functions.php");
- // Connect to database
- $handle = connectToDatabase();
- // Select the database I want to work with
- mysql_select_db("testdb");
- // Switch the post that comes back
- switch ($_POST['action'])
- {
- // When action returns as "add" call function addUser()
- case "add":
- addUser();
- break;
- // When action returns as "remove" call function removeUser()
- case "remove":
- removeUser();
- break;
- }
- // Function to add users
- function addUser()
- {
- // Store the username and password back from the post
- $username = $_POST['username'];
- $password = $_POST['password'];
- // Construct the SQL
- $sql = "INSERT INTO `users` (`username`, `password`) VALUES ('{$username}', '{$password}')";
- // Execute the query
- executeQuery($sql);
- }
- // Function to remove users
- function removeUser()
- {
- // Store the ID of the row you want to delete
- $row_id = $_POST['row_id'];
- // Construct the SQL
- $sql = "DELETE FROM `users` WHERE 1 AND `id` = '{$row_id}'";
- // Execute the query
- executeQuery($sql);
- }
- // Function execute query
- function executeQuery($sql)
- {
- // Query the database
- $results = mysql_query($sql);
- // Print out the affected rows
- printf("Query executed: %s rows affected.", mysql_affected_rows());
- }
- // Function to return the users currently in the database
- function getUsers()
- {
- // Construct the SQL
- $sql = "SELECT * FROM `users` WHERE 1 LIMIT 20";
- // Query the database
- $results = mysql_query($sql);
- // Start constructing the select block
- $output = "<select name=\"row_id\">";
- // While through your results
- while ($row = mysql_fetch_array($results))
- {
- $output .= "<option value=\"{$row['id']}\">{$row['username']}</option>\n";
- }
- // Finish the select block
- $output .= "</select>";
- // Return your <select> block
- return $output;
- }
- ?>
- <!DOCTYPE HTML>
- <html>
- <head>
- <title>PHP and MySQL</title>
- </head>
- <body>
- <form action="ello.php" method="post">
- <input type="text" name="username" value="Username" />
- <input type="password" name="password" value="password" />
- <input type="hidden" name="action" value="add" />
- <input type="submit" value="Add User" />
- </form>
- <br />
- <form action="ello.php" method="post">
- <?php echo getUsers(); ?>
- <input type="hidden" name="action" value="remove" />
- <input type="submit" value="Delete User" />
- </form>
- </body>
- </html>
Add Comment
Please, Sign In to add comment