Search code examples
phphtmllamp

LAMP Stack - Update HTML Table - Not mapping to/from DB


Hello I have a basic HTML form Index.html

<html>
 <body>
 <form action="update.php" method="POST">
     Department: <input type="text" name="department"><br><br>
     Subname: <input type="text" name="subname"><br><br>
     Labels: <input type="text" name="labels"><br><br>
     Pagerduty: <input type="text" name="pagerduty"><br><br>
     Description: <input type="text" name="description"><br><br>
     <input type="submit" value="Submit" name="submit">
 </form>
</body>
</html>

On submit the following PHP script is executed to update my DB with the details. update.php

<?php
$hostname = "localhost";
$username = "root";
$password = "xxxxxxxxxxx";
$db = "dora";
$dbconnect=mysqli_connect($hostname,$username,$password,$db);
if ($dbconnect->connect_error) {
 die("Database connection failed: " . $dbconnect->connect_error);
}
if(isset($_POST['submit'])) {
 $department=$_POST['department'];
 $subname=$_POST['subname'];
 $labels=$_POST['labels'];
 $pagerduty=$_POST['pagerduty'];
 $description=$_POST['description'];
$query = "INSERT INTO dora (department, subname, labels, pagerduty, description)
VALUES ('$department', '$subname', '$labels', '$pagerduty', '$description')";
if (!mysqli_query($dbconnect, $query)) {
      die('An error occurred when submitting your review.');
  } else {
    echo "Thanks for your review.";
  }
  }
?>

I then want another page showing a HTML table with the contents from the DB Main.html

<html>
<body>
<?php
$hostname = "localhost";
$username = "root";
$password = "xxxxxxxx";
$db = "dora";
$dbconnect=mysqli_connect($hostname,$username,$password,$db);
if ($dbconnect->connect_error) {
  die("Database connection failed: " . $dbconnect->connect_error);
}
?>
<table border="1" align="center">
<tr>
  <td>Department</td>
  <td>Subname</td>
  <td>Labels</td>
  <td>Pagerduty</td>
  <td>Description</td>
</tr>
<?php
$query = mysqli_query($dbconnect, "SELECT * FROM dora")
   or die (mysqli_error($dbconnect));

while ($row = mysqli_fetch_array($query)) {
  echo
   "<tr>
    <td>{$row['department']}</td>
    <td>{$row['subname']}</td>
    <td>{$row['labels']}</td>
    <td>{$row['pagerduty']}</td>
    <td>{$row['description']}</td>
   </tr>\n";
}
?>
</table>
</body>
</html>

Everything works fine apart from two columns aren't mapping across, namely Subname & Labels

I've double checked my code but I cant spot the mistake, can anyone help me out?

I've restarted https etc. but nothing seems to work :(

DORA SCHEMA

MariaDB [dora]> show columns from dora;

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| TAB_ID      | int(11)      | NO   | PRI | NULL    | auto_increment |
| department  | varchar(200) | YES  |     | NULL    |                |
| subname     | varchar(200) | YES  |     | NULL    |                |
| labels      | varchar(200) | YES  |     | NULL    |                |
| pagerduty   | varchar(200) | YES  |     | NULL    |                |
| description | varchar(200) | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

The results from my table

Department  Subname Labels  Pagerduty   Description
1                             4              5
1                             4              5
sdfsdf                        adfasdfad      adfadfadfa

Solution

  • I tested your code and it is working fine . the only difference is that im using InnoDB which should be the same. Try the following:

    1 change those columns names.

    2 recreate your DB.

    3 make sure there is no trigers setting the two column to plank