Page 352 - Computer Software Application TP - Volume 1
P. 352

COMPUTER SOFTWARE APPLICATION - CITS




           ii  Create a PHP script (update.php) to connect to the database and handle the update process based on user
              input.
              1  Open the text editor

              2  Write the following codes
                       <html >
              <head> HTML form </title>
              </head>
              <body>
                         <?php

                // Database connection parameters
                $servername = “localhost”;  // Change this if  MySQL server is on a different host
                $username = “your_username”; // Change this to  MySQL username
                $password = “your_password”; // Change this to  MySQL password

                $database = “example_db”;    // Change this to  database name
               // Create connection
               $conn = new mysqli($servername, $username, $password, $database);
               // Check connection
               if ($conn->connect_error)

              {
                  die(“Connection failed: “ . $conn->connect_error);
               }
               // Check if the form is submitted

               if ($_SERVER[“REQUEST_METHOD”] == “POST”)
              {
                   // Get form data
                   //$id = $_POST[“id”];
                   $newUsername = $_POST[“username”];
                   $newEmail = $_POST[“email”];

                  // Prepare and execute the SQL statement to update data in the table
                  $stmt = $conn->prepare(“UPDATE users SET email = ? WHERE username = ?”);
                  $stmt->bind_param(“ss”,$newEmail,$newUsername);
                     if ($stmt->execute())

                {
                              echo “Record updated successfully”;
                  }
                else
                {

                               echo “Error: “ . $conn->error;
                  }




                                                           337

                               CITS : IT & ITES - Computer Software Application - Exercise 61
   347   348   349   350   351   352   353   354   355   356   357