简体   繁体   中英

Failed to load resource: the server responded with a status of 500 (); works in local environment

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() 位置

On line 34 in logincrud/main.php:

第 34 行不工作

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM