If you want more of my pastes visit: https://randompaste.000webhostapp.com/index.html
--------------------------------------------------------------------------------------
<html>
<head>
<title>Task3</title>
</head>
<body>
<div id="body">
<?php include 'menu.inc'; ?>
<?php
//define server variables
$servername = "localhost";
$username = 'root' ;
$password= '';
$dbname = "task3";
//define connection
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM modules";
//output in a table form
echo "<h4>SQL Query using PDO showing results in Table form</h4>";
echo "<table style='border:1px solid black;'> ";
echo "<tr><th>Code</th><th>Name</th><th>Lecturer</th></tr>";
foreach ($conn->query($sql) as $row) {
echo "<tr> <td style='width:250px;border:1px solid black;'>" . $row["code"] .
"</td> <td style='width:250px;border:1px solid black;'>" . $row["name"] .
"</td> <td style='width:250px;border:1px solid black;'>" . $row["lecturer"] . "</td> </tr>";
}
//end for each
//defining variables to enter into database
$sqlValueCode="";
$sqlValueName="";
$sqlValueLecturer="";
?>
<!--ADD Table to receive data for database-->
</table>
<br>
<br>
<!--Add Form-->
<form action="task4.php" method="post">
<strong>Add Form</strong><br>
Code <br><input type="text" name="modCode"><br>
Module Name <br><input type="text" name="modName"><br>
Lecturer <br><input type="text" name="lectName"><br>
<input type="submit">
</form>
<?php
//reading in data from Add Form
$modCode = filter_input (INPUT_POST, 'modCode' );
$modName = filter_input (INPUT_POST, 'modName' );
$lectName = filter_input (INPUT_POST, 'lectName' );
//setting up variables to enter into database
$sqlValueCode=$modCode;
$sqlValueName=$modName;
$sqlValueLecturer=$lectName;
//testing output
//echo "<br>". $modCode . "<br>" . $modName . "<br>". $lectName ;
//define sql insert command
$sqlAdd= "INSERT INTO modules (code, name, lecturer) values ("."'$sqlValueCode',"." '$sqlValueName',"." '$sqlValueLecturer')";
//only inserting data if variables have been set
if ($sqlValueCode != NULL or $sqlValueName != NULL or $sqlValueLecturer != NULL) {
//executing sql insert command
$conn->exec($sqlAdd);
echo "entry added successfully";
} else {
echo "could not add the entry";
}
?>
<!--Update Form...................................................................-->
<br><br>
<form action="task4.php" method="post">
<strong>Update Form</strong><br>
Code <br><input type="text" name="modCode"><br>
Module Name <br><input type="text" name="modName"><br>
Lecturer <br><input type="text" name="lectName"><br>
<input type="submit">
</form>
</div>
</body>
</html>