write_on_file: file = $file,

\n"; echo "

name = $this->name,

\n"; echo "

surname = $this->surname,

\n"; echo "

serial_number = $this->serial_number,

\n"; echo "

email = $this->email,

\n"; echo "

phone = $this->phone,

\n"; echo "

laurea_degree = $this->laurea_degree,

\n"; } /* 1) open the file $file in append mode, 2) write the student data into the file $file and then 3) close the file $file. */ $handle = fopen($file, "a"); fwrite($handle,"$this->name\t$this->surname\t$this->serial_number\t$this->email\t$this->phone\t$this->laurea_degree\n"); fclose($handle); } /* Declare a public (by default) method named "write_on_database": */ function write_on_database($database,$table) { /* By using the following variables I am referring to those variables whose scope is global (defined outside the function). On the other hand, I give $database and $table as input to the function. */ global $mysql_host, $mysql_user, $mysql_password; if (DEBUG){ echo "

write_on_database: mysql_host = $mysql_host, mysql_user = $mysql_user, mysql_password = $mysql_password

\n"; echo "

write_on_database: database = $database, table = $table

\n"; echo "

writeing the following data

\n"; echo "

name = $this->name,

\n"; echo "

surname = $this->surname,

\n"; echo "

serial_number = $this->serial_number,

\n"; echo "

email = $this->email,

\n"; echo "

phone = $this->phone,

\n"; echo "

laurea_degree = $this->laurea_degree.

\n"; } // Connecting to the MySQL server: $link = mysqli_connect($mysql_host, $mysql_user, $mysql_password) or die('Could not connect: ' . mysqli_error($link)); if (DEBUG){ echo "

Connected successfully to the server !!

\n"; } // Selecting the database: mysqli_select_db($link,$database) or die('Could not select database'); if (DEBUG){ echo "

Successfully selected the database !!

\n"; } // If $table doesn't exists then create one with 7 fields (i. e., columns) as follows: // first create the SQL query, $query = "CREATE TABLE IF NOT EXISTS `$table` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `when` TIMESTAMP DEFAULT NOW(), `name` VARCHAR(30), `surname` VARCHAR(30), `serial_number` INT(6) UNSIGNED, `email` VARCHAR(40), `phone` CHAR(15), `laurea_degree` VARCHAR(100) );"; if (DEBUG){ echo "

".$query."

\n"; } // then submit the query to the selected $database. mysqli_query($link,$query) or die('Query failed: ' . mysqli_error($link)); // Insert data into $table. Note that the first two field are computed (see $table definition just above). // Again, first create the SQL query, $query = "INSERT INTO `$table` (`name`,`surname`,`serial_number`,`email`,`phone`,`laurea_degree`) VALUES ( '$this->name', '$this->surname', $this->serial_number, '$this->email', '$this->phone', '$this->laurea_degree' );"; if (DEBUG){ echo "

".$query."

\n"; } // then submit the query to the selected $database. mysqli_query($link,$query) or die('Query failed: ' . mysqli_error($link)); // Close the connection to the MySQL server. mysqli_close($link); } /* Declare a public (by default) method named "write_in_output": */ function write_in_output() { /* Echo the student properties. To be continued. */ echo "Name: $this->name,
Surname: $this->surname,
Serial number: $this->serial_number,
eccetera (To be continued)."; } } /* End of student class definition */ /* Defining a function which outputs the $database $table in nice HTML format */ function bump_table($database,$mysql_host,$mysql_user,$mysql_password,$table) { // Connecting and then selecting database $link = mysqli_connect($mysql_host, $mysql_user, $mysql_password) or die('Could not connect: ' . mysqli_error($link)); mysqli_select_db($link,$database) or die('Could not select database'); if (DEBUG){ echo "

Connected and selected successfully the database !!

\n"; } // Getting all the table data with the following SQL query. // First create the SQL query, $query = "SELECT * FROM $table ORDER BY name DESC"; if (DEBUG){ echo "

".$query."

\n"; } // then submit the query to the selected $database to get the data (into $result). $result = mysqli_query($link,$query) or die('Query failed: ' . mysqli_error($link)); // Printing results in an HTML table: echo "\n"; // first write the first two lines: // see man page for mysql_fetch_array. $line = mysqli_fetch_array($result, MYSQLI_ASSOC); // line 1: writing the keys. echo "\t\n"; foreach ($line as $key => $col_value) { echo "\t\t\n"; } echo "\t\n"; // line 2: writing the first row of col_values. echo "\t\n"; foreach ($line as $key => $col_value) { echo "\t\t\n"; } echo "\t\n"; // Hence, I write the remaining once. while ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) { echo "\t\n"; foreach ($line as $key => $col_value) { echo "\t\t\n"; } echo "\t\n"; } // close the table. echo "
$key
$col_value
$col_value
\n"; // Free resultset mysqli_free_result($result); // Closing connection mysqli_close($link); } //MAIN PROGRAM (this is the global scope): // database and file data: $mysql_host = 'localhost'; $mysql_user = 'icto201920'; $mysql_password = ''; $database = 'my_icto201920'; $table = 'student_data_table'; $file = 'student_data_file.txt'; // creating/instanziating one student object: $current_student = new student; // fill the student object with the data from "student_registration_form_2.html" $current_student -> name = $_POST['name']; $current_student -> surname = $_POST['surname']; $current_student -> serial_number = (int)$_POST['serial_number']; // Note the integer cast (why did I put it ?) $current_student -> email = $_POST['email']; // $current_student -> phone = $_POST['phone']; To be implemented. $current_student -> laurea_degree = $_POST['laurea_degree']; /* write the above data in the output file (the one sent to the client): */ $current_student -> write_in_output(); /* write the above data in the file $file in the web server. */ $current_student -> write_on_file($file); /* write the above data in the table $table of the database $database. Note: the MySQL server data are passed to the function by using the keyword 'global' (it may not be an ortodox way of doing). */ $current_student -> write_on_database($database,$table); /* write the table $table data of the database $database in nice HTML into the output file. Note: here instead, the MySQL server data are passed as input to the function. */ bump_table($database,$mysql_host,$mysql_user,$mysql_password,$table); ?>