Advertisement
thotfrnk

week6 & 7.html & php

Oct 19th, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.46 KB | None | 0 0
  1. index.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>Retrieving, Updating and Deleting data</title>
  8.   <link rel="stylesheet" href="styles.css">
  9. </head>
  10. <body>
  11.   <div class="container">
  12.     <div class="nav">
  13.       <ul>
  14.         <li><a href="index.html">Home</a></li>
  15.       </ul>
  16.     </div>
  17.     <div class="content">
  18.       <!--to retrieve data-->
  19.  
  20.       <form action="retrieve.php" method="post" id="retrieve">
  21.  
  22.         <fieldset>
  23.  
  24.           <legend>Retrieve Data</legend>
  25.  
  26.           User ID <input type="text" name="userID-retrieve" id="userID-retrieve">
  27.  
  28.           Last Name <input type="text" name="lname-retrieve" id="lname-retrieve">
  29.  
  30.           Email <input type="email" name="email-retrieve" id="email-retrieve">
  31.           <input type="submit" name="retrieve" value="retrieve">
  32.  
  33.         </fieldset>
  34.  
  35.       </form>
  36.  
  37.       <br>
  38.  
  39.       <!--to update data-->
  40.  
  41.       <form action="update.php" method="post" id="update">
  42.  
  43.         <fieldset>
  44.  
  45.           <legend>Update Data</legend>
  46.  
  47.           <input type="hidden" id="upd-userID" name="upd-userID" value="lois_5" />
  48.  
  49.           First Name <input type="text" name="upd-fname" id="upd-fname"> <br>
  50.  
  51.           Last Name <input type="text" name="upd-lname" id="upd-lname">
  52.  
  53.           <input type="submit" name="update" value="update">
  54.  
  55.         </fieldset>
  56.  
  57.       </form>
  58.  
  59.       <br>
  60.  
  61.       <!--to delete data-->
  62.  
  63.       <form action="delete.php" method="post" id="delete">
  64.  
  65.         <fieldset>
  66.  
  67.           <legend>Delete Data</legend>
  68.  
  69.           User ID <input type="text" name="uid-delete" id="uid-delete">
  70.  
  71.           Email <input type="email" name="email-delete" id="email-delete">
  72.  
  73.           <input type="submit" name="delete" value="delete">
  74.  
  75.         </fieldset>
  76.  
  77.       </form>
  78.  
  79.       <br><br>
  80.  
  81.     </div>
  82.    
  83.     <div class="footer">
  84.       &copy; adnagee creations inc.
  85.     </div>
  86.   </div>
  87.  
  88. </body>
  89. </html>
  90.  
  91. retrieve.php
  92. <!DOCTYPE html>
  93. <html lang="en">
  94. <head>
  95.   <meta charset="UTF-8">
  96.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  97.   <title>Retrieving data</title>
  98.   <link rel="stylesheet" href="styles.css">
  99. </head>
  100. <body>
  101.   <div class="container">
  102.     <div class="nav">
  103.       <ul>
  104.         <li><a href="index.html">Home</a></li>
  105.       </ul>
  106.     </div>
  107.     <div class="content">
  108.       <h2>Retrieving data from the database</h2>
  109.       <p>This process still requires the 5 steps as when we stored data to the database i.e. </p>
  110.       <ol>
  111.         <li>Connect to the database</li>
  112.         <li>Retrieve the form data</li>
  113.         <li>Create the query string</li>
  114.         <li>Execute the query</li>
  115.         <li>Provide user feedback, based on the success of the query</li>
  116.       </ol>
  117.       <p>What we should pay particular attention to though is that step 5 is more involved as we have to process the retrieved data by adding it dynamically to the web page. <br> As you will notice in this case the $result variable will not simply hold TRUE or FALSE, but will instead contain a <strong>resultset</strong> object which is essentially a 2-dimensional array or table-like data structure.</p>
  118.       <?php
  119.  
  120.       // set database variables
  121.       $server = 'localhost';
  122.       $user = 'root';
  123.       $password = ''; //there is no password for the current setup of MySQL
  124.       $database = 'ITEC244_2410';
  125.       //make the actual connection to MySQL and the chosen database
  126.       $conn = mysqli_connect($server, $user, $password, $database);
  127.       //if the connection failed print error message
  128.       if (!$conn) {
  129.         die('Database Connection failed ' . mysqli_connect_error());
  130.       }
  131.       echo "Successful connection to $database ";
  132.       //establish connection and select the database as shown previously
  133.       //retrieve the form data
  134.       $uid = $email = $lname = ""; //initialize the variables to empty strings
  135.       if (!empty($_POST['userID-retrieve']))
  136.         $uid = $_POST['userID-retrieve'];
  137.       if (!empty($_POST['email-retrieve']))
  138.         $email = $_POST['email-retrieve'];
  139.       if (!empty($_POST['lname-retrieve']))
  140.         $lname = $_POST['lname-retrieve'];
  141.  
  142.  
  143.  
  144.       //establish connection and select the database as shown previously
  145.       //retrieve the form data as shown previously
  146.       //Create the query string
  147.       $query = "select * from fans where userid = '$userID' OR email = '$email' OR lname = '$lname';";
  148.       //establish connection and select the database as shown previously
  149.       //retrieve the form data as shown previously
  150.       //create the query string
  151.       //execute the query
  152.       $result = mysqli_query($conn, $query);
  153.       if ($result) {
  154.         if (mysqli_num_rows($result) > 0) {
  155.           // if we are in this block it means that $result was NOT false and it contained
  156.           // at least one row of data... so let's process the data one row at a time
  157.           while ($row = mysqli_fetch_assoc($result)) {
  158.             // if we are in this inner block it means that we just successfully retrieved
  159.             // a row of data from the resultset held in $result. We can embed this in
  160.             // our HTML page as shown below
  161.             echo "<h4 class=\"standout\">$row[userID] <br>
  162.            <img src='$row[profile_pic]'  alt='profile' /></h4><p>Full Name: $row[fname] $row[lname]
  163.        <br>Email Address: $row[email]</p>";
  164.           } //end while loop
  165.         } //end if 1 or more rows
  166.         else
  167.           echo "<br>No rows returned";
  168.       }
  169.       mysqli_close($conn);
  170.       ?>
  171.     </div>
  172.     <div class="footer">
  173.       &copy; adnagee creations inc.
  174.     </div>
  175.   </div>
  176.  
  177. </body>
  178. </html>
  179.  
  180. update.php
  181. <!DOCTYPE html>
  182. <html lang="en">
  183. <head>
  184.   <meta charset="UTF-8">
  185.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  186.   <title>Updating data</title>
  187.   <link rel="stylesheet" href="styles.css">
  188. </head>
  189. <body>
  190.   <div class="container">
  191.     <div class="nav">
  192.       <ul>
  193.         <li><a href="index.html">Home</a></li>
  194.       </ul>
  195.     </div>
  196.     <div class="content">
  197.       <h2>Updating data in database</h2>
  198.       This process still requires the 5 steps as when we stored and retrieved data to/from the database i.e.
  199.       <ol>
  200.         <li>Connect to the database</li>
  201.         <li>Retrieve the form data</li>
  202.         <li>Create the query string</li>
  203.         <li>Execute the query</li>
  204.         <li>Provide user feedback, based on the success of the query</li>
  205.       </ol>
  206.       <p>If the update is unsuccessful for any reason the $result variable will contain FALSE, otherwise it will be TRUE.</p>
  207.       <?php
  208.  
  209.        // set database variables
  210.        $server = 'localhost';
  211.        $user = 'root';
  212.        $password = ''; //there is no password for the current setup of MySQL
  213.        $database = 'ITEC244';
  214.        //make the actual connection to MySQL and the chosen database
  215.        $conn = mysqli_connect($server, $user, $password, $database);
  216.        //if the connection failed print error message
  217.        if (!$conn) {
  218.          die('Database Connection failed ' . mysqli_connect_error());
  219.        }
  220.        echo "Successful connection to $database ";
  221.  
  222.        //retrieve the form data
  223.        $fname = $lname = "";
  224.        $userID = $_POST['upd-userID'];
  225.        //retrieve the hidden data from the form
  226.        if (!empty($_POST['upd-fname']))
  227.        $fname = $_POST['upd-fname'];
  228.        if (!empty($_POST['upd-lname']))
  229.        $lname = $_POST['upd-lname'];
  230.  
  231.        //Create the query string
  232.        $query = "update fans set fname = '$fname', lname = '$lname' where userID = '$userID';";  
  233.        //execute the query
  234.        $result = mysqli_query($conn, $query);
  235.  
  236.        //process the results and provide feedback          
  237.        if ($result)
  238.        {
  239.         //query successfully executed in MySQL  
  240.         $rows_aff = mysqli_affected_rows($conn);
  241.         //retrieves the number of rows updated              
  242.         if ($rows_aff > 0) {// at least 1 row was updated                
  243.           echo "$rows_aff rows were updated as requested. <br><br>";            
  244.         }
  245.         else
  246.          {
  247.           // query executed but no rows were updated                
  248.           echo "Sorry no rows were found for user ID $userID. <br><br>";          
  249.          }          
  250.          }
  251.          else {              
  252.           echo "Sorry, there was an error in processing this update. <br><br>";          
  253.       }          
  254.  
  255.          //close the connection        
  256.          mysqli_close($conn);
  257.  
  258. ?>
  259.     </div>
  260.     <div class="footer">
  261.       &copy; Priyanka & Kareena creations inc.
  262.     </div>
  263.   </div>
  264.  
  265. </body>
  266. </html>
  267.  
  268. delete.php
  269. <!DOCTYPE html>
  270. <html lang="en">
  271. <head>
  272.   <meta charset="UTF-8">
  273.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  274.   <title>Deleting data</title>
  275.   <link rel="stylesheet" href="styles.css">
  276. </head>
  277. <body>
  278.   <div class="container">
  279.     <div class="nav">
  280.       <ul>
  281.         <li><a href="index.html">Home</a></li>
  282.       </ul>
  283.     </div>
  284.     <div class="content">
  285.       <h2>Deleting data from database</h2>
  286.      <p>Once again, this process requires the 5 steps as when we stored, retrieved and updated data to/from the database i.e. </p>
  287.       <ol>
  288.         <li>Connect to the database</li>
  289.         <li>Retrieve the form data</li>
  290.         <li>Create the query string</li>
  291.         <li>Execute the query</li>
  292.         <li>Provide user feedback, based on the success of the query</li>
  293.       </ol>
  294.       <p>Like the update query, if the delete is unsuccessful for then the $result variable will contain FALSE, otherwise it will be TRUE.</p>
  295.  
  296.       <?php
  297.  
  298. // set database variables
  299. $server = 'localhost';
  300. $user = 'root';
  301. $password = ''; //there is no password for the current setup of MySQL
  302. $database = 'ITEC244';
  303. //make the actual connection to MySQL and the chosen database
  304. $conn = mysqli_connect($server, $user, $password, $database);
  305. //if the connection failed print error message
  306. if (!$conn) {
  307.   die('Database Connection failed ' . mysqli_connect_error());
  308. }
  309. echo "Successful connection to $database ";
  310.  
  311. //retrieve the form data
  312. $del_userID = $del_email = "";
  313. if (isset($_POST['userID-delete']))
  314. $del_userID = $_POST['userID-delete'];
  315. if (isset($_POST['email-delete']))
  316. $del_email = $_POST['email-delete'];  
  317.  
  318. //Create the query string
  319. $query = "delete from fans where userID = '$del_userID' OR email = '$del_email';";  
  320. //execute the query
  321. $result = mysqli_query($conn, $query);  
  322.  
  323. //process the results and provide feedback          
  324. if ($result) { //query successfully executed in MySQL            
  325.   $rows_aff = mysqli_affected_rows($conn);
  326.   //retrieves the number of rows updated              
  327.   if ($rows_aff > 0) { // at least 1 row was deleted              
  328.     echo "$rows_aff row(s) were deleted for user with ID $del_userID or email $del_email as requested. <br><br>";            
  329.   } else
  330.   {
  331.     // query executed but no rows matched the criterion of the where clause.                      
  332.     echo "Sorry no rows were found for user with ID $del_userID or email $del_email. <br><br>";            
  333.    }          
  334.   }
  335.   else
  336.   {            
  337.      echo "Sorry, there was an error in processing this delete. <br><br>";          
  338.     }    
  339.        
  340. //close the connection          
  341. mysqli_close($conn);
  342.  
  343. ?>
  344.     </div>
  345.     <div class="footer">
  346.       &copy; Priyanka & Kareena creations inc.
  347.     </div>
  348.   </div>
  349.  
  350. </body>
  351. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement