Log in

View Full Version : Help With My Addon to Top X


gfxhavenowner
07-13-2004, 06:48 PM
This is my first hack, so try and help in as least complicated words as possible :). J/k.

Thanks to Zero Tolerence for giving me some guidence as well.

Here's my code so far.

<?php

// ######################### ERROR REPORTING #############################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);
define('GET_EDIT_TEMPLATES', true);
define('THIS_SCRIPT', 'topxadmin');

require_once('./global.php');
require_once('./includes/functions_misc.php');

$DB=$DB_site;

// ###################### Edit Settings ########################
if ($_GET['act'] == "update")
{
print_cp_header("Top X Control Panel");

$excludedforums=$DB->query_first("SELECT excludedforums FROM topx_admin");

print_form_header('topxadmin', 'do_update_topxsetting');
print_table_header("Top X Settings");
print_input_row("Forums to exclude", 'exclude_forums',$excludedforums['exclude_forums']);
print_submit_row("Update Top X Settings", 0);
}

if($_POST['do']=="do_update_topxsetting")
{
$DB->query("UPDATE topx_admin SET excludedforums='{$_POST['excludedforums']}'");
//Redirect
define('CP_REDIRECT', 'topxadmin.php?act=update');
print_stop_message('excluded_forums_updated');

}

?>

As you can see, my field name is excludedforums, my table name is topx_admin. excludedforums is a varchar(255), not null, with a blank default. It is the only field in the table.

The page builds everything alright. My AdminCP link is:

construct_nav_option("Forum Display Settings", 'topxadmin.php?act=update', '<br />');

which works fine.

excluded_forums_admin is a VB phrase. It contains the text "Excluded forums have been updated." and is a global phrase.

In the index.php of the main page(not AdminCP) I replaced the original Top X code of

$excludedforums = "9,10,11,12,26,58"; // forums to be excluded from latest threads. Format "0,5,7"

with my new code of

$excludedforums=$DB_site->("SELECT excludedforums FROM topx_admin");

Problem 1.

When I click the Update button in the AdminCP, I get "Could not find phrase 'excluded_forums_updated'."

Problem 2.

After replacing the original Top X code with mine, and going through the AdminCP and typing in any value, I get this error.

Parse error: parse error, unexpected '(', expecting T_STRING or T_VARIABLE or '{' or '$' in /home/.yardan/gfxhaven/gfxhaven.com/forums/index.php on line 423

Line 423 is the line my code is on.

Sorry for taking up so much space lol, if anyone could help me I would greatly apprieciate it and would glady give you credit.

Dark_Wizard
07-13-2004, 11:02 PM
First off change this:

$DB->query("UPDATE topx_admin SET excludedforums='{$_POST['excludedforums']}'");


to this:

