Advertisement
vvccs

wt_14_updatedata

Oct 13th, 2024 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.50 KB | None | 0 0
  1. form.html
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Update User</title>
  8. </head>
  9. <body>
  10. <h2>Update User Information</h2>
  11. <form action="update_user.php" method="post">
  12. <input type="hidden" name="id" value="1"> <!-- Assuming we want to update user with ID 1 -->
  13. <label for="name">Name:</label>
  14. <input type="text" id="name" name="name" required>
  15. <br><br>
  16. <label for="email">Email:</label>
  17. <input type="email" id="email" name="email" required>
  18. <br><br>
  19. <input type="submit" value="Update User">
  20. </form>
  21. </body>
  22. </html>
  23.  
  24.  
  25. updare_user.php
  26. <?php
  27. // Database credentials
  28. $host = 'localhost';
  29. $username = 'root';
  30. $password = '';
  31. $dbname = 'WT';
  32.  
  33. // Create connection
  34. $conn = mysqli_connect($host, $username, $password, $dbname);
  35.  
  36. // Check connection
  37. if (!$conn) {
  38.    die("Connection failed: " . mysqli_connect_error());
  39. }
  40.  
  41. // Check if form data is submitted
  42. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  43.    // Get user ID, name, and email from form input
  44.    $id = $_POST['id'];
  45.    $name = $_POST['name'];
  46.    $email = $_POST['email'];
  47.  
  48.    // Prepare the SQL query
  49.    $sql = "UPDATE users SET username = '$name', email = '$email' WHERE id = $id";
  50.  
  51.    // Execute the query
  52.    if (mysqli_query($conn, $sql)) {
  53.        echo "User updated successfully";
  54.    } else {
  55.        echo "Error updating user: " . mysqli_error($conn);
  56.    }
  57. }
  58.  
  59. // Close the connection
  60. mysqli_close($conn);
  61. ?>
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement