0

I am trying to display and later amend the data inserted into this database on my web page. But the data is not displaying. I corrected my mistakes from my previous post and tried to troubleshoot. I believe the connection to the database is working because I am able to insert data into the database.

 /*top of page invites.php*/
<?php 
include("LoginRegistrationSystem/connect.php");
include("guestlist.php");
include("functions.php");
if(logged_in())
{

?>

    /*in invites.php*/

   <?php
  $sql = "SELECT fname, lname, email, contact  FROM guestlist";
  $resultset = mysqli_query($con, $sql) or die("database error:". 
mysqli_error($con));

  echo "<table border="2">
        <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Contact</th>
      <th>Invitation</th>
    </tr>";

  while($row = mysqli_fetch_array($resultset)) 
   { 

        echo "<tr>";
          echo"<td>" . $row['fname'] . "</td>";
          echo"<td>" . $row['lname']. "</td>";
          echo"<td>" . $row['email']. "</td>";
          echo"<td>" . $row['contact']. "</td>";
        echo "<tr>";
}
        echo "</table>";
        mysqli_close($con);
 ?> 



/*LoginRegistrationSystem/connect.php*/
    <?php

    $con = mysqli_connect("localhost","root","","registration");

    if(mysqli_connect_errno())
    {
        echo "Error occured while connecting with database 
".mysqli_connect_errno();
        }

    session_start();
?>
3
  • So you have that code inside of 'invites.html'? That shouldn't work because this doesn't get interpreted as php. Otherwise where does the code stop? Commented Sep 13, 2017 at 0:24
  • I apologies it is invites.php. Commented Sep 13, 2017 at 0:34
  • You cannot use " in echo with ". It should be echo '<table border="2"> instead. Commented Sep 13, 2017 at 0:38

2 Answers 2

1

There is one major error I'm spotting. Near the top of the code, you have a conditional:

if(logged_in()) {

but no closing brace to match it. If your page isn't loading at all, this is likely the problem.

If you fix this and the table still doesn't display, make sure that you have permissions to SELECT on the guestlist table. It would be odd, considering that you can INSERT into the database, but check it nonetheless. Permissions errors been a huge cause of headache for me in the past.

As a bit of friendly advice, you might also want to consider switching from mysqli_fetch_array to mysqli_fetch_assoc. While this will not change how your program functions for the purposes here, with mysqli_fetch_array the results are indexed both numerically and associatively, i.e. there is both a $row[0] and a $row['fname'] even though they are the same thing. mysqli_fetch_assoc returns only the associative array, i.e. there are no numerical indices in the return value.

Also, you should indent your code properly. It will drastically improve readability and help you with debugging.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi. I forgot to mention I ended with <!--End PHP--> else part for the top. Thank you for clarifying my doubt with clear explanation of assoc and array. How can I fix the permission error?
@alexvin The general SQL syntax is GRANT [permission] ON [object] TO [username]. Full documentation for the GRANT command can be found at learn.microsoft.com/en-us/sql/t-sql/statements/…. MySQL Workbench also has a nice GUI for this.
0

Try this: while($row = mysqli_fetch_assoc($resultset)){ .... }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.