-1

I am working on posting a CRUD Application to where the user logs in first. The application is made with 2 MySQL tables; one for logging in and the other for the CRUD values. This example hasn’t worked on my local environment using both XAMP/PC and MAMP/MAC.

When I uploaded it to my host it does work, however the CRUD values don’t populate when it the for each loop is specified. I concatenated the mysql_error(); to explain why this is not working on my host.

mysql_error() location

On line 34 in logincrud/main.php:

line 34 is not working

The code from the table in main.php:

    <table class="table table-striped table-bordered">
              <thead>
                <tr>
                  <th>Number</th>
                  <th>Explanation</th>
                  <th>Date Accured</th>
                  <th>Action</th>
                </tr>
              </thead>
              <tbody>
              <?php
               include 'database.php';
               $pdo = Database::connect();
               $sql = 'SELECT * FROM saftey ORDER BY id DESC';
               foreach (($sql) as $row) {
                        echo '<tr>';
                        echo '<td>'. $row['ins_n'] . '</td>';
                        echo '<td>'. $row['explanation'] . '</td>';
                        echo '<td>'. $row['date'] . '</td>';
                        echo '<td width=250>';
                        echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
                        echo ' ';
                        echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
                        echo ' ';
                        echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
                        echo '</td>';
                        echo '</tr>';
               }
               Database::disconnect();
              ?>
              </tbody>

        </table>

The Add, Update, and Delete works on the host as I can see the records from the safety table on myphpadmin. The login/logoff is from a completely different table. Again, the local environment works with no issues.

I opened a help ticket with my host provider, but all I received is they do not troubleshoot Applications.

I have been looking for solutions for a few weeks and all I couldn’t find a solution. In the Chrome browser the console states the error: hp:1 Failed to load resource: the server responded with a status of 500 ()" Server issue? Aware of SQL Injection problems.

I uploaded a GITHUB project if it helps.

Thank you in advance.

Tryah85
  • 371
  • 3
  • 12
  • 1
    You do not execute your SQL query. Remove `mysql_error()` as it does not exist anymore in PHP. – Dharman Apr 25 '19 at 19:47
  • Possible duplicate of [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Dharman Apr 25 '19 at 19:48
  • I took the mysql_error() function out of the php file, a still Invalid Argument on line 34 :( – Tryah85 Apr 26 '19 at 04:48

1 Answers1

0

I solved the problem why it was not working on my live site. Looking at this Stackoverflow article The way the PHP Data Object has the forward arrow executes the code. The way this foreach works caused the value return false; in other words not in an array.

I reorganized the PHP Data Object into a variable known as $records.

foreach (($sql) as $row) {

to

$records = $pdo->query($sql);
                   foreach ($records as $row) {

changed the echo to print

}

Works now. Weird how echo is different than print in this situation.

<table class="table table-striped table-bordered">
                  <thead>
                    <tr>
                      <th>Number</th>
                      <th>Explanation</th>
                      <th>Date Accured</th>
                      <th>Action</th>
                    </tr>
                  </thead>
                  <tbody>
                  <?php
                   include 'database.php';
                   $pdo = Database::connect();
                   $sql = 'SELECT * FROM safety ORDER BY id DESC';
                   $records = $pdo->query($sql);
                   foreach ($records as $row) {
                            print '<tr>';
                            print '<td>'. $row['ins_n'] . '</td>';
                            print '<td>'. $row['explanation'] . '</td>';
                            print '<td>'. $row['date'] . '</td>';
                            print '<td width=250>';
                            print '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
                            print ' ';
                            print '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
                            print ' ';
                            print '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
                            print '</td>';
                            print '</tr>';
                   }
                   Database::disconnect();
Tryah85
  • 371
  • 3
  • 12