$DB->query("
UPDATE topx_admin
SET excludedforums = $excludedforums
");


Then add your phrase to the "Control Panel Stop" phrases group

Dark_Wizard
07-13-2004, 11:22 PM
Also...add an id field to that table and call it "topx_adminid" as well and set it to autoincrement and set it as primary.

gfxhavenowner
07-13-2004, 11:23 PM
That fixed the phrase problem, thanks.

However, I still get the parse error.

Parse error: parse error, unexpected '(', expecting T_STRING or T_VARIABLE or '{' or '$' in /home/.yardan/gfxhaven/gfxhaven.com/forums/index.php on line 423

Dark_Wizard
07-13-2004, 11:31 PM
Well your script is missing some code and doesn't know what to update since you are not defining what to update, another words if you add an id field as I stated in my other post, this line:


$excludedforums=$DB->query_first("SELECT excludedforums FROM topx_admin");


should be changed to this:

$excludedforums = $DB->query_first("SELECT excludedforums FROM topx_admin WHERE id = $id");


Then you should globalize id after this:

if ($_GET['act'] == "update")
{



and while your at it change the above:

if ($_GET['act'] == "update")
{


to this:

if ($_REQUEST['do'] == "update")
{


and when you link it with a url set it like this:

topxadmin.php?do=update&amp;id=$id


also get rid of this:
$DB=$DB_site;
and just use $DB_site

hmmm...I keep finding more...

take this:

print_cp_header("Top X Control Panel");


and move it to where $DB=$DB_site; was

gfxhavenowner
07-13-2004, 11:44 PM
LOL, damn, didn't my code was so bad lol.

Anyways, here's the new code:

<?php

// ######################### ERROR REPORTING #############################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);
define('GET_EDIT_TEMPLATES', true);
define('THIS_SCRIPT', 'topxadmin');

require_once('./global.php');
require_once('./includes/functions_misc.php');

print_cp_header("Top X Control Panel");

// ###################### Edit Settings ########################
if ($_REQUEST['do'] == "update")
{

$excludedforums=$DB_site->query_first("SELECT excludedforums FROM topx_admin WHERE id = $topx_adminid");

print_form_header('topxadmin', 'do_update_topxsetting');
print_table_header("Top X Settings");
print_input_row("Forums to exclude", 'exclude_forums',$excludedforums['exclude_forums']);
print_submit_row("Update Top X Settings", 0);
}

if($_POST['do']=="do_update_topxsetting")
{
$DB_site->query(" UPDATE topx_admin SET excludedforums = '" . $_POST['excludedforums'] . "' ");

define('CP_REDIRECT', 'topxadmin.php?act=update');
print_stop_message('excluded_forums_updated');

}



?>


And here's the error I get when clicking the link.

Database error in vBulletin 3.0.3:

Invalid SQL: SELECT excludedforums FROM topx_admin WHERE id =
mysql error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

mysql error number: 1064

Andreas
07-13-2004, 11:47 PM
to this:

$DB->query("
UPDATE topx_admin
SET excludedforums = '" . $_POST['excludedforums'] . "'
");



Never-ever should one use a variable from an URL or user input unquoted in an SQL-query!

Dark_Wizard
07-13-2004, 11:53 PM
Never-ever should one use a variable from an URL or user input unquoted in an SQL-query!


Agreed...just went through this very fast without wanting to rewrite all of his code...

gfxhavenowner
07-13-2004, 11:54 PM
Agreed...just went through this very fast without wanting to rewrite all of his code...

:( I'm getting the feeling like I should just cut my losses now lol.

Dark_Wizard
07-13-2004, 11:58 PM
:( I'm getting the feeling like I should just cut my losses now lol.

Your code is missing alot unless your not posting all of it. May I make a suggestion...check out how vb does it and get your ideas from that...if your still having problems let me know offline instead of posting everything back and forth in a thread and getting you more confused.

I really didn't want to rewrite your code for you as this would be a good learning experience for you and at the same time gets you more familiar with php as the dev's a vb.com have done an excellent job on vb 3.x......

gfxhavenowner
07-14-2004, 12:57 AM
Thanks for the help. I have no clue what I'm doing wrong though lol.

Edit : decided to give up on it. Spent 3 days reading through the Reference Manuals for both MySQL and PHP and still get these errors.

gfxhavenowner
07-14-2004, 01:29 AM
Ok, this is really pissing me off. Here's my code.

<?php

// ######################### ERROR REPORTING #############################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);
define('GET_EDIT_TEMPLATES', true);
define('THIS_SCRIPT', 'topxadmin');

require_once('./global.php');
require_once('./includes/functions_misc.php');

// ###################### Edit Settings ########################
if ($_GET['act'] == "update")
{
print_cp_header("Top X Control Panel");
$excludedforums=$DB_site->query_first("SELECT excludedforums FROM topx_admin WHERE topx_adminid = '1'");

print_form_header('topxadmin', 'do_update_topxsetting');
print_table_header("Top X Settings");
print_input_row("Forums to exclude", 'exclude_forums',$excludedforums['exclude_forums']);
print_submit_row("Update Top X Settings", 0);
}

if ($_POST['do'] == "do_update_topxsetting")
{
print_cp_header("Top X Control");

$DB_site->query("UPDATE topx_admin SET excludedforums='{$_POST['exclude_forums']}' WHERE topx_adminid = '1'");

define('CP_REDIRECT', 'topxadmin.php?act=update');
print_stop_message('excluded_forums_updated');

}


?>

Now, everything looks right, I don't see any problems, but that might just be me. Please, some one point out a problem and WHY it's wrong, not just how to fix it.

Edit : feel free to AIM me at thumbsup2mrcoffe to help me, or MSN at thumbsup@gfxhaven.com

Andreas
07-14-2004, 06:54 AM
First of all, you should specify a default action:


if (!$_REQUEST['do']) {
$_REQUEST['do'] = 'update';
}


Furtheremore, vB3 (this is for Vb3, right?) does always use do= for actions, so you should also use this:


if ($_REQUEST['do'] == "update")
{


Then, if this hack is not just for yourself but for release you should keep in mind ppl using table prefixes:

$excludedforums=$DB_site->query_first("SELECT excludedforums FROM" . TABLE_PREFIX . "topx_admin WHERE topx_adminid = '1'");


You must also use the same column name as in your table when you use print_input_row:


print_input_row("Forums to exclude", 'exclude_forums', $excludedforums['excludedforums']);



As already stated above, you must always quote strings from a form or URL, as otherwise this will be a high security risk:


$DB_site->query("UPDATE " . TABLE_PREFIX . "topx_admin SET excludedforums='" . addslashes($_POST['exclude_forums']) . "' WHERE topx_adminid = '1'");



define('CP_REDIRECT', 'topxadmin.php?do=update');

Once again, you should use do= here.

As a last thing, your script is missing print_cp_footer();

Here is the whole script (topxadmin.php in admincp directory):

<?php

// ######################### ERROR REPORTING #############################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);
define('GET_EDIT_TEMPLATES', true);
define('THIS_SCRIPT', 'topxadmin');

require_once('./global.php');
require_once('./includes/functions_misc.php');

print_cp_header('Top X Admin');

if (!$_REQUEST['do']) {
$_REQUEST['do'] = 'update';
}

// ###################### Edit Settings ########################
if ($_REQUEST['do'] == "update")
{
$excludedforums = $DB_site->query_first("SELECT excludedforums FROM " . TABLE_PREFIX . "topx_admin WHERE topx_adminid = '1'");

print_form_header('topxadmin', 'do_update_topxsetting');
print_table_header("Top X Settings");
print_input_row("Forums to exclude", 'exclude_forums', $excludedforums['excludedforums']);
print_submit_row("Update Top X Settings", 0);
}

if ($_POST['do'] == "do_update_topxsetting")
{
$DB_site->query("UPDATE " . TABLE_PREFIX . "topx_admin SET excludedforums='" . addslashes($_POST['exclude_forums']) . "' WHERE topx_adminid = '1'");

define('CP_REDIRECT', 'topxadmin.php?do=update');
print_stop_message('excluded_forums_updated');
}

print_cp_footer();

?>


This does work for me, given your table looks like that:

CREATE TABLE `topx_admin` (
`topx_adminid` int(10) unsigned NOT NULL auto_increment,
`excludedforums` varchar(255) NOT NULL default '',
PRIMARY KEY (`topx_adminid`)
) TYPE=MyISAM AUTO_INCREMENT;

gfxhavenowner
07-14-2004, 12:12 PM
I can't get my table to work with the MyISAM AUTO_INCREMENT, and I get this error still lol.

Database error in vBulletin 3.0.3:

Invalid SQL: SELECT excludedforums FROMtopx_admin WHERE topx_adminid = '1'
mysql error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE topx_adminid = '1'' at line 1

mysql error number: 1064