1

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.

https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04

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!

enter image description here

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();
?>
Michael Watts
  • 365
  • 1
  • 4
  • 8
  • 2
    images of code; why? what if someone needs to retype something? – Funk Forty Niner Apr 11 '17 at 16:54
  • you can add code in question box. Please update your question with code pasted in it. – Murtaza Bhurgri Apr 11 '17 at 17:13
  • 2
    Possible duplicate of [PHP code is not being executed, instead code shows on the page](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – Funk Forty Niner Apr 11 '17 at 17:16
  • 1
    One problem: the code which is getting dumped to your browser says `connect_erroer)`, while the code you posted does not have the typo, suggesting that you made some change at some point, thus there's no way of telling if the problem is related. Post code, not pictures. – alanlittle Apr 11 '17 at 17:17
  • I'm sorry about that guys. Changes have been made. I see where my connect_error typo was and have gone and fixed it. – Michael Watts Apr 11 '17 at 17:30
  • @MichaelWatts After seeing your edit: that isn't the only thing wrong with your code. Once you figured out why it's showing you "code" rather than parsed php directives, your code will fail. – Funk Forty Niner Apr 11 '17 at 17:35
  • @Fred-ii- I just went to the server to check that PHP is setup and I don't appear to be able to navigate to the info.php page that I made earlier. I will troubleshoot that a bit further and give an update. Thank you for pointing me in the right direction! – Michael Watts Apr 11 '17 at 17:41
  • @MichaelWatts welcome. Thing is, how are you trying to access the file(s) as; `http://localhost` (or hosted) or directly in your browser as `file:///`? and if they all are `.php` extensions. – Funk Forty Niner Apr 11 '17 at 17:43
  • http://localhost/info.php is what I have been trying. – Michael Watts Apr 11 '17 at 17:47
  • I now have the info.php page working. I had it in a folder that was not active with apache2. It is working now. So what should my next step in trouble shooting be? – Michael Watts Apr 11 '17 at 17:53

1 Answers1

0

The fact that your code appears on the outputted HTML, suggests that PHP is not parsed/executed. Are you sure you have everything setup correctly on the Apache/PHP side?

Orestis Samaras
  • 123
  • 2
  • 9
  • 1
    Agreed. However this part of your answer *"Are you sure you have everything setup correctly on the Apache/PHP side?"* should have read something like *"Make sure you have everything setup correctly on the Apache/PHP side."* and not asking a question. Those are comments, just saying. – Funk Forty Niner Apr 11 '17 at 17:15