Here’s a perfect example I haven’t seen before in GPT4. This comes from a conversation about this code where the locations, functions and context has been explained in detail many times. My prompt starts after GPT made a mistake in a code block in the previous response and I tell it
Prompt:
There are 2 things wrong with your last response:
1: you forgot that the settings.php and business_profile.php files both reside in the settings/ folder. I’ve corrected this in the index.php file and settings.php now.
2: I get an error when I open the settings page:
Fatal error: Cannot redeclare fetch_business_profile_data() (previously declared in /home/u321618269/domains/solidslick.com/public_html/dev/wp-content/plugins/pro-repair/settings/business_profile.php:4) in /home/u321618269/domains/solidslick.com/public_html/dev/wp-content/plugins/pro-repair/settings/business_profile.php on line 3
This is settings/business_profile.php:
<?php
// Fetch Business Profile Data
function fetch_business_profile_data() {
global $wpdb;
// Fetch data from the database
$results = $wpdb->get_results("SELECT * FROM ersp_company", ARRAY_A);
// Return the data as a JSON response
wp_send_json($results);
}
// Add ajax action hook
add_action('wp_ajax_fetch_business_profile_data', 'fetch_business_profile_data');
?>
<tbody id="company-table">
<!-- data will be inserted here dynamically -->
</tbody>
This is settings/settings.php:
<div class="wrap">
<h1><?= esc_html(get_admin_page_title()); ?></h1>
<nav class="nav-tab-wrapper">
<a href="#business-profile" class="nav-tab" data-tab-target="business-profile">Business profile</a>
<a href="#reset-db" class="nav-tab" data-tab-target="reset-db">Reset DB</a>
</nav>
<div id="tab-content">
<div id="business-profile" class="tab-content" style="padding-left: 20px;"><?php include plugin_dir_path(__FILE__) . 'business_profile.php'; ?></div>
<div id="reset-db" class="tab-content" style="padding-left: 20px;"><?php include plugin_dir_path(__FILE__) . 'reset_db.php'; ?></div>
</div>
</div>
<script type="text/javascript" src="<?php echo plugin_dir_url(__FILE__) . 'scripts/settings.js'; ?>"></script>
And this is settings/scripts/settings.js:
jQuery(document).ready(function ($) {
$('.nav-tab').click(function (e) {
e.preventDefault();
// Switch tabs
$('.nav-tab').removeClass('nav-tab-active');
$(this).addClass('nav-tab-active');
$('.tab-content').hide();
$('#' + $(this).data('tab-target')).show();
// If the Business Profile tab is clicked, fetch data from the database
if ($(this).data('tab-target') === 'business-profile') {
$.ajax({
url: ajaxurl,
type: 'post',
data: {
action: 'fetch_business_profile_data'
},
success: function(response) {
// Clear the table
$('#company-table').empty();
// Add each row to the table
$.each(response, function(index, row) {
$('#company-table').append('<tr><td>' + row.company_name + '</td><td>' + row.company_address1 + '</td></tr>');
});
}
});
}
});
// Trigger click on the first tab to make it active by default
$('.nav-tab').first().trigger('click');
});
Please correct the errors and provide the complete corrected code blocks for each of those file, so I can copy/paste them back entirely.
GPT Response:
I apologize for the confusion. The error you’re seeing is because the fetch_business_profile_data
function is being declared twice. This is happening because the business_profile.php
file is being included twice: once in the settings.php
file and once in the index.php
file.
To fix this, you should remove the include
line for business_profile.php
from the index.php
file. The business_profile.php
file should only be included in the settings.php
file.
Here are the corrected code blocks:
- index.php:
/* Includes the install file that installs the databases */
include(plugin_dir_path(__FILE__) . 'base/install.php');
/* Includes the search and filter function used on the models page */
include(plugin_dir_path(__FILE__) . 'base/search/fetch_models.php');
/* Includes the model modifications PHP file */
require plugin_dir_path(__FILE__) . 'models/model_mods.php');
register_activation_hook(__FILE__, 'ersp_install');
- settings/settings.php:
<div class="wrap">
<h1><?= esc_html(get_admin_page_title()); ?></h1>
<nav class="nav-tab-wrapper">
<a href="#business-profile" class="nav-tab" data-tab-target="business-profile">Business profile</a>
<a href="#reset-db" class="nav-tab" data-tab-target="reset-db">Reset DB</a>
</nav>
<div id="tab-content">
<div id="business-profile" class="tab-content" style="padding-left: 20px;"><?php include plugin_dir_path(__FILE__) . 'business_profile.php'; ?></div>
<div id="reset-db" class="tab-content" style="padding-left: 20px;"><?php include plugin_dir_path(__FILE__) . 'reset_db.php'; ?></div>
</div>
</div>
<script type="text/javascript" src="<?php echo plugin_dir_url(__FILE__) . '../scripts/settings.js'; ?>"></script>
- settings/business_profile.php:
<?php
// Fetch Business Profile Data
function fetch_business_profile_data() {
global $wpdb;
// Fetch data from the database
$results = $wpdb->get_results("SELECT * FROM ersp_company", ARRAY_A);
// Return the data as a JSON response
wp_send_json($results);
}
// Add ajax action hook
add_action('wp_ajax_fetch_business_profile_data', 'fetch_business_profile_data');
?>
<tbody id="company-table">
<!-- data will be inserted here dynamically -->
</tbody>
- settings/scripts/settings.js:
jQuery(document).ready(function ($) {
$('.nav-tab').click(function (e) {
e.preventDefault();
// Switch tabs
$('.nav-tab').removeClass('nav-tab-active');
$(this).addClass('nav-tab-active');
$('.tab-content').hide();
$('#' + $(this).data('tab-target')).show();
// If the Business Profile tab is clicked, fetch data from the database
if ($(this).data('tab-target') === 'business-profile') {
$.ajax({
url: ajaxurl,
type: 'post',
data: {
action: 'fetch_business_profile_data'
},
success: function(response) {
// Clear the table
$('#company-table').empty();
// Add each row to the table
$.each(response, function(index, row) {
$('#company-table').append('<tr><td>' + row.company_name + '</td><td>' + row.company_address1 + '</td></tr>');
});
}
});
}
});
// Trigger click on the first tab to make it active by default
$('.nav-tab').first().trigger('click');
});
Please replace these files with the corrected code blocks. This should resolve the error you’re seeing.
NOW HERE’S A BREAKDOWN OF WHAT’S WRONG ABOUT THIS:
- require plugin_dir_path(FILE) . ‘models/model_mods.php’); contains a ) after php’) that’s not supposed to be there, wasn’t there and has never been there.
- I just explained in this prompt that settings.js is located in settings/scripts/settings.js because it messed up the file location in the previous response and I corrected it already in the example (settings.php) - yet, it changes it back again from: scripts/settings.js to: …/scripts/settings.js
- We just agreed in the previous prompt to move the include statement from settings.php to index.php. It generated the code blocks, which placed the include statements in both index.php and the settings.php page again. So, in this response it recognizes that the error is generated due to this mistake and then reverts to :
To fix this, you should remove the include
line for business_profile.php
from the index.php
file. The business_profile.php
file should only be included in the settings.php
file.
We just agreed to move that in the previous prompt, then it messes up by duplicating the include and then just reverts back to the previous state.
These are not extremely long code blocks. They are short and simple blocks of code I had no trouble with only recently, but now it’s turned into an absolute mess. I can’t use chatgpt to work with if it makes these kind of mistakes frequently. The reason why I’m on a paid plan is that 3.5 makes these kind of errors where 4 didn’t use to, but right now it’s almost as bad as. If this doesn’t change soon I may as well stop my plan as it’s starting to cost more time checking and trying to fix responses than it’s saving me time.