Simple web form and table that stores text data in a MySQL database. I am using Ubuntu 16.04, and the newest forms of MySQL and Apache. The site has 2 pages, one that shows the data that is in the table, and the other is the form that allows the user to input new data.
- Ubuntu 16.04
- MySQL version 14.14 Distrib 5.7.17
- PHP 7.0.15
- Apache2 2.4.18
Problem: My code does not appear to be connecting to the MySQL database. According to Fred -ii- my PHP may not be properly configured. The link below this is the tutorial I followed to set everything up.
Below are screenshots of my code, and the home page that should display the data. I felt the form page was kind of pointless because it shows no error code on the page or on clicking submit. If you need more information please let me know!
HTML For Page with the Table:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Technological Pioneers</title>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<font face="verdana"><center><h1>Technological Pioneers</h1></center>
<br>
<center><a href="input.html">Click here to add a name to the list!</a></center></font>
<?php
$servername = "localhost";
$username = "XXXXX";
$password = "XXXXX";
$dbname = "Apollo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM Employees");
?>
<table font face="verdana" style= "background-color: #b0e0e6; color: #000000; margin: 0 auto;" >
<thead>
<tr>
<th>Employee Name</th>
<th>Job Title</th>
<th>Job Summary</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr>
<td>{$row\['name'\]}</td>
<td>{$row\['title'\]}</td>
<td>{$row\['summary'\]}</td>
</tr>\n";
}
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
</body>
</html>
HTML for Form:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Make your mark!</title>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<center>
<font face="verdana">
<h1>Make your mark on history!</h1><br>
<p>Know someone who worked on the Apollo Program? Did you work on Apollo? <br> Add the NASA employee's name to our database here, so their contribution will always be remembered.</p>
</center><
<center>
<form action="store.php" method="POST">
Employee Name:<br>
<input type="text" name="name" size="30" maxlength="60" autofocus ><br><br>
Job Title:<br>
<input type="text" name="title" size="30" maxlength="60" ><br><br>
Job Summary:<br>
<textarea rows="4" cols="50" name="summary" maxlength="249"></textarea><br><br>
<input type="submit" name="submit" value="Submit">
</form></font>
</center>
</body>
</html>
PHP To Store Data:
<?php
$servername = "localhost";
$username = "XXXXX";
$password = "XXXXX";
$dbname = "Apollo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Employees (name, title, summary)
VALUES ('name', 'title', 'summary')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>