MyBB Community Forums

Full Version: help with bulk SQL (template inserts) via plugin install
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So for my garage system, I have to insert 45 new templates. I have a text file in proper SQL syntax that if I copy/paste the contents into phpMyAdmin, the templates insert fine.

My plugin uses file_get_contents to read the .sql file and then does a str_replace to get the correct table name for the users installation.

then I use a single $db->write_query() with the freshly replaced sql data and I get an generic error that doesnot help me find the problem.

Anyone have an idea?

	//insert new base templates
	if(file_exists(MYBB_ROOT."inc/plugins/garage/garage_templates.sql"))
	{
		$template_sql = @file_get_contents(MYBB_ROOT."inc/plugins/garage/garage_templates.sql");
		if($template_sql != '')
		{
			$template_sql = str_replace('mybb_templates', TABLE_PREFIX.'templates', $template_sql);
			$db->write_query($template_sql);
		}
	}

I have also attached a text file with the SQL queries in it (I can not attach a .sql file, so I renamed it .txt, but it is the exact file that I am pulling with the above code)
Rather than putting the sql calls in a separate file and reading them in as text put them in a php file and set the sql calls as an array

Like this:
<?php
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
$insert_array = array(
                                'garage_new_attachments_attachment' => '<tr>\r\n<td width="5%" align="center">{$attachment[\'icon\']}</td>\r\n<td style="white-space: nowrap">{$attachment[\'filename\']} ({$attachment[\'size\']})</td>\r\n<td style="white-space: nowrap; text-align: right;">{$attach_mod_options} <input type="submit" class="button" name="rem" value="{$lang->remove_attachment}" onclick="return Garage.removeAttachment({$attachment[\'aid\']});" /> {$postinsert}</td>\r\n</tr>',
                                'garage_view_comments_add' => '<tr><td class="{$trow_style}" align="center" colspan=2><p />\r\n<textarea cols="75" rows="5" name="comments"></textarea> <br>{$comment_text_limit}<br>\r\n<input type="submit" name="addcomment" value="Add Comment" />\r\n<input type="hidden" name="action" value="comments" />\r\n<input type="hidden" name="gid" value="{$mybb->input[\'gid\']}" />\r\n<input type="hidden" name="posthash" value="{$garage[\'posthash\']}" />\r\n<input type="hidden" name="commentcid" value="" />\r\n<input type="hidden" name="commentact" value="" />\r\n</form><p />\r\n</td></tr>',
...
...
                                'garage_view_admin_delete' => '<input type="submit" class="button" name="delete" value="{$lang->garage_delete}" onClick="return Garage.removeGarage({$mybb->input[\'gid\']});"/>'
);
?>

Then in the plugin do this:
include('sql_inserts.php'); // The file with the above sql array
foreach($insert_array as $template_title => $template_data)
	{
		$insert_templates = array(
			'title' => $db->escape_string($template_title),
			'template' => $db->escape_string($template_data),
			'sid' => "-1",
			'version' => "100",
			'dateline' => TIME_NOW
			);
		$db->insert_query('templates', $insert_templates);
	}

Make sure you have global $db in the function to use the db elements.
i had tried that but it was not working correctly. maybe i missed an escape or something.

i will have to look at it again tomorrow with fresh eyes. i'm "code blind" right now....
okay, so the thing i am having issue with the \r\n bits of the code. When escape_string is applied, they get \\r\\n and then show up as \r\n in the template, rather than a true new line.

without escape_string, the insert fails even though the template code is PHP valid as a string

so my plugin install function now has

	//insert new base templates
	if(file_exists(MYBB_ROOT."inc/plugins/garage/garage_templates.php"))
	{
		require_once(MYBB_ROOT."inc/plugins/garage/garage_templates.php");
		foreach($garage_templates as $title => $template)
		{
			$insert_array = array(
				'title' => $title,
				'template' => $db->escape_string($template),
				'sid' => -2,
				'version' => 1600,
				'dateline' => TIME_NOW
				);
			
			$db->insert_query('templates', $insert_array);
		}
	}

and the template file has

$garage_templates['garage_new_attachments_attachment'] = '<tr>\n<td width="5%" align="center">{$attachment["icon"]}</td>\n<td style="white-space: nowrap">{$attachment["filename"]} ({$attachment["size"]})</td>\n<td style="white-space: nowrap; text-align: right;">{$attach_mod_options} <input type="submit" class="button" name="rem" value="{$lang->remove_attachment}" onclick="return Garage.removeAttachment({$attachment["aid"]});" /> {$postinsert}</td>\n</tr>';

Nevermind, I got it. I totally spaced on escaping the $'s