MyBB Hacks

Full Version: How to add a field in the dataabse with install function for a plugin I am creating.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I am creating my first plugin ever for mybb and I have gotten everything to work with the exeption of adding a field in the forums table.

The query I am trying to add is this:

PHP Code:
UPDATE 'mybb_forums' ADD 'forum_ignore' INT(1) NOT NULL DEFAULT '0'


When I run this query at the database my plugin works as it should. Howeevr I want to add it automatically with the install function in the plugin file. After I studied some plugins I tried this but it did not work.

PHP Code:
function forumsignore_install()
{
global $db;
$db->write_query("UPDATE ".TABLE_PREFIX."forums ADD 'forum_ignore' INT(1) NOT NULL DEFAULT '0'");		
}


Can someone please show me what I am doing wrong and how to fix it?

Thank you Smile

The MySQL query is incorrect.

You need to use an ALTER TABLE query to modify table schema.
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

UPDATE queries change values in the table, not the table itself.

Depending on what you do, you may also need to update the forum cache after you perform the query.  From memory, it should be something like:

PHP Code:
$cache->update_forums();

Thank you. I tried it but it is not working. This is the error message I get when I tried that.

PHP Code:
MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
    1064 - 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 'mybb_forums ADD 'forum_ignore' INT(1) NOT NULL DEFAULT '0'' at line 1
Query:
    ALTER mybb_forums ADD 'forum_ignore' INT(1) NOT NULL DEFAULT '0' 



Can you please post the correct code for me with the right install and uninstall functions with the query? I would really appreciate it. I have been trying for quite some time and I havent found the solution.

It's ALTER TABLE (look at the example queries on the page I linked to).

SQL Code
ALTER TABLE 'mybb_forums' ADD COLUMN 'forum_ignore' INT(1) NOT NULL DEFAULT '0'

ALTER TABLE 'mybb_forums' DROP COLUMN 'forum_ignore'


PHP Code:
function forumsignore_install()
{
global $db, $cache;
$db->write_query("ALTER TABLE ".TABLE_PREFIX."forums ADD 'forum_ignore' INT(1) NOT NULL DEFAULT '0'");
$cache->update_forums();
}
function forumsignore_is_installed()
{
global $db;
return $db->field_exists('forum_ignore', 'forums');
}
function forumsignore_uninstall()
{
global $db, $cache;
$db->write_query("ALTER TABLE ".TABLE_PREFIX."forums DROP 'forum_ignore'");
$cache->update_forums();
}

Thank you but it did not work for me. I got the same error again. I really appreciate your experienced help ZiNgA BuRgA.
Maybe Yumi meant something like this:

PHP Code:
function forumsignore_install()
{
global $db, $cache;
$db->write_query("ALTER TABLE `".TABLE_PREFIX."forums` ADD `forum_ignore` INT(1) NOT NULL DEFAULT '0'");
$cache->update_forums();
}
function forumsignore_is_installed()
{
global $db;
return $db->field_exists('forum_ignore', 'forums');
}
function forumsignore_uninstall()
{
global $db, $cache;
$db->write_query("ALTER TABLE `".TABLE_PREFIX."forums` DROP `forum_ignore`");
$cache->update_forums();
}

Oh, I forgot that MySQL didn't like those quotes.  Sorry about that and thanks RateU!
Thank you very much to both you. Now it works. I am still learning, was that quote difference that big that mysql didnt accept it?
I think that type of quote (single and double) is probably reserved for strings, not identifiers.
Thank you for your explanation.
Reference URL's