Jump to content

500 - Internal server error on index.php


Go to solution Solved by mac_gyver,

Recommended Posts

Hey guys,

So I making a basic website form to do CRUD operations on a database, and all of my components work, but I keep getting 500 - Internal server error on my index.php

 

Heres my code:

<?php
require_once('config.php');
require_once('menu.php');
 
echo '<h1>View All Alien Interactions</h1>';
 
/* Start the table with the fields we want to display
Remember $fields is now in config.php */
echo '<table>
    <tr>';
foreach($fields AS $label){
    // th is a table header; the column's title or label.
    echo "<th>{$label}</th>";
}
//Add the edit and delete columns at the end of the table
echo '<th>Edit</th><th>Delete</th>';
echo '</tr>';
/*
Select the fields we want, from all the rows
The first line takes the array keys from our $fields array
and implodes them, using backticks and commas. The end result
will look like `first_name`, `last_name`, `phone_number`...
The next line creates a SELECT query using that string.
*/
 
$fields_str = '`contact_id`, `'.implode(array_keys($fields), '`, `').'`';
$sql = "SELECT {$fields_str} FROM `aliens_abduction`";
 
foreach($dbh->query($sql) as $row) {
    echo '<tr>';
    // Loop through the fields again to display them for this row.
        // Note: The tutorial originally contained an error here, this has been updated.
    foreach($fields AS $field=>$value){
        // if the field is blank, we want to empty a blank space, otherwise the HTML won't work properly
        echo '<td>'.(isset($row[$field]) && strlen($row[$field]) ? $row[$field] : '&nbsp'.'</td>');
    }
    echo '</tr>';
    echo '<td><a href="edit.php?contact_id='.$row['contact_id'].'">Edit</a></td>';
    echo '<td><a href="delete.php?contact_id='.$row['contact_id'].'">Delete</a></td>';
    echo '</tr>';
echo '</table>';
?>

and heres my config.php code (idk if this is the root of the problem, i dummied out my credentials):

<?php
//Connect to the database
$dbh = new PDO('mysql:host=xxxxxxxxx;dbname=db_demo', 'xxxxx', 'xxxxx');
//Set the default fetch mode to be an associative array.
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,  PDO::FETCH_ASSOC);
 
//Define the fields for our CRUD application
$fields = array(
    'first_name' => 'First Name',
    'last_name' => 'Last Name',
    'when_it_happened' => 'When it happened',
    'how_many' => 'How many',
    'alien_description' => 'Alien description',
    'what_they_did' => 'What they did',
    'fang_spotted' => 'Fang spotted',
    'email' => 'Email'
);
?>
Edited by tekkenfan2
Link to comment
https://forums.phpfreaks.com/topic/289345-500-internal-server-error-on-indexphp/
Share on other sites

  • Solution

the http 500 server error for php files is usually due to a fatal parse or a fatal runtime error. in your case, there's a fatal parse/syntax error in your code.

 

you need to set php's error_reporting to E_ALL and display_errors to ON in your php.ini on your development system to get php to help you by reporting and displaying all the errors it detects (putting these settings into your file won't help with fatal parse errors in your main file because your code never runs to modify the settings.)

 

as to what is causing the fatal parse/syntax error, it looks like you are missing a closing } for one of your foreach(...){ loops.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.