summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSudipto Mallick <smlckz@termux-alpine>2024-01-24 17:16:15 +0000
committerSudipto Mallick <smlckz@termux-alpine>2024-01-24 17:16:15 +0000
commita5afdcfa7165e100042f03de1b3f7a601389776f (patch)
tree3df68011167d9b717017717bbb930140daaf23a5
parent6766b6479a1d30680c1eb59df4c4b720eb22ce1c (diff)
downloadzadania-a5afdcfa7165e100042f03de1b3f7a601389776f.tar.gz
Rewrite PHP assignment #1
mysql-php/code/a1.php: Perform some syntactic and structural changes.
-rw-r--r--mysql-php/code/a1.php64
1 files changed, 22 insertions, 42 deletions
diff --git a/mysql-php/code/a1.php b/mysql-php/code/a1.php
index 107f07a..4569ca8 100644
--- a/mysql-php/code/a1.php
+++ b/mysql-php/code/a1.php
@@ -59,6 +59,7 @@ function display_menu() {
 
 function display_search_form() {
     html_prologue('Student details');
+    /* $_SERVER['PHP_SELF'] is approximately equal to $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'] */
     ?>
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>">
     <h2>Enter E-mail address to search for the record of a student</h2>
@@ -117,21 +118,12 @@ function insert_details($dbh) {
         'city' => 'City',
         'roll' => 'Roll No.'
     ]);
-    $stmt = mysqli_prepare($dbh, 'INSERT INTO STUDENT (
-        ROLL, NAME, EMAIL, CITY, DATE_OF_BIRTH
-    ) VALUES (?,?,?,?,?)');
-    mysqli_stmt_execute($stmt, [
-        $vars['roll'], $vars['name'], $vars['email'], $vars['city'], 
-        $vars['date_of_birth']
-    ]);
+    $stmt = mysqli_prepare($dbh, 'INSERT INTO STUDENT (ROLL, NAME, EMAIL, CITY, DATE_OF_BIRTH) VALUES (?,?,?,?,?)');
+    mysqli_stmt_bind_param($stmt, 'sssss', $vars['roll'], $vars['name'], $vars['email'], $vars['city'], $vars['date_of_birth']);
+    mysqli_stmt_execute($stmt);
 }
 
-function show_table($dbh) {
-    $result = mysqli_query($dbh, 'SELECT * FROM STUDENT');
-    html_prologue('Students\' details');
-    ?>
-<h2>Students' details</h2>
-<p><?php echo mysqli_num_rows($result); ?> record(s) found.</p>
+function show_table($result) { ?>
 <table>
     <tr>
         <th>Roll No.</th>
@@ -139,48 +131,36 @@ function show_table($dbh) {
         <th>E-mail</th>
         <th>City</th>
         <th>Date of birth</th>
-    </tr><?php
-    while ($row = mysqli_fetch_assoc($result)) { ?>
+        </tr>
+    <?php while ($row = mysqli_fetch_assoc($result)) { ?>
     <tr><td>
-        <?php echo implode('</td><td>', array_map('htmlspecialchars', [
-            $row['ROLL'], $row['NAME'], $row['EMAIL'], $row['CITY'],
-            $row['DATE_OF_BIRTH']		
-        ])); ?>
+        <?php echo implode('</td><td>', array_map('htmlspecialchars', [$row['ROLL'], $row['NAME'], $row['EMAIL'], $row['CITY'], $row['DATE_OF_BIRTH']])); ?>
     </td></tr>
-    <?php
-    } ?>
+    <?php } ?>
 </table>
+<?php }
+
+function show_table_normal($dbh) {
+    if(!($result = mysqli_query($dbh, 'SELECT * FROM STUDENT')))
+        display_failure('Could not perform query: ' . mysqli_error($dbh));
+    html_prologue('Students\' details');
+    ?>
+<h2>Students' details</h2>
+<p><?php echo mysqli_num_rows($result); ?> record(s) found.</p>
 <?php
+    show_table($result);
 }
 
 function show_table_dob_range($dbh) {
-    if (!($result = mysqli_query($dbh, 'SELECT * FROM STUDENT 
-            WHERE YEAR(DATE_OF_BIRTH) BETWEEN 2000 AND 2005')))
+    if (!($result = mysqli_query($dbh, 'SELECT * FROM STUDENT WHERE YEAR(DATE_OF_BIRTH) BETWEEN 2000 AND 2005')))
         display_failure('Could not perform query: ' . mysqli_error($dbh));
     html_prologue('Students\' details');
     ?>
 <h2>Students' details</h2>
 <p>The students who were born in years 2000 to 2005, inclusive.</p>
 <p><?php echo mysqli_num_rows($result); ?> record(s) found.</p>
-<table>
-    <tr>
-        <th>Roll No.</th>
-        <th>Name</th>
-        <th>E-mail</th>
-        <th>City</th>
-        <th>Date of birth</th>
-    </tr><?php
-    while ($row = mysqli_fetch_assoc($result)) { ?>
-    <tr><td>
-        <?php echo implode('</td><td>', array_map('htmlspecialchars', [
-            $row['ROLL'], $row['NAME'], $row['EMAIL'], $row['CITY'],
-            $row['DATE_OF_BIRTH']		
-        ])); ?>
-    </td></tr>
-    <?php
-    } ?>
-</table>
 <?php
+    show_table($result);
 }
 
 function search_student($dbh, $email) {
@@ -224,7 +204,7 @@ if ($path === 'insert') {
     }
 } elseif ($path === 'display') {
     $dbh = connect_to_database();
-    show_table($dbh);
+    show_table_normal($dbh);
     mysqli_close($dbh);
 } elseif ($path === 'disp-dob-range') {
     $dbh = connect_to_database();