In: Computer Science
Create a PHP class named "User" with the following private fields:
name,
birthdate in yyyy/mm/dd format,
age, and
department.
In the class, include getter and setter methods to get and set the values of those variables.
Author a data entry webform using HTML text input boxes and a submit button. When the user clicks on the submit button, a "User" class is instantiated and the new User object's fields are populated. The HTML input boxes correspond to the field in the "User" class.
Add an "Insert" button that allows the user to insert the form data to a MySQL table.
Add a "Display" button the displays the contents of your MySQL table on the web form when the end-user clicks on the button.
ANSWER:
<! DOCTYPE html>
<html>
<head>
<mete http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="insert1.php"method="post">
name:<input type="text" name="name"><br>
Birthday:<input type"date" name="birthday"><br>
Age:<input type ="text" name="age"><br>
Department : <input type ="text" name="department"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'password');
define('DBNAME', 'database');
$db = new mysqli(HOST, USER, PASS, DBNAME);
if ($db=>connect_errno) {echo "failed to connect to mySQL:(" .$db->connect_errno .") " . $db->connect _error;}
if ($_POST['submit']) {
$name = $_POST[ 'name'];
$birthday =$_POST['birthday'];
$age = $POST['age'];
$department =$POST['department'];
if(!empty($name) && !empty($birthday) && !empty($age) && !empty($department)) {
$query = "INSERT INTO form _data (name, birthday , age ,department)
VALUES(?,?,?)" ;
$conn = $db =>prepare($query);
if ($conn ->execute(array(
$name,
$birthday,
$age,
$department
))){
header('location : http://localhost/insert.html');
} else {
echo ' error' ;
}
$db->close